try和catch语句中的代码没有执行,什么错了?

时间:2013-03-08 01:59:28

标签: exception jframe try-catch

我有以下简单示例;

public class TES extends JFrame {

private JPanel contentPane;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TES frame = new TES();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public TES() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 100, 100);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    textField = new JTextField();
    textField.setColumns(10);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


            try {
                System.out.println("Before ");
                textField.setText("Before");
                Thread.sleep(4000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textField.setText("After");
        }
    });



    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.TRAILING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNewButton)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                .addComponent(btnNewButton)
                .addContainerGap())
    );
    contentPane.setLayout(gl_contentPane);


}

}

未执行textField.settext(“Before”)行。

            try {
                System.out.println("Before ");
                textField.setText("Before");
                Thread.sleep(4000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textField.setText("After");
        }

虽然执行了System.out.println(“Before”)行。我输入thread.sleep(4000)只是暂停,所以我可以看到JtextField中的“Before”和“After”文本。在我原来的代码中,我在那个地方运行另一个类;

                                    try {
                System.out.println("Before ");
                textField.setText("Before");
                Functions.dbSet();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textField.setText("After");
        }

所以主要问题是textField.setText(“Before”);行没有被执行。

1 个答案:

答案 0 :(得分:0)

看起来你需要了解Java"事件发送线程"。

见这里:Java Event-Dispatching Thread explanation

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html