下面是一个按钮的实例
package com.liuyanzhao;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Demo2 implements ActionListener {
JPanel panel;
JButton button;
public static void main(String[] args) {
Demo2 d = new Demo2();
d.go();
}
public void go() {
JFrame frame = new JFrame();
button = new JButton("红色");
panel = new JPanel();
panel.setSize(200,100);
panel.add(button);
button.addActionListener(this);//向按钮注册
frame.setBackground(Color.red);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.setBounds(100, 200, 300, 300);
}
@Override
public void actionPerformed(ActionEvent e) { //处理事件的方法
if(e.getActionCommand()=="红色") {
button.setText("蓝色");
panel.setBackground(Color.red);
button.setEnabled(true);
} else if(e.getActionCommand()=="蓝色") {
button.setText("红色");
panel.setBackground(Color.blue);
button.setEnabled(true);
}
}
}
效果是:点击按钮button,按钮上的字会变化,容器panel的背景会变色,按钮会重新激活,可以循环。
下面是两个按钮的实例
package com.liuyanzhao;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Demo2 implements ActionListener {
JButton button_open ;
JButton button_close;
Label label;//这个地方不要用JLable,否则空白符不占位
Label label2;
public static void main(String[] args) {
Demo2 d = new Demo2();
d.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setSize(300, 100);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
label = new Label("灯状态:");
label2 = new Label(" ");
button_open = new JButton("开灯");
button_close = new JButton("关灯");
button_open.addActionListener(this);
button_close.addActionListener(this);
panel.add(label);
panel.add(label2);
panel.add(button_open);
panel.add(button_close);
}
@Override
public void actionPerformed(ActionEvent e) {
//方法一:getActionCommand
// if(e.getActionCommand()=="开灯") {
// label2.setBackground(Color.red);
// button_open.setEnabled(false);
// button_close.setEnabled(true);
// } else if(e.getActionCommand()=="关灯") {
// label2.setBackground(Color.black);
// button_close.setEnabled(false);
// button_open.setEnabled(true);
// }
//方法二:getSource
if(e.getSource()==button_open) {//button_open不要加引号
label2.setBackground(Color.red);
button_open.setEnabled(false);
button_close.setEnabled(true);
} else if(e.getSource()==button_close) {//button_closen不要加引号
label2.setBackground(Color.black);
button_close.setEnabled(false);
button_open.setEnabled(true);
}
}
}
使用内部类
package com.liuyanzhao;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Demo2 {
public Demo2 daili;
JButton button_open ;
JButton button_close;
Label label;//这个地方不要用JLable,否则空白符不占位
Label label2;
public static void main(String[] args) {
Demo2 d = new Demo2();
d.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setSize(300, 100);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
label = new Label("灯状态:");
label2 = new Label(" ");
button_open = new JButton("开灯");
button_close = new JButton("关灯");
//相对于将this传给注册方法,现在传的是对应的实例
button_open.addActionListener(new MyListener());
button_close.addActionListener(new MyListener());
panel.add(label);
panel.add(label2);
panel.add(button_open);
panel.add(button_close);
}
//我们可以创建多个内部类,比如LabelListener和ColorListener
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button_open) {//button_open不要加引号
label2.setBackground(Color.red);
button_open.setEnabled(false);
button_close.setEnabled(true);
} else if(e.getSource()==button_close) {//button_closen不要加引号
label2.setBackground(Color.black);
button_close.setEnabled(false);
button_open.setEnabled(true);
}
}
}
}
效果图如下