以下代码怎么改正? import java.awt.Button; import java.
以下代码怎么改正?importjava.awt.Button;importjava.awt.FlowLayout;importjavax.swing.JFrame;imp...
以下代码怎么改正?
import java.awt.Button;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test_event implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JOptionPane.showMessageDialog(null,"Hello world");
}
public static void main(String[] args)
{
JFrame jframe=new JFrame("石航源软件");
Button button1,button2,button3;
button1=new Button("1");
button2=new Button("2");
button3=new Button("3");
FlowLayout flowlayout=new FlowLayout();
flowlayout.setAlignment(FlowLayout.LEFT);
jframe.setLayout(flowlayout);
jframe.add(button1);
jframe.add(button2);
jframe.add(button3);
jframe.setSize(500,600);
jframe.setVisible(true);
button1.addActionListener(e); 展开
import java.awt.Button;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test_event implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JOptionPane.showMessageDialog(null,"Hello world");
}
public static void main(String[] args)
{
JFrame jframe=new JFrame("石航源软件");
Button button1,button2,button3;
button1=new Button("1");
button2=new Button("2");
button3=new Button("3");
FlowLayout flowlayout=new FlowLayout();
flowlayout.setAlignment(FlowLayout.LEFT);
jframe.setLayout(flowlayout);
jframe.add(button1);
jframe.add(button2);
jframe.add(button3);
jframe.setSize(500,600);
jframe.setVisible(true);
button1.addActionListener(e); 展开
1个回答
展开全部
修改后的效果图
参考代码
import java.awt.Button;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test_event implements ActionListener {
//把main方法的代码, 移动到构造器里
public Test_event() {
JFrame jframe = new JFrame("石航源软件");
Button button1, button2, button3;
button1 = new Button("1");
button2 = new Button("2");
button3 = new Button("3");
FlowLayout flowlayout = new FlowLayout();
flowlayout.setAlignment(FlowLayout.LEFT);
jframe.setLayout(flowlayout);
jframe.add(button1);
jframe.add(button2);
jframe.add(button3);
jframe.setSize(500, 600);
jframe.setVisible(true);
button1.addActionListener(this);//修改的地方, 添加自身, 因为已经实现了ActionListener
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//建议添加的代码,当点击关闭按钮,可以退出虚拟机,释放系统资源
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello world");
}
public static void main(String[] args) {
new Test_event();//main方法里,调用构造函数.创建窗口
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询