【问题标题】:JFrame variable updatingJFrame 变量更新
【发布时间】:2023-01-11 00:38:06
【问题描述】:

我想为学校做一个小彩票计划。您从 500 学分开始,但每次您输掉时,都会扣除 50 学分。我的问题是你总是从 500 学分开始。

package jframe;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class lotto {
    public static void main(String[]args) {
        
        Random randI = new Random();
        

        
        
        
        JFrame frame = new JFrame("Lotto");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        
        JTextField num1Field = new JTextField();
        num1Field.setBounds(80, 10, 100, 30);
        frame.add(num1Field);
        
        JTextField num2Field = new JTextField();
        num2Field.setBounds(80, 50, 100, 30);
        frame.add(num2Field);
        
        JTextField num3Field = new JTextField();
        num3Field.setBounds(80, 90, 100, 30);
        frame.add(num3Field);
        
        
        JLabel num1Label = new JLabel("Zahl 1: ");
        num1Label.setBounds(20, 10, 50, 30);
        frame.add(num1Label);

        JLabel num2Label = new JLabel("Zahl 2: ");
        num2Label.setBounds(20, 50, 50, 30);
        frame.add(num2Label);
        
        JLabel num3Label = new JLabel("Zahl 3: ");
        num3Label.setBounds(20, 90, 50, 30);
        frame.add(num3Label);
        
        JButton startButton = new JButton("Start!");
        startButton.setBounds(30, 150, 80, 30);
        frame.add(startButton);
        
        JButton resetButton = new JButton("Reset");
        resetButton.setBounds(120, 150, 80, 30);
        frame.add(resetButton);
        
        JLabel ergLabel = new JLabel();
        ergLabel.setBounds(10, 200, 400, 30);
        frame.add(ergLabel);
        
        JLabel ghLabel = new JLabel("500");
        ghLabel.setBounds(50, 230, 200, 30);
        frame.add(ghLabel);
        
        
        
        
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              
              int num1 = Integer.parseInt(num1Field.getText());
              int num2 = Integer.parseInt(num2Field.getText());
              int num3 = Integer.parseInt(num2Field.getText());
              

              
              int credit = 500;
              
           
              System.out.println(credit);
              
              
              
              int pcnum1 = randI.nextInt(48);
              pcnum1 = pcnum1+1;
              
              int pcnum2 = randI.nextInt(48);
              pcnum2 = pcnum2+1;
              
              int pcnum3 = randI.nextInt(48);
              pcnum3 = pcnum3+1;
              
              boolean zahl1 = false;
              boolean zahl2 = false;
              boolean zahl3 = false;
              
              if(num1 == pcnum1) {
                  zahl1 = true;
              } else {
                  zahl1 = false;
              }
              
              if(num2 == pcnum2) {
                  zahl2 = true;
              } else {
                  zahl2 = false;
              }
              
              if(num3 == pcnum3) {
                  zahl3 = true;
              } else {
                  zahl3 = false;
              }
              
              if(zahl1 == true && zahl2 == true && zahl3 == true) {
                  credit = credit + 500;
              }
              
              if(zahl1 == true && zahl2 == true && zahl3 == false || zahl1 == true && zahl3 == true && -                 zahl2 == false || zahl2 == true && zahl3 == true && zahl1 == false) {
                  credit = credit + 250;
              }
              
              if(zahl1 == true && zahl2 == false && zahl3 == false || zahl1 == false && zahl3 == false -                 && zahl2 == true || zahl1 == false && zahl2 == false && zahl3 == true) {
                  credit = credit + 100;
              }
              
              if(zahl1 == false && zahl2 == false && zahl3 == false){
                  credit = credit - 50;
              }
              
              ergLabel.setText("1. Number: " + zahl1 + "  2. Number: " + zahl2 + "  3. Number: " +          -                 zahl3);
              ghLabel.setText("Credit: " + credit);
              
            }
          });
        
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
                num1Field.setText("");
                num2Field.setText("");
                num3Field.setText("");
                ergLabel.setText("");
            }
          });
        
        

        
        
        
        
        
        frame.setVisible(true);
        
    }
}

我唯一的解决办法是在函数外声明变量,但如果我在函数外声明变量,我不知道如何在函数内使用它。

【问题讨论】:

  • 将其移至班级并使其成为static

标签: java function variables jframe


【解决方案1】:
  1. 移动积分信用 = 500;成为instance variable
  2. 将所有代码移入主要的进入default constructor 为你的班级乐透
  3. 在您的 main 方法中创建一个 Lotto 实例

    此外,将类名大写是惯例,所以我在这里更改了它。

    public class Lotto {
        
        int credit = 500;
    
        public Lotto() 
        {
            Random randI = new Random();
            
            JFrame frame = new JFrame("Lotto");
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(null);
            // ...
        }
        
        public static void main(String[]args) {
            Lotto lotto = new Lotto();
        }
    
    

    或者,如果您想将代码保留在 main 中并且不创建任何实例,请移动积分信用 = 500;退出方法并使其静态。

    public class Lotto {
        static int credit = 500;
    

    如果将类中的变量设为静态,则无需实例即可使用它。
    如果你在一个类中使变量不是静态的,那么你必须有一个实例乐透lotto = new Lotto();访问它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-12
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    相关资源
    最近更新 更多