Domanda

I have a JLabel into JFrame, I want in windowOpened event of JFrame set label to 'Hello Java' but i get following error :

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

code:

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);
È stato utile?

Soluzione

Place

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

before

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

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

not after it and your syntax error should go away.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top