【问题标题】:Label will not update with the flow标签不会随流量更新
【发布时间】:2014-11-26 18:12:13
【问题描述】:

我正在开发这个 RPSGUI;就逻辑而言,一切都很顺利,但大约一周左右前我进入了最后阶段,从那以后我一直被困在这里。我无法更新我的标签。所以说用户赢了,计数器会从 0 变为 1、2 等等。我有很多东西被注释掉了,所以你可以看到我已经尝试过了。我实际上删除了我评论的几个部分,因为我感到困惑,但是是的。我还想让 gui 说出播放的内容,并且不确定从哪里开始:-/。我希望有人可以帮助我解决这个问题。这对我来说意义重大。

这是没有驱动程序类的程序。除了显示之外,驱动程序类中没有任何内容。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class GamePanel extends JPanel{
    private JButton rockButton, paperButton, scissorsButton;
    private JLabel userLabel, computerLabel, resultLabel, winLabel, tieLabel, loseLabel;
    private int winInt, tieInt, loseInt = 0;
    private String rockButtonText = "Rock";
    private String paperButtonText = "Paper";
    private String scissorsButtonText = "Scissors";
    private JPanel buttonPanel, labelPanel, resultPanel;
    public int computerInt;

    String[] attackArray = {"Rock", "Paper", "Scissors"};

    public GamePanel(){
        String[] attackArray = {"Rock", "Paper", "Scissors"};

        setLayout(new  GridLayout(5,3));

        setBackground(Color.white.brighter());
        setForeground(Color.ORANGE.darker());

        //make the buttons
        JButton rockButton = new JButton(rockButtonText);
        JButton paperButton = new JButton(paperButtonText);
        JButton scissorsButton = new JButton(scissorsButtonText);

        //style the buttons
        rockButton.setBackground(Color.orange);
        rockButton.setForeground(Color.black.darker());
        paperButton.setBackground(Color.orange);
        paperButton.setForeground(Color.black.darker());
        scissorsButton.setBackground(Color.orange);
        scissorsButton.setForeground(Color.black.darker());

        //add labels
        JLabel userLabel = new JLabel("User:");
        JLabel computerLabel = new JLabel("Computer:");
        JLabel winLabel = new JLabel("Win: " + winInt);
        JLabel tieLabel = new JLabel("Tie: " + tieInt);
        JLabel loseLabel = new JLabel("Lose:" + loseInt);

        //style Labels
        userLabel.setBackground(Color.orange);
        userLabel.setForeground(Color.black.darker());
        computerLabel.setBackground(Color.orange);
        computerLabel.setForeground(Color.black.darker());      
        winLabel.setBackground(Color.orange);
        winLabel.setForeground(Color.black.darker());       
        tieLabel.setBackground(Color.orange);
        tieLabel.setForeground(Color.black.darker());       
        loseLabel.setBackground(Color.orange);
        loseLabel.setForeground(Color.black.darker());


        //add buttons to panel
        add(rockButton);
        //add(Box.createRigidArea(new Dimension (0,10)));
        add(paperButton);
        //add(Box.createRigidArea(new Dimension (0,10)));
        add(scissorsButton);
        //add(Box.createRigidArea(new Dimension (0,10)));


        add(userLabel);
        //add(Box.createRigidArea(new Dimension (0,10)));
        add(computerLabel);
        //add(Box.createRigidArea(new Dimension (0,10)));


        add(winLabel);
        //add(Box.createRigidArea(new Dimension (0,10)));
        add(tieLabel);
        //add(Box.createRigidArea(new Dimension (0,10)));
        add(loseLabel);
        //add(Box.createRigidArea(new Dimension (0,10)));


        // add action listeners
        rockButton.addActionListener(new ButtonListener());
        paperButton.addActionListener(new ButtonListener());
        scissorsButton.addActionListener(new ButtonListener());


    }
    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ae){
            int computerSourceAI = (int) (Math.random() * 2); //random generator

                     String oppChoice = attackArray[computerSourceAI];  // the choice of your opponent 

                    String yourChoice = ((JButton) ae.getSource()).getText();  // casts source from object to button and gets the text



               int outcome = determineWinner(yourChoice, oppChoice);

               // 0 = tie
               //1 == win
               //2 = lose

            if (outcome == 1) 
        {

        // win message 
        //System.out.println("You win!");
        winLabel.setText("You Won" + winInt++);
        }

    else if (outcome == 0)
     {

       // tie message
       //System.out.println("Tie");
         tieLabel.setText("You tied" + tieInt++);


     }

        else
      {
             loseLabel.setText("You lose" + loseInt++);
     }


        }
    }



 public int determineWinner(String you, String opponent)
{

     if(you.equals("Rock") && (opponent.equals("Rock"))){
         //tieInt++;
         tieLabel.setText("You tied" + tieInt++);
         return tieInt++;
     }else if(you.equals("Rock") && (opponent.equals("Rock"))){
         //tieInt++;
         tieLabel.setText("You tied" + tieInt++);
        return tieInt++;
     }else if(you.equals("Scissors") && (opponent.equals("Scissors"))){
         //tieInt++;
         tieLabel.setText("You tied" + tieInt++);
         return tieInt++;
     }else if(you.equals("Rock") && (opponent.equals("Scissors"))){
         //winInt++;
         winLabel.setText("You Won" + winInt++);
         return winInt++; 
     }else if(you.equals("Paper") && (opponent.equals("Rock"))){
         //winInt++;
         //return 1;
         winLabel.setText("You Won" + winInt++);
         return winInt++;
     }else if(you.equals("Scissors") && (opponent.equals("Paper"))){
         //winInt++;
         //return 1;
         winLabel.setText("You Won" + winInt++);
         return winInt++;
     }else if(you.equals("Scissors") && (opponent.equals("Rock"))){
         //loseInt++;
         //return -1;
         loseLabel.setText("You lose" + loseInt++);
         return loseInt++;
     }else if(you.equals("Rock") && (opponent.equals("Paper"))){
         //loseInt++;
         //return -1;
         loseLabel.setText("You lose" + loseInt++);
         return loseInt++;
     }else 
         if(you.equals("Paper") && (opponent.equals("Scissors"))){
        //loseInt++;
         //return -1;
             loseLabel.setText("You lose" + loseInt++);
             return loseInt++;
     }
    return 0;


}

【问题讨论】:

    标签: java swing user-interface label logic


    【解决方案1】:

    您通过在类中重新声明 tieLabel 变量来隐藏它。通过这样做,构造函数中声明的局部变量被初始化,但类中声明的字段保持为空:

    public class GamePanel extends JPanel {
       private JButton rockButton, paperButton, scissorsButton;
    
       // all these variables are null and remain null
       private JLabel userLabel, computerLabel, resultLabel, winLabel, tieLabel,
             loseLabel;
    
       // ...
    
       public GamePanel() {
    
          // ...
    
          // ***** shadowing vars here
          JButton rockButton = new JButton(rockButtonText);
          JButton paperButton = new JButton(paperButtonText);
          JButton scissorsButton = new JButton(scissorsButtonText);
    
          //...
    
          // *** and here
          JLabel userLabel = new JLabel("User:");
          JLabel computerLabel = new JLabel("Computer:");
          JLabel winLabel = new JLabel("Win: " + winInt);
          JLabel tieLabel = new JLabel("Tie: " + tieInt);
          JLabel loseLabel = new JLabel("Lose:" + loseInt);
    

    为避免这种情况,请不要重新声明。例如,

       public GamePanel() {
    
          // ...
    
          rockButton = new JButton(rockButtonText);
          paperButton = new JButton(paperButtonText);
          scissorsButton = new JButton(scissorsButtonText);
    
          //...
    
          userLabel = new JLabel("User:");
          computerLabel = new JLabel("Computer:");
          winLabel = new JLabel("Win: " + winInt);
          tieLabel = new JLabel("Tie: " + tieInt);
          loseLabel = new JLabel("Lose:" + loseInt);
    

    现在构造函数将初始化类中声明的字段,而不是初始化局部变量,现在字段不会为空。

    【讨论】:

    • 这是有道理的,但它仍然不起作用:-/。这是我在 GamePanel.determineWinner(GamePanel.java:176) 的线程“AWT-EventQueue-0”java.lang.NullPointerException 中得到异常的错误,它继续像这样 javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)在 javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)
    • 等待 nvm 它现在可以工作了。但它并没有显示谁在用户和计算机旁边玩了“你赢了”或“你输了”类型的东西:(。
    • @wwe9112:是时候做一些调试了。要么使用你的 IDE 的调试功能,要么使用一堆 println 语句来确定你的变量在程序运行时处于什么状态,从而找到我们的错误。
    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多