这就是一个响应多个action的例子用ActionEvent e来判断package textarea;import javax.swing.UIManager;
import java.awt.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Application1 {
boolean packFrame = false; //Construct the application
public Application1() {
Txtarea frame = new Txtarea();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}
}
package textarea;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Txtarea extends JFrame {
JPanel contentPane;
boolean flag =true;
JScrollPane sp = new JScrollPane();
JTextArea txt1 = new JTextArea();
JButton btn1 = new JButton();
JMenuBar mbar = new JMenuBar();
JMenu menu1=new JMenu("File");
JMenu menu2=new JMenu("Color");
JMenuItem f1=new JMenuItem("Open");
JMenuItem f2=new JMenuItem("new");
JMenuItem f3=new JMenuItem("Save");
JMenuItem f4=new JMenuItem("Save All");
JMenuItem f5=new JMenuItem("Print");
JMenuItem f6=new JMenuItem("Quit");
JMenuItem c1=new JMenuItem("Red");
JMenuItem c2=new JMenuItem("Green");
JMenuItem c3=new JMenuItem("Blue");
JTextField txt2 = new JTextField();
//Construct the frame
public Txtarea() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(433, 343));
this.setTitle("Frame Title");
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setBounds(new Rectangle(78, 31, 234, 202));
txt1.setText("I love China!");
btn1.setBounds(new Rectangle(317, 74, 72, 40));
btn1.setText("Press");
btn1.addActionListener(new myactionlistener());
txt2.setBounds(new Rectangle(77, 246, 235, 37));
menu1.add(f1);
menu1.add(f2);
menu1.add(f3);
menu1.add(f4);
menu1.add(f5);
menu1.addSeparator();
menu1.add(f6);
c1.addActionListener(new myactionlistener());
c2.addActionListener(new myactionlistener());
c3.addActionListener(new myactionlistener());
menu2.add(c1);
menu2.add(c2);
menu2.add(c3);
mbar.add(menu1);
mbar.add(menu2);
setJMenuBar(mbar);
contentPane.add(sp, null);
contentPane.add(btn1, null);
contentPane.add(txt2, null);
sp.getViewport().add(txt1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}class myactionlistener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == btn1) {
if (flag == true) {
txt1.setText("kdsjfkskjfkskslkdffjlks");
flag = false;
}
else {
txt1.setText("I love China!");
flag = true;
}
}
if(evt.getSource()==c1)
txt2.setBackground(Color.red);
else
if(evt.getSource()==c2)
txt2.setText("kskdsjklsajfkl");
else
if(evt.getSource()==c3)
{
txt2.setText("You love me!");
} }
}
}
import java.awt.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Application1 {
boolean packFrame = false; //Construct the application
public Application1() {
Txtarea frame = new Txtarea();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}
}
package textarea;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Txtarea extends JFrame {
JPanel contentPane;
boolean flag =true;
JScrollPane sp = new JScrollPane();
JTextArea txt1 = new JTextArea();
JButton btn1 = new JButton();
JMenuBar mbar = new JMenuBar();
JMenu menu1=new JMenu("File");
JMenu menu2=new JMenu("Color");
JMenuItem f1=new JMenuItem("Open");
JMenuItem f2=new JMenuItem("new");
JMenuItem f3=new JMenuItem("Save");
JMenuItem f4=new JMenuItem("Save All");
JMenuItem f5=new JMenuItem("Print");
JMenuItem f6=new JMenuItem("Quit");
JMenuItem c1=new JMenuItem("Red");
JMenuItem c2=new JMenuItem("Green");
JMenuItem c3=new JMenuItem("Blue");
JTextField txt2 = new JTextField();
//Construct the frame
public Txtarea() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(433, 343));
this.setTitle("Frame Title");
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setBounds(new Rectangle(78, 31, 234, 202));
txt1.setText("I love China!");
btn1.setBounds(new Rectangle(317, 74, 72, 40));
btn1.setText("Press");
btn1.addActionListener(new myactionlistener());
txt2.setBounds(new Rectangle(77, 246, 235, 37));
menu1.add(f1);
menu1.add(f2);
menu1.add(f3);
menu1.add(f4);
menu1.add(f5);
menu1.addSeparator();
menu1.add(f6);
c1.addActionListener(new myactionlistener());
c2.addActionListener(new myactionlistener());
c3.addActionListener(new myactionlistener());
menu2.add(c1);
menu2.add(c2);
menu2.add(c3);
mbar.add(menu1);
mbar.add(menu2);
setJMenuBar(mbar);
contentPane.add(sp, null);
contentPane.add(btn1, null);
contentPane.add(txt2, null);
sp.getViewport().add(txt1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}class myactionlistener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == btn1) {
if (flag == true) {
txt1.setText("kdsjfkskjfkskslkdffjlks");
flag = false;
}
else {
txt1.setText("I love China!");
flag = true;
}
}
if(evt.getSource()==c1)
txt2.setBackground(Color.red);
else
if(evt.getSource()==c2)
txt2.setText("kskdsjklsajfkl");
else
if(evt.getSource()==c3)
{
txt2.setText("You love me!");
} }
}
}
不过还是要感谢fruitking(许果)。