import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class MyFrame1 extends JFrame {
private JLabel lbl = new JLabel("Hello, world!");
private JButton okButton = new JButton("OK");
public MyFrame1() {
this.setSize(400, 300);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout()); // 使用流式布局管理器
lbl.setFont(new Font("Consolas", Font.BOLD, 36));
this.add(lbl); // 向窗口添加标签控件
// 为按钮绑定点击事件(添加一个监听器并重写回调方法)
// 创建匿名内部类对象作为监听器绑定到按钮上
okButton.addActionListener(new ActionListener() { // 就地实例化ActionListener接口
@Override
public void actionPerformed(ActionEvent e) { // 点击按钮时需要执行的回调方法
// 在此处添加处理按钮点击事件的代码
lbl.setVisible(!lbl.isVisible());
}
});
this.add(okButton); // 向窗口添加按钮控件
}
public static void main(String[] args) {
new MyFrame1().setVisible(true);
}
}
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class MyFrame2 extends JFrame implements ActionListener {
private JLabel lbl = new JLabel("Hello, world!");
private JButton okButton = new JButton("OK");
public MyFrame2() {
this.setSize(400, 300);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout()); // 使用流式布局管理器
lbl.setFont(new Font("Consolas", Font.BOLD, 36));
this.add(lbl); // 向窗口添加标签控件
// 用当前类的对象充当监听器
// 由于当前类实现了ActionListener接口所以具备了充当监听器的能力
okButton.addActionListener(this);
this.add(okButton); // 向窗口添加按钮控件
}
public static void main(String[] args) {
new MyFrame2().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
lbl.setVisible(!lbl.isVisible());
}
}
import java.awt.FlowLayout;
import java.awt.Font;
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.JOptionPane;
@SuppressWarnings("serial")
public class MyFrame3 extends JFrame {
// 内部类(内部类可以访问外部类的成员,包括private成员)
public class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
lbl.setVisible(!lbl.isVisible());
}
}
public class MyListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "shit!!!");
}
}
private JLabel lbl = new JLabel("Hello, world!");
private JButton okButton = new JButton("OK");
public MyFrame3() {
this.setSize(400, 300);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout()); // 使用流式布局管理器
lbl.setFont(new Font("Consolas", Font.BOLD, 36));
this.add(lbl); // 向窗口添加标签控件
// 创建内部类对象作为监听器
okButton.addActionListener(new MyListener2());
okButton.addActionListener(new MyListener());
this.add(okButton); // 向窗口添加按钮控件
}
public static void main(String[] args) {
new MyFrame3().setVisible(true);
}