简体   繁体   中英

Text in JLabel doesn't get updated on pressing a JButton

I am working on a project but the program seems to have a bug that I can not find.

Here is an MCVE which reproduces the problem:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.FlowLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SO{
    JLabel label;
    JButton button;
    JPanel panel;
    JFrame frame;

    public static void main(String[] args){
        new SO().start();
    }

    public void start()
    {
        label = new JLabel("Button not pressed");
        button = new JButton("Press me");
        frame = new JFrame();
        panel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        panel.add(label);
        panel.add(button);

        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Button was pressed");
                label = new JLabel("Button is pressed"); //Doesn't work
                frame.repaint();
            }
        });
    }
}

The above program has a JLabel with some text and a JButton both of which are added into a JPanel which in turn is added to a JFrame.

When the button is pressed, I want the text in the JLabel to change. But the text doesn't get changed despite the println executing every time I press the button.

What is the problem here?

You are creating a new object of the JLabel on clicking on the button but not adding it to the JPanel or JFrame after that.

In spite of creating new object ie

label = new JLabel("Button is pressed")

do something like,

label.setText("Button is pressed");

More Info

change

label = new JLabel("Button is pressed");

to

label.setText("Button is pressed");

you don't need to create and assign new lable each time.just change the text

You can change that line to label.setText("Button is pressed"); to make this work.

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.FlowLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SO{
JLabel label;
JButton button;
JPanel panel;
JFrame frame;

public static void main(String[] args){
    new SO().start();
}

public void start()
{
    label = new JLabel("Button not pressed");
    button = new JButton("Press me");
    frame = new JFrame();
    panel = new JPanel(new FlowLayout(FlowLayout.CENTER));

    panel.add(label);
    panel.add(button);

    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Button was pressed");
            label.setText("Button is pressed"); //Doesn't work
            frame.repaint();
        }
    });
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM