当前位置: 代码迷 >> Java相关 >> 程序代码错在哪?
  详细解决方案

程序代码错在哪?

热度:217   发布时间:2007-06-11 14:54:12.0
程序代码错在哪?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class demo extends JFrame {

public demo(String t) {
super(t);
Container contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
setBackground(Color.white);
contentPane.add(a1);
contentPane.add(a2);
contentPane.add(a3);
contentPane.add(a4);
contentPane.add(a5);
contentPane.add(a6);


a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xu");}
});
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xian");}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("yue");}
});

a5.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
a6.setText("yes");
}
});
}
JButton a1=new JButton("demo1");
JButton a2=new JButton("demo2");
JButton a3=new JButton("demo3");
JLabel a4=new JLabel("demo4");
JLabel a6=new JLabel("demo6");
JCheckBox a5=new JCheckBox("demo5",false);

public static void main(String[] args) {
demo beat =new demo("good");
beat.addWindowListener(new WindowAdapter() {
                      //匿名类用于注册监听器
       public void windowClosing(WindowEvent e) {
         System.exit(0);
       }
     });
beat.setSize(500,300);
beat.setVisible(true);

}
}
搜索更多相关的解决方案: 代码  

----------------解决方案--------------------------------------------------------
main函数是静态的,而setSize,addwindowListener等都非静态的
应该你放在其他位置就不会了,
但是我也不明白如果是:很多地方都会这样写: new demo("F").setSize(500,300);
为什么就没有错了。。
莫非 new demo("F").setSize(500,300); 这样的时候new demo("F")就属于一个final类型的,所以就可以调用了?

[此贴子已经被作者于2007-6-11 15:43:00编辑过]


----------------解决方案--------------------------------------------------------

编译后,窗口事件提示有一大堆同样的错误,什么illegal character?
把addwindowListener放在构造函数也一样。


----------------解决方案--------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class demo extends JFrame {

public demo(String t) {
super(t);
Container contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
setBackground(Color.white);
contentPane.add(a1);
contentPane.add(a2);
contentPane.add(a3);
contentPane.add(a4);
contentPane.add(a5);
contentPane.add(a6);


a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xu");}
});
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xian");}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("yue");}
});

a5.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
a6.setText("yes");
}
});
this.setSize(500,300);
this.setVisible(true);
}
JButton a1=new JButton("demo1");
JButton a2=new JButton("demo2");
JButton a3=new JButton("demo3");
JLabel a4=new JLabel("demo4");
JLabel a6=new JLabel("demo6");
JCheckBox a5=new JCheckBox("demo5",false);

public static void main(String[] args) {
demo beat =new demo("good");
beat.addWindowListener(new WindowAdapter() {
                      //匿名类用于注册监听器
       public void windowClosing(WindowEvent e) {
         System.exit(0);
       }
     });


}
}

就可以了


----------------解决方案--------------------------------------------------------
呵呵,我弄好了,代码是没错,我的ultraedit出了点问题,整了半天。谢谢各位了。
----------------解决方案--------------------------------------------------------

楼上的这样写好像还是有错吧?反正我编译有错。。

这样可以,
[CODE]
new demo("Good").addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});[/CODE]


这样好像也可以:
[CODE]final demo bean = new demo("FF");

bean.setSize(500,300);
bean.setVisible(true);
bean.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);[/CODE]


----------------解决方案--------------------------------------------------------
但是我编译也有错,难道我的JC也出错了吗?
----------------解决方案--------------------------------------------------------
的确没有什么,已经运行过了。

----------------解决方案--------------------------------------------------------
beat.addWindowListener(new WindowAdapter() {
                      //匿名类用于注册监听器
       public void windowClosing(WindowEvent e) {
         System.exit(0);
       }
     });
有这个我的也运行不了哦。
我在想为什么还要加个这个东西呢 ?用beat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);不就什么问题都解决了哦。 而且方便.

----------------解决方案--------------------------------------------------------