📄 9.11-testactionevent.java
字号:
// TestActionEvent.java: Create a Close button in the frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent extends JFrame
implements ActionListener
{
// Create an object for "Close" button
private JButton jbtOk = new JButton("OK");
private JButton jbtCancel = new JButton("Cancel");
// Default constructor
public TestActionEvent()
{
// Set the window title
setTitle("TestActionEvent");
// Set FlowLayout manager to arrange the components
// inside the frame
getContentPane().setLayout(new FlowLayout());
// Add buttons to the frame
getContentPane().add(jbtOk);
getContentPane().add(jbtCancel);
// Register listeners
jbtOk.addActionListener(this);
jbtCancel.addActionListener(this);
}
// Main method
public static void main(String[] args)
{
TestActionEvent frame = new TestActionEvent();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 80);
frame.setVisible(true);
}
// This method will be invoked when a button is clicked.
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtOk)
{
System.out.println("The OK button is clicked");
}
else if (e.getSource() == jbtCancel)
{
System.out.println("The Cancel button is clicked");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -