| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:程序代码错在哪?
只看楼主 加入收藏
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:8 
程序代码错在哪?
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);

}
}
搜索更多相关主题的帖子: 代码 
2007-06-11 14:54
起风的时候
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-1-16
收藏
得分:0 
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编辑过]


2007-06-11 15:41
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
得分:0 

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

2007-06-11 15:48
食恶不色
Rank: 2
等 级:新手上路
威 望:3
帖 子:632
专家分:5
注 册:2006-11-8
收藏
得分: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");
}
});
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);
       }
     });


}
}

就可以了


做人要厚道,看帖要回帖!回帖是尊重,回帖是美德! 美德要发扬,我们要顶帖!顶帖是好事,千万莫灌水!
2007-06-11 16:20
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
得分:0 
呵呵,我弄好了,代码是没错,我的ultraedit出了点问题,整了半天。谢谢各位了。
2007-06-11 16:55
起风的时候
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-1-16
收藏
得分:0 

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

这样可以,
[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]


2007-06-11 17:05
起风的时候
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-1-16
收藏
得分:0 
但是我编译也有错,难道我的JC也出错了吗?

2007-06-11 17:06
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
得分:0 
的确没有什么,已经运行过了。
2007-06-11 19:44
cos100
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2007-5-30
收藏
得分:0 
beat.addWindowListener(new WindowAdapter() {
                      //匿名类用于注册监听器
       public void windowClosing(WindowEvent e) {
         System.exit(0);
       }
     });
有这个我的也运行不了哦。
我在想为什么还要加个这个东西呢 ?用beat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);不就什么问题都解决了哦。 而且方便.
2007-06-11 22:30
快速回复:程序代码错在哪?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015431 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved