简体   繁体   中英

Why isn't my JLabel updating on each click?

Basically, I'm trying to make it so every time you click somewhere in the JFrame, the JLabel gets added by 1. The problem is, if I were to print out the value of amount, it increases, but the actual JLabel continues to stay the same.

To try and fix this I tried to update the JLabel object every time you click but that didn't seem to work either. (Did this by writing "JLabel();") Other than that, I really can't think of what it might be,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Franels extends MouseAdapter {
    int width, height;
    int amount = 0;

    JFrame frame = new JFrame("Title");
    JPanel panel = new JPanel();
    JButton button = new JButton("Hello");
    JLabel label = new JLabel();

    public void FrameConfig(int width, int height) {
        this.width = width;
        this.height = height;
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLayout(null);
        frame.setResizable(false);

        frame.add(panel);

        frame.getContentPane().addMouseListener(new Franels());
    }

    public void PanelConfig() {
        panel.setBounds(300, 300, 300, 300);
        panel.setBackground(Color.blue);
        panel.setVisible(true);

        panel.add(label);        
    }

    public void LabelConfig() {
        label.setForeground(Color.red);
        label.setText(String.valueOf(amount));
    }

    public void mouseClicked(MouseEvent e) {
        amount++;
        LabelConfig();
        System.out.println(amount);
    }
}
import javax.swing.*;
import java.awt.*;

public class Main {


    public static void main(String args[]) {
        Franels windows = new Franels();
        windows.FrameConfig(900, 900);
        windows.PanelConfig();
        windows.LabelConfig();
    }
}

So, your "basic" problem is, JPanel() is never called, so the label is never added to the UI.

public class Franels extends MouseAdapter {

    //...
    JPanel panel = new JPanel();

    // Just want to point out that these method names
    // are poorly chosen
    public void JFrame(int width, int height) {
        //...
        // Thi sis going to give you no end of issues
        frame.setLayout(null);
        //...
        frame.add(panel);
        //...
    }

    // This is never called
    public void JPanel() {
        panel.setBounds(300, 300, 300, 300);
        panel.setBackground(Color.blue);
        panel.setVisible(true);

        panel.add(label);        
    }


    //...
}

Your larger problem a many.

  1. I'd avoid using method to directly initialise the components. Instead, they should create their own instance of the object and return it. If needed, you would then assign them to other variables, but the methods them selves should be self contained
  2. You method naming is... difficult to read. Consider making use of getXxx instead (ie, getPanel , getLabel ) or something more meaningful
  3. null layouts are just a complete pain in the core. You'd be better off taking the time to learn how to use the layout manager API. See Laying Out Components Within a Container for more details

Updated...

Okay, after some more digging. The instance of label been updated is not the same as the instance of the label on the screen. This took some more digging, but the issue comes down to:

frame.getContentPane().addMouseListener(new Franels());

You're creating a new instance of Franels , which is receiving the mouse events and updating its instance of label , which is not the same as the one of the screen.

Instead, use

frame.getContentPane().addMouseListener(this);

Update the JLabel text after you change the amount, it does not do it automatically.

 public void mouseClicked(MouseEvent e) {
      amount++;
      label.setText(String.valueOf(amount));
 }

EDIT: As you said the above solution didn't work for you I have written a small program to emulate what you're trying to do.

public class Panel extends JPanel implements MouseListener
{
    JLabel label = new JLabel();
    int amount = 0;

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(400,400);
        frame.setContentPane(new Panel());
        frame.setVisible(true);
    }

    public Panel()
    {
        addMouseListener(this);
        label.setText(""+amount);
        add(label);
    }

    public void mouseClicked(MouseEvent arg0)
    {
        amount++;
        label.setText(""+amount);
        System.out.println(amount);
    }
}

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