📄 eventsample3.java
字号:
// 例6.2.3 EventSample3.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventSample3 implements ActionListener
{
private static JFrame frame;
private static JPanel pl;
private JButton btn1,btn2;
public EventSample3()
{
frame = new JFrame("事件演示窗口");
pl = new JPanel();
btn1 = new JButton("按我可以听到声音");
btn2 = new JButton("按我弹出信息对话框");
btn1.addActionListener(this);
btn2.addActionListener(this);
pl.add(btn1);pl.add(btn2);
frame.getContentPane().add(pl);
frame.setSize(350,100);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn1) //getSource判断哪个按钮被按下了
Toolkit.getDefaultToolkit().beep();
if(e.getSource()==btn2)
JOptionPane.showMessageDialog(frame,"欢迎学习事件处理!");
}
public static void main(String[] args)
{
new EventSample3();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -