0

所以我试图让 JLabel 在这段代码中工作。我可以让按钮和动作监听器工作,但不能让标签工作。MyDice 是我创建面板和按钮的地方。

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));
    }
}

MyRoll 是我在单击按钮时获得动作监听器的地方。

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);

    }
}

但是,这最后一部分不起作用。任何想法为什么?

4

3 回答 3

3

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

于 2012-09-19T20:27:10.267 回答
2

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:

  1. Create a JFrame.

  2. Create a JPanel.

  3. Add components to the JPanel.

  4. Add the JPanel to the JFrame.

  5. 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.

于 2012-09-19T20:29:00.140 回答
0

如果您正在寻找一种刷新方法,我不知道对于 . 相反,您可以设置一个线程,根据您提供的逻辑更新输出。

于 2012-09-19T20:33:45.860 回答