A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ╰青青子佩ˊゝ 中级黑马   /  2014-4-7 13:21  /  1047 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

关于Swing的代码,求大神帮忙看下代码,不知道那么怎么就会报错,如下面代码所示。
  1. class EventTestDemo extends JFrame implements ActionListener {
  2.         private EventTestDemo jf = new EventTestDemo() ; //在这里报错,搞不清楚怎么回事。
  3.         private JTextField jTextField ;
  4.         private JLabel showLabel ;
  5.         private JLabel jLabel ;
  6.         private Container con ;
  7.         EventTestDemo(){
  8.                 init();
  9.         }
  10.         public void init(){
  11.                 jf.setBounds(300, 200, 500, 300) ;
  12.                 con = jf.getContentPane();
  13.                 con.setLayout(new FlowLayout()) ;
  14.                 jLabel = new JLabel("输入数值:");
  15.                 con.add(jLabel);
  16.                 jTextField = new JTextField(10);
  17.                 jTextField.addActionListener(jf) ;
  18.                 con.add(jTextField);
  19.                 showLabel = new JLabel() ;
  20.                 showLabel.setBackground(Color.green);
  21.                 con.add(showLabel);
  22.                
  23.                 jf.setVisible(true);
  24.         }
  25.         public void actionPerformed(ActionEvent e){
  26.                 showLabel.setText("得到数值为:"+jTextField.getText()) ;
  27.         }
  28.         public static void main(String[] args) {
  29.                 new EventTestDemo();
  30.         }
  31. }
复制代码

2 个回复

倒序浏览
你这代码 老实说 我觉得有很严重的问题

改成如下 可能会达到你的要求把
  1. public class EventTestDemo extends JFrame implements ActionListener {
  2.         private static EventTestDemo jf=null;
  3.         private JTextField jTextField;
  4.         private JLabel showLabel;
  5.         private JLabel jLabel;
  6.         private Container con;
  7.        
  8.         static EventTestDemo getInstance(){
  9.                 return jf= new EventTestDemo();
  10.         }

  11.         public void init() {
  12.                 jf.setBounds(300, 200, 500, 300);
  13.                 con = jf.getContentPane();
  14.                 con.setLayout(new FlowLayout());
  15.                 jLabel = new JLabel("输入数值:");
  16.                 con.add(jLabel);
  17.                 jTextField = new JTextField(10);
  18.                 jTextField.addActionListener(jf);
  19.                 con.add(jTextField);
  20.                 showLabel = new JLabel();
  21.                 showLabel.setBackground(Color.green);
  22.                 con.add(showLabel);

  23.                 jf.setVisible(true);
  24.         }

  25.         public void actionPerformed(ActionEvent e) {
  26.                 showLabel.setText("得到数值为:" + jTextField.getText());
  27.         }

  28.         public static void main(String[] args) {
  29.                 EventTestDemo et = EventTestDemo.getInstance();
  30.                 et.init();
  31.         }
  32. }
复制代码
回复 使用道具 举报
osully 发表于 2014-4-7 18:33
你这代码 老实说 我觉得有很严重的问题

改成如下 可能会达到你的要求把

谢谢,这个可以。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马