Skip to content
Advertisement

How can I solve Java JLabel issues in application? [closed]

I am quite new to Java. I decided to code tic-tac-toe as practice (from scratch).

Anyway, I am trying to have the ‘JLabel label’ change when I click; going from 1, then 2, etc. Eventually, it will change to something other than numbers. But, for now, it works for a test.

The ‘System.out.println(“Number of clicks: ” + mouseIn);’ works fine, and produces the output I want. A picture of the console and JFrame/JPanel/JLabel can be seen here:

image

(The little 1 in the JFrame is the JLabel. I want to match what is output in the console.

I have googled, and searched, and tried everything I know (which ain’t much!) and I can’t get the dang thing to work…I’m just asking for some guidance. Main method just includes building of JFrame/Panel.

Code below:

From the main class, called (namone.java [named it this for my own reasons]):

JavaScript

And then the code from my MouseEvents.java class:

JavaScript

As I said. I am very new to Java and I’m trying to learn how to develop applications with it. I’m sure it’s caused by my own ignorance.

Thanks.

Advertisement

Answer

Assuming that mouse is of type MouseEvents that you write, one possibility is that you need to pass mouse.getText() to your call to label.setText(.).

Regardless, the way you set up your game is a bit strange to me. What is the reason to create a brand new JPanel every time someone clicks? Why not maintain the original JPanel and update it instead. I personally would attach a custom ActionListener to each JButton that runs some code everytime the button is clicked. If this ActionListener is an inner class, it can also view variables in the scope that the JButton is defined.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement