本人是初学者!
刚刚编写了一个小GUI程序源程序如下:
import java.awt.*;
import java.awt.event.*;
class Buttonevent
{
public static void main(String[] args)
{
Frame myFrame=new Frame();
}
class frame extends Frame implements ActionListener
{
private Button button1;
private Button button2;
private Label label1;
//构造方法
frame()
{
super("第一个图形用户程序");
this.setLayout(new FlowLayout());
button1=new Button("确定");
button1.addActionListener(this);
button2=new Button("取消");
button2.addActionListener(this);
label1=new Label();
this.add(button1);
this.add(button2);
this.add(label1);
//设置一个窗口监听方法
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
this.pack();
this.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
label1.setText("这是正确的");
else
label1.setText("");
this.pack();
}
}
}
程序到此处!
编译时没有其他什么问题就是不能运行还要编译完有如下提示:
Note:Buttonevent.java uses or overrides a deprecated API.
Note:Recompile with _Xlint:deprecation for details.
就这些程序是不能运行,是为社么呢?
求教!谢谢!
刚刚编写了一个小GUI程序源程序如下:
import java.awt.*;
import java.awt.event.*;
class Buttonevent
{
public static void main(String[] args)
{
Frame myFrame=new Frame();
}
class frame extends Frame implements ActionListener
{
private Button button1;
private Button button2;
private Label label1;
//构造方法
frame()
{
super("第一个图形用户程序");
this.setLayout(new FlowLayout());
button1=new Button("确定");
button1.addActionListener(this);
button2=new Button("取消");
button2.addActionListener(this);
label1=new Label();
this.add(button1);
this.add(button2);
this.add(label1);
//设置一个窗口监听方法
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
this.pack();
this.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
label1.setText("这是正确的");
else
label1.setText("");
this.pack();
}
}
}
程序到此处!
编译时没有其他什么问题就是不能运行还要编译完有如下提示:
Note:Buttonevent.java uses or overrides a deprecated API.
Note:Recompile with _Xlint:deprecation for details.
就这些程序是不能运行,是为社么呢?
求教!谢谢!
class frame extends Frame implements ActionListener -> static class frame extends Frame implements ActionListener
为什么要加static?
我在书中学到不加啊!
谢谢回答!
main方法是个静态的方法,静态方法的里面不能直接构造非静态的内部类的对象。
如果你不想把frame类改成静态的,就把frame类写到Buttonevent类的外面