黑马程序员技术交流社区
标题:
关于Swing的代码问题
[打印本页]
作者:
╰青青子佩ˊゝ
时间:
2014-4-7 13:21
标题:
关于Swing的代码问题
关于Swing的代码,求大神帮忙看下代码,不知道那么怎么就会报错,如下面代码所示。
class EventTestDemo extends JFrame implements ActionListener {
private EventTestDemo jf = new EventTestDemo() ; //在这里报错,搞不清楚怎么回事。
private JTextField jTextField ;
private JLabel showLabel ;
private JLabel jLabel ;
private Container con ;
EventTestDemo(){
init();
}
public void init(){
jf.setBounds(300, 200, 500, 300) ;
con = jf.getContentPane();
con.setLayout(new FlowLayout()) ;
jLabel = new JLabel("输入数值:");
con.add(jLabel);
jTextField = new JTextField(10);
jTextField.addActionListener(jf) ;
con.add(jTextField);
showLabel = new JLabel() ;
showLabel.setBackground(Color.green);
con.add(showLabel);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e){
showLabel.setText("得到数值为:"+jTextField.getText()) ;
}
public static void main(String[] args) {
new EventTestDemo();
}
}
复制代码
作者:
osully
时间:
2014-4-7 18:33
你这代码 老实说 我觉得有很严重的问题
改成如下 可能会达到你的要求把
public class EventTestDemo extends JFrame implements ActionListener {
private static EventTestDemo jf=null;
private JTextField jTextField;
private JLabel showLabel;
private JLabel jLabel;
private Container con;
static EventTestDemo getInstance(){
return jf= new EventTestDemo();
}
public void init() {
jf.setBounds(300, 200, 500, 300);
con = jf.getContentPane();
con.setLayout(new FlowLayout());
jLabel = new JLabel("输入数值:");
con.add(jLabel);
jTextField = new JTextField(10);
jTextField.addActionListener(jf);
con.add(jTextField);
showLabel = new JLabel();
showLabel.setBackground(Color.green);
con.add(showLabel);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
showLabel.setText("得到数值为:" + jTextField.getText());
}
public static void main(String[] args) {
EventTestDemo et = EventTestDemo.getInstance();
et.init();
}
}
复制代码
作者:
╰青青子佩ˊゝ
时间:
2014-4-7 21:26
osully 发表于 2014-4-7 18:33
你这代码 老实说 我觉得有很严重的问题
改成如下 可能会达到你的要求把
谢谢,这个可以。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2