用Java语言编写一个图形界面,由于初学,下面是代码,求指教
发布网友
发布时间:2022-06-03 05:49
我来回答
共4个回答
热心网友
时间:2023-10-12 18:23
必须实现必须实现WindowListener接口里的方法
package cn.broadengate.day1;
import java.awt.*;
import java.awt.event.*;
class prassa extends Frame implements ActionListener,WindowListener
{
TextArea t;
Button b;
int x=0;
Label l;
prassa()
{
l=new Label();
t=new TextArea("shdjhdj");
b=new Button("单击");
setLayout(new BorderLayout());//边界布局
add("North",l);
add("Center",t);
add("South",b);
t.setEditable(false);
b.addActionListener(this);//监听按钮b
addWindowListener(this);
setSize(400,400);
setVisible(true);
}
public static void main(String []ae)
{
new prassa();//实例化窗口
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
x=x+1;
t.setText("你已经第"+""+x+""+"此点击按钮");
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowActivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowClosed(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeactivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeiconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowIconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowOpened(WindowEvent e) {
// TODO 自动生成方法存根
}
}
热心网友
时间:2023-10-12 18:23
也不必专门实现WindowListener嘛,写成内部的类,才是最常用的办法,也不难掌握的
把最后的那个windowClosing方法改成下面的样子,写进构造方法里边:
addWindowListener(new windowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
});
热心网友
时间:2023-10-12 18:24
l类名一般首字母大写 Prassa ,并且驼峰式,这是java并命规范
2 实现接口就要实现它的所有方法,即使是空方法也要写,如果不想实现所有方法 可以用适配器Adapter
热心网友
时间:2023-10-12 18:24
1.类名一般大写 prassa
2.必须实现WindowListener接口里的方法
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}