📄 mybutton.java
字号:
import java.awt.*;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.*;
class MyButtonPanel extends Panel{
public MyButtonPanel() {
Button bButton = new Button( "蓝色" );
Button gButton = new Button( "绿色" );
Button eButton = new Button( "退出" );
add( bButton );
add( gButton );
add( eButton );
MyActionListener bAction = new MyActionListener( Color.blue );
MyActionListener gAction = new MyActionListener( Color.green );
MyActionListener eAction = new MyActionListener( Color.red );
bButton.addActionListener(bAction);
gButton.addActionListener(gAction);
eButton.addActionListener(eAction);
}
private class MyActionListener implements ActionListener {
private Color bgColor;
public MyActionListener( Color c ){
this.bgColor = c;
}
public void actionPerformed( ActionEvent event ){
setBackground( bgColor );
repaint();
if( event.getActionCommand() == "退出" )
System.exit(0);
}
}
}
class MyButtonFrame extends Frame {
public MyButtonFrame() throws HeadlessException {
setTitle( "测试按钮事件" );
setSize( 300,200 );
MyButtonPanel panel = new MyButtonPanel();
add(panel);
}
}
public class MyButton {
public static void main( String[] args ){
MyButtonFrame frm = new MyButtonFrame();
frm.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -