Reputation: 761
So I'm trying to make the JLabel work in this code. I can get the button and Action Listener to work but not the Label. MyDice is where I create the Panels and buttons.
public class MyDice
{
public static void main(String[] args)
{
JFrame frame = new JFrame("MyDice v1.0");
frame.setSize(800,600);
frame.setLocation(560, 240);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(new Color(200,200,200));
panel.setSize(800,600);
frame.add(panel);
panel.setVisible(true);
JButton button_d4 = new JButton("Roll d4");
panel.add(button_d4);
button_d4.addActionListener (new MyRoll(4,panel));
}
}
And the MyRoll is where I got the Action Listener that does something when I click the buttons.
public class MyRoll implements ActionListener
{
int dice;
JPanel panel;
public MyRoll (int dice, JPanel panel)
{
this.dice = dice;
this.panel = panel;
}
public void actionPerformed(ActionEvent e)
{
rollDice(dice,1);
}
public void rollDice (int dice, int times)
{
int r=0;
for (int i=0; i<times;i++)
{
double rand = Math.random();
rand = rand*dice + 1;
r = (int) rand;
}
System.out.println("You rolled "+r+" out of "+dice);
JLabel output = new JLabel();
output.setText("You rolled "+r+" out of "+dice);
panel.add(output);
}
}
However, this last part does not work. Any ideas why?
Upvotes: 0
Views: 7329
Reputation: 83527
Look at the following code:
JLabel output = new JLabel();
output.setText("You rolled "+r+" out of "+dice);
panel.add(output);
Here you are adding a new JLabel
to the panel (which happens to be in your main frame) every time you click the button. However, you there is nothing to tell it to repaint itself.
My suggestion to fix this is to create the JLabel
and add it to the JPanel
in main()
and pass the JLabel reference to your MyRoll
class. Then you can simply call setText()
any time you wish. You really don't need a reference to the JPanel
in MyRoll
; you just need a reference to the JLabel
.
p.s. I want to make a few comments on your main()
method. In particular, if you organize your code differently, you can simplify it a little bit. Typically creating a Swing GUI follows these steps:
Create a JFrame
.
Create a JPanel
.
Add components to the JPanel
.
Add the JPanel
to the JFrame
.
Set the JFrame
visible.
Notice that you only need to call setVisible()
on the JFrame
, if you do it very last. This will automatically call setVisible()
on the JPanel
and all its subcomponents.
Upvotes: 2
Reputation: 9331
Use a JLabel and set the Label Message in your logic
REVISION for the updated CODE POSTED
You should set up the Label outside of the Logic, added to the Panel with initial empty value and then on your logic you change the Label's Value
i.e
//Setting up your GUI
JLabel label = new JLabel("");
panel.add(label);
//Within your Logic method
System.out.println("Printing info");
label.setLabel("Printing info");
That way you are not constantly adding a JLabel to your Panel
Upvotes: 3
Reputation: 31
If you are looking for a refresh method, there is none that I know of for . Instead you can set up a thread, that updates the output depending on the logic you provide.
Upvotes: 0