写出完整的java程序,实现一个有按钮的窗口,按钮名字为“请点击”。要怎么写?

2024-11-07 16:31:12
推荐回答(3个)
回答1:

我这里有个最基本的用法,你可以参考一下

import java.awt.*;
import java.awt.event.*;

public class TestA extends Frame {
Button bt = new Button("请点击");
TestA() {
setLayout(null);
// 关闭窗口
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setBounds(200, 100, 200, 200);
setTitle("小窗口");
setVisible(true);
bt.setBounds(50,50,40,20);
add(bt);
}
public static void main(String[] args) {
new TestA();
}
}

回答2:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import textedit.MainFrame;
public class click extends JFrame {
private JButton clickButton = new JButton();
public click() {
try {
jbInit(); // 初始化文本编辑器
} catch (Exception e) {
e.printStackTrace();
}
this.addWindowListener(new WindowAdapter() { // 添加监听
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setSize(600, 600);
this.setVisible(true);
}
public static void main(String[] args) {

click click = new click(); // 声明Frame
}
private void jbInit() throws Exception {
clickButton.setText("请点击我");
clickButton.setSize(10,10);
this.add(clickButton);
}
}

回答3: