【问题标题】:Java Displaying String on a JTextField after a JButton in a separate class is pressed按下单独类中的 JButton 后,Java 在 JTextField 上显示字符串
【发布时间】:2013-05-23 20:39:30
【问题描述】:

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

    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 中所有与前面提到的东西无关的代码都可以忽略,它来自其他各种类。

【问题讨论】:

    标签: java jpanel jbutton jtextfield arrays


    【解决方案1】:

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

    相反,buttonsPanel 应该引用wheelGUI。这将允许buttonsPanel 在需要时调用wheelGUI 上所需的方法。

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

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

    wheelGUI 构造函数中更改以下行

     JTextField results = new JTextField(spinValue);
    

    results = new JTextField(spinValue);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-24
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      相关资源
      最近更新 更多