Java开发GUI之Button控件
Java中的awt包提供了丰富的用户界面组件。重要的是,Java的跨平台性使用awt包可以在Windows,MacOS等平台创建桌面软件。本篇博客总结Button控件的简单使用。
package App;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonTest {
public static void main(String[] args ) {
//创建面板
Frame frame = new Frame("BUTTON");
//创建按钮
Button button1 = new Button();
//设置按钮标题
button1.setLabel("按钮");
//设置按钮标记 用在触发方法中分区按钮
button1.setActionCommand("tag1");
//获取按钮标题
System.out.println(button1.getLabel());
//添加按钮
frame.add(button1);
//添加交互监听
button1.addActionListener(new MyActionListener());
frame.pack();
frame.show();
}
}
class MyActionListener implements ActionListener{