按下JButton时,textField.setText没有工作和eclipse冻结

时间:2017-07-15 22:17:27

标签: java swing user-interface

我遇到了eclipse窗口构建器的问题, 我的代码只是在按下按钮时尝试在文本字段中写入文本,但是每当我按下按钮时都没有打印任何内容而只是冻结。

这是我的代码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class B {

    private JFrame frame;
    private JTextField textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    B window = new B();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public B() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textField = new JTextField();
        frame.getContentPane().add(textField, BorderLayout.NORTH);
        textField.setColumns(10);

        JButton btnCount = new JButton("count");
        btnCount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {    
                textField.setText("Result");    
            }
        });
        frame.getContentPane().add(btnCount, BorderLayout.SOUTH);
    }
}

1 个答案:

答案 0 :(得分:0)

我通过以下方式解决了这个问题:

public void actionPerformed(ActionEvent e) {

            try {
                runCode();




            } catch (ClassNotFoundException | IOException | InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }



        }

        public void runCode() throws ClassNotFoundException, IOException, InterruptedException {
            // TODO Auto-generated method stub


            MyDriver.main(null);



        }