mybutton.java
来自「java课程的资料以及实验的代码」· Java 代码 · 共 53 行
JAVA
53 行
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 + =
减小字号Ctrl + -
显示快捷键?