testbutton.java

来自「支持图形化界面GUI工具 有关JAVA 容器 监听类的例子」· Java 代码 · 共 28 行

JAVA
28
字号
import java.awt.*;
import java.awt.event.* ;

public class TestButton{
    public static void main(String args[ ]){
		Frame f = new Frame("Test");
		 FlowLayout bl=new FlowLayout();
		 f.setLayout(bl);
        Label label=new Label("内容显示");
		f.add(label,"Center");
		Button b = new Button("Press Me!");
		b.addActionListener(new ButtonHandler( )); //注册点击鼠标事件监听器。
		f.add(b,"South");//BorderLayout.SOUTH
		f.setSize(200,100);
		f.setVisible(true) ;
		f.setBackground(Color.yellow);
	}
 }

//定义ActionEvent监听器类
class ButtonHandler implements ActionListener{

    public void actionPerformed(ActionEvent e){
		System.out.println("Action occurred");
		System.out.println("Button's label is:"+
		                       e.getActionCommand());
		}
 	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?