0

目前正在从事一个学校项目,我正在用 Java 制作一个幸运轮复制品。我JButton在一个名为 的类中有一个 s面板buttonPanel,还有一个单独的类wheelGUI,它充当程序运行的类。我想要发生的是,当在JButton spinGUI 上按下 时,它使用作为参数的方法将随机值分配String[] wheelStuff给字符串,然后在 GUI 的青色框中显示该随机值。在非技术术语中,当按下按钮旋转时,在青色框中显示一个随机值,作为当前玩家的旋转值。这是该课程的代码spinValuespinWheelJTextField resultsbuttonPanel

    package wheelOfFortune;

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

public class buttonPanels extends JPanel
                        implements ActionListener
{
private JButton spin, solve, buyVowel, guess, reset, end, cont;
Color yungMoney = new Color(0, 180, 100);
private static String[] wheelStuff = new String[]{"Bankrupt", "Lose a Turn", "$5000", "$600", "$500", "$300", "$800", "$550", "$400", "$900", "$350", "$450", "$700"};

public buttonPanels()
{
    setBackground(yungMoney);
    spin = new JButton("Spin!");
    spin.addActionListener(this);
    solve = new JButton("Solve the Puzzle");
    solve.addActionListener(this);
    buyVowel = new JButton("Buy a Vowel");
    buyVowel.addActionListener(this);
    guess = new JButton("Guess a Letter");
    guess.addActionListener(this);
    reset = new JButton("Reset");
    reset.addActionListener(this);
    cont = new JButton("Continue");
    cont.addActionListener(this);

    JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 5, 5));
    buttonPanel.setPreferredSize(new Dimension(300,380));
    buttonPanel.setBackground(yungMoney);
    buttonPanel.add(spin);
    buttonPanel.add(guess);
    buttonPanel.add(buyVowel);
    buttonPanel.add(solve);
    buttonPanel.add(cont);
    buttonPanel.add(reset);

    add(buttonPanel);
}
 public void actionPerformed(ActionEvent e)
 {
     JButton b = (JButton)e.getSource();
     b.addActionListener(this);
     if(b==spin)
     {
         wheelGUI.spinWheel(wheelStuff);
     }
     repaint();
 }

}

这是主类的代码,wheelGUI

package wheelOfFortune;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.Random;

import javax.swing.*;

public class wheelGUI extends JFrame implements ActionListener {
private playerPlate player1, player2, player3;
Color yungMoney = new Color(0, 180, 100);
private String fileName = "M:/wheelOfFortune/src/wheelOfFortune/img/wheel1.png";
private String cat;
private static String spinValue = "";
private static String[] wheelStuff = new String[]{"Bankrupt", "Lose a Turn", "$5000", "$600", "$500", "$300", "$800", "$550", "$400", "$900", "$350", "$450", "$700"};
private static JTextField results;

public wheelGUI() {
    super("Butt Stuff!");

    ImageIcon i = new ImageIcon(fileName);
    JLabel picture = new JLabel(i);

    player1 = new playerPlate("Garrett", Color.RED);
    player2 = new playerPlate("Jonny", Color.YELLOW);
    player3 = new playerPlate("Robert", Color.BLUE);

    buttonPanels buttons = new buttonPanels();
    letterBoard letters = new letterBoard();
    catBox category = new catBox(cat);
    inputField input = new inputField();

    Box wall = Box.createHorizontalBox();
    wall.add(player1);
    wall.add(Box.createHorizontalStrut(5));
    wall.add(player2);
    wall.add(Box.createHorizontalStrut(5));
    wall.add(player3);

    JPanel result = new JPanel();
    result.setBackground(yungMoney);
    JTextField results = new JTextField(spinValue);
    results.setBackground(Color.CYAN);
    results.setHorizontalAlignment(JTextField.CENTER);
    results.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));
    results.setPreferredSize(new Dimension(150,100));
    results.setFont(new Font("Impact", Font.PLAIN, 28));
    results.setEditable(false);
    result.add(results);


    Box catInput = Box.createVerticalBox();
    catInput.add(category);
    catInput.add(Box.createVerticalStrut(50));
    catInput.add(result);
    catInput.add(Box.createVerticalStrut(100));
    catInput.add(input);

    Container c = getContentPane();
    c.setBackground(yungMoney);
    c.add(buttons, BorderLayout.EAST);
    c.add(wall, BorderLayout.SOUTH);
    c.add(letters, BorderLayout.NORTH);
    c.add(picture, BorderLayout.WEST);
    c.add(catInput, BorderLayout.CENTER);
}
public static String spinWheel(String[] wheelStuff)
{
    Random rnd = new Random();
    wheelStuff[rnd.nextInt(wheelStuff.length)] = spinValue;
    return spinValue;
}
public static void main(String[] args) {
    wheelGUI window = new wheelGUI();
    window.setBounds(50, 50, 1024, 768);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setResizable(false);
    window.setVisible(true);
}

public void actionPerformed(ActionEvent e) 
{
    // logic for any additional panels. other logics should be in individual
    // classes.
}

}

谢谢你的帮助!注意:wheelGUI与前面所述的内容无关的所有代码都可以忽略,它来自其他各种类。

4

1 回答 1

0

static不建议以这种方式使用字段。

相反,buttonsPanel应该参考wheelGUI. 这将允许在需要时buttonsPanel调用所需的方法wheelGUI

更好的解决方案是使用可以位于两个 UI 之间的模型并对逻辑进行建模,在模型更改的部分触发事件

您的主要问题是您遮蔽了results文本字段。也就是说,您已经声明了类级别的静态字段,然后在构造函数中重新声明了它

wheelGUI在构造函数中更改以下行

 JTextField results = new JTextField(spinValue);

results = new JTextField(spinValue);

这意味着当您在结果字段上设置文本时,您将设置该字段的正确实例的值。

于 2013-05-23T20:53:46.580 回答