[英]JFrame variable updating
我想為學校做一個小彩票計划。 您從 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);
}
}
我唯一的解決方案是在 function 之外聲明變量,但是如果我在外面聲明它,我不知道如何使用 function 內部的變量。
此外,將 class 名稱大寫是慣例,所以我在這里更改了它。
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 中並且不創建任何實例,請移動int credit = 500; 退出方法並使其成為static。
public class Lotto {
static int credit = 500;
如果你在 class static 中創建變量,那么你不必有一個實例來使用它。
如果你在 class 而不是 static 中創建變量,那么你必須有一個實例Lotto lotto = new Lotto(); 訪問它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.