如何在JFrame的windowOpened事件中设置标签的值?

时间:2013-10-02 10:48:43

标签: java swing jframe jlabel windowlistener

我有一个JLabel进入JFrame,我希望在jFrame的windowOpened事件中设置标签为' Hello Java'但我得到以下错误:

Syntax error on token(s), misplaced construct(s)

代码:

public p5() {
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent arg0) {

            lblA.setText("Hello Java"); // Error is here
        }
    });
    final JLabel lblA = new JLabel("New label");
    lblA.setBounds(91, 68, 46, 14);
    contentPane.add(lblA);

1 个答案:

答案 0 :(得分:0)

放置

final JLabel lblA = new JLabel("New label");

addWindowListener(new WindowAdapter() {
    @Override
    public void windowOpened(WindowEvent arg0) {

        lblA.setText("Hello Java"); // Error is here
    }
});

不在它之后,你的语法错误就会消失。