帮忙调试一个简单的java程序,不知道哪错了

2025-03-13 12:15:09
推荐回答(2个)
回答1:

import java.awt.*;
import java.awt.event.*;
public class smp61 extends Frame implements ActionListener{
Button btn = new Button("退出");
public smp61(){
btn.setBackground(Color.yellow);
btn.setForeground(Color.blue);
add(btn);
setVisible(true);
pack();
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn){
System.exit(0);
}
}
public static void main(String args[]){
smp61 smp = new smp61();
}
}

回答2:

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class smp61 extends Frame implements ActionListener
{
Button btn=new Button("退出");

smp61()
{
btn.setBackground(Color.yellow);
btn.setForeground(Color.blue);
add(btn);
setVisible(true);
pack();
btn.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn)
{
System.exit(0);
}
}

public static void main(String args[])
{
smp61 smp=new smp61();
}
}

actionPerformed(),p大写,构造方法没有返回值