📜  Java AWT Button

📅  最后修改于: 2020-09-28 04:44:29             🧑  作者: Mango

Java AWT按钮

button类用于创建具有平台独立实现的带标签的按钮。按下按钮后,应用程序将执行某些操作。

AWT按钮类声明

public class Button extends Component implements Accessible

Java AWT按钮示例

import java.awt.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
Button b=new Button("Click Here");
b.setBounds(50,100,80,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

输出:

Java AWT按钮示例与ActionListener

import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
final TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
            tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

输出: