-2

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();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You never modify the label's value – MadProgrammer Oct 29 '19 at 22:41
  • @MadProgrammer I added the "JLabel();" that I thought was supposed to update it. –  Oct 29 '19 at 22:44
  • not related to your issue, but you should change the name of your `JLabel`method. It doesn't comply with [java naming convention](https://www.oracle.com/technetwork/java/codeconventions-135099.html) and it's misleading as it's also the name of a class – jhamon Oct 29 '19 at 22:48
  • @jhamon Oh yeah definitely, this was more for me and I didn't notice that when I posted. Sorry about that, I'll change it. –  Oct 29 '19 at 22:49
  • You don't call `PanelConfig()`. Missing part in the code? [java naming convention](https://www.oracle.com/technetwork/java/codeconventions-135099.html), [Why is it frowned upon to use a null layout in Swing?](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – jhamon Oct 29 '19 at 22:54
  • @jhamon Yes it is missing. I will add it right now. Thanks :) –  Oct 29 '19 at 23:03

2 Answers2

3

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);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I call the JPanel in a main class. ```java public class Main { public static void main(String args[]) { Franels windows = new Franels(); windows.FrameConfig(900, 900); windows.PanelConfig(); windows.LabelConfig(); } } ``` For the other information. I changed it right after and sorry about that. I will definitely take the time to learn Layout Manager API; I just put in null because a website had that. –  Oct 29 '19 at 23:01
  • 1
    Well congratulations on not providing enough information for people to diagnose your issue :/ – MadProgrammer Oct 29 '19 at 23:06
  • Sarcasm will save my sanity, providing all the information need to correctly diagnose your issue and stop us from wildly guessing at it, will save you ;) – MadProgrammer Oct 29 '19 at 23:14
  • I took people's advice and information, and applied it. I can make mistakes, but if I take the time to correct them, and then you start talking about not having enough information.. I'm not sure what's left for me to deduce. I'm not forcing anyone to help me or make them constantly check for what I update, but if they decide that they would like to help and not attempt to derive updated information the OP may give, I'd rather you just not "help" at all. Regarding your updated version of the comment and explanation, thanks, I'll try it out now. –  Oct 29 '19 at 23:23
  • Regarding the update, it works perfectly now! I'm a bit confused about the new instance of Franels and current instance; do you know where this new instance is located? Either way, thanks. –  Oct 29 '19 at 23:27
  • In order for use to help you, you need to help us first, no offence. But a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) is a pretty commonly request element to most questions. It's a fine balance between to much and to little, but telling me you're "already doing something" which isn't provided in the original question is ... well frustrating and waste of both our times – MadProgrammer Oct 29 '19 at 23:28
  • *"do you know where this new instance is located"* - In memory, so, that's a little vague. The reference to the instance will stored in `contentPane`'s event listeners, which is why the new instance is receiving the mouse events – MadProgrammer Oct 29 '19 at 23:29
  • It should have been in the original question. I mean it does say I updated a little bit before your the congrat reply. I believe your focus isn't the updating but rather the original post which I believe is totally acceptable as you should try to make the original the most user friendly, but then again, there are always cases where someone will leave something out. I don't want to leave this on bad terms but rather something positive, and I'll definitely try to make my later posts more legible for those who try to help from the beginning. –  Oct 29 '19 at 23:33
  • Glad we got to the bottom of it ;) – MadProgrammer Oct 29 '19 at 23:35
  • Okay, I will definitely have to learn much more about contentPane and other swing related things. By seeing my ignorance in this subject it would probably be better fit for me to sit down and take in some more tutorials and lessons regarding swing. –  Oct 29 '19 at 23:35
0

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);
    }
}
Edd
  • 136
  • 9
  • I tried this before and the 0 inside of the Panel was just gone. –  Oct 29 '19 at 22:43
  • Thanks, I'll take note of your edit and try to use it and understand each bit. –  Oct 29 '19 at 23:11