📄 jbuttondemo.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonDemo extends JFrame
{
private JButton button1, button2;
public JButtonDemo(){
super("测试按钮类");
setSize(250, 100);
//设置外观
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
//获取内容面板
Container container = getContentPane();
//设置内容面板的布局管理器
container.setLayout(new GridLayout(1, 2));
container.setBackground(Color.YELLOW);
//创建按钮对象
button1 = new JButton("按钮1");
button1.setFont(new Font("Serif", Font.PLAIN, 12));
ImageIcon img1 = new ImageIcon("wave.gif");
ImageIcon img2 = new ImageIcon("stop.gif");
button2 = new JButton("按钮2", img1);
button2.setRolloverIcon(img2);
button2.setFont(new Font("Serif", Font.PLAIN, 12));
//为组件注册监听器
ButtonHandler handler = new ButtonHandler();
button1.addActionListener(handler);
button2.addActionListener(handler);
//将各种组件添加到内容面板
container.add(button1);
container.add(button2);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
JButtonDemo demo = new JButtonDemo();
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(JButtonDemo.this,
"你按了: " + event.getActionCommand());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -