📄 simple1.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Simple1
{
private static JFrame frame;
private static JPanel myPanel;
private JButton button1;
private JButton button2;
public Simple1()
{
myPanel=new JPanel();
button1=new JButton("Button 1");
button2=new JButton("Button 2");
SimpleListener ourListener=new SimpleListener();
button1.addActionListener(ourListener);
button2.addActionListener(ourListener);
myPanel.add(button1);
myPanel.add(button2);
}
private class SimpleListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String buttonName=e.getActionCommand();
if(buttonName.equals("Button 1"))
JOptionPane.showMessageDialog(frame,"Button 1 Pressed");
else if(buttonName.equals("Button 2"))
JOptionPane.showMessageDialog(frame,"Button 2 Pressed");
else
JOptionPane.showMessageDialog(frame,"Unknown event");
}
}
public static void main(String s[])
{
Simple1 gui=new Simple1();
frame=new JFrame("Simple1");
frame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.getContentPane().add(myPanel);
frame.pack();
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -