写出完整的Java程序,实现有一个按钮的窗口点击按钮会弹出一条消息

2024-11-20 07:29:39
推荐回答(2个)
回答1:

不是吧,还有这么简单的?
import java.awt.*;
import java.awt.event.*;
public class Test implements ActionListener{
javax.swing.JFrame frame=new javax.swing.JFrame("test");
Button button=new Button("click me");
public Test(){

button.addActionListener(this);
frame.getContentPane().add(button);
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Test();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
javax.swing.JOptionPane.showMessageDialog(null, "You do it!");
}
}
}

有问题继续问......

回答2:

看看这个
import javax.swing.*;
import java.awt.event.*;

public class test extends JFrame implements ActionListener {
static JButton btn=new JButton("click");
static test t=new test();
public static void main(String[] args) {
t.setBounds(300, 300, 300, 200);
t.setLayout(null);
btn.setBounds(120, 60, 80, 30);
btn.addActionListener(t);
t.add(btn);
t.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
javax.swing.JOptionPane.showMessageDialog(null, "You do it");

}

}