我正在尝试使用Jbutton动作侦听器单击以将我的按下变量从0设置为1,2或3.如果此数字变为切换,则将卡套件文本字段。我的问题是不是我的设置按变量为一个新的号码,只是将其保持在0如何在java中使用Jbutton中的if语句
我的主文件
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cardapp;
/**
*
* @author Angela
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardApp extends JFrame implements ActionListener {
private JButton oneButton,
twoButton,
threeButton;
private int width = 25;
private int height = 15;
private JTextField TextField = new JTextField(3);
Hand handObject = new Hand();
private int pressedNumber;
public CardApp() {
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
oneButton = new JButton("1");
twoButton = new JButton("2");
threeButton = new JButton("3");
// Listen for events on each button
oneButton.addActionListener(this);
twoButton.addActionListener(this);
threeButton.addActionListener(this);
// Add each to the panel of buttons
buttonPanel.add(oneButton);
buttonPanel.add(twoButton);
buttonPanel.add(threeButton);
// Add everything to a main panel attached to the content pane
JPanel mainPanel = new JPanel(new BorderLayout());
getContentPane().add(mainPanel);
mainPanel.add(TextField, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
setTitle("Sabacc Example by Angela Rucci");
setSize(375, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
handObject.Discards(pressedNumber);
TextField.setText(handObject.ListOfCards());
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == oneButton){
pressedNumber = 1;
handObject.Discards(pressedNumber);
}
if (e.getSource() == twoButton){
pressedNumber = 2;
handObject.Discards(pressedNumber);
}
if (e.getSource() == threeButton){
pressedNumber = 3;
handObject.Discards(pressedNumber);
}
TextField.setText(handObject.ListOfCards());
}
public static void main(String[] args) {
CardApp c = new CardApp();
}
}
我的文件中,这样会产生按下的按钮了一套新衣服。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cardapp;
import java.util.Random;
//import javax.swing.JButton;
import javax.swing.JOptionPane;
//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.*;
/**
*
* @author Angela
*/
public class Hand {
String [] Suits = {"C", "H", "S", "D"};
String [] probability = {"C","H","R","S","D"};
Random randomInt = new Random();
String RandomSuit;
String RandomShuffle;
String ThreeSuits;
String LeftSuit;
String MiddleSuit;
String RightSuit;
int pressed;
public int Discards(int pressedNumber){
Randomizer();
return pressed;
}
public void Randomizer(){
int RandomSuitNumber = randomInt.nextInt(4);//this is generator a random number
//------------------Decide what hand to randomize --------------------------//
if (pressed==1){
LeftSuit= Suits[RandomSuitNumber];
}
if (pressed==2){
MiddleSuit=Suits[RandomSuitNumber];
}
if (pressed==3){
RightSuit=Suits[RandomSuitNumber];}
//----------------20% chance of new random set------------------------------------//
int ProabilityRandomNum = randomInt.nextInt(5);
RandomShuffle= probability[ProabilityRandomNum];
//------------If proability array equals R then change all of the suits----------//
if (RandomShuffle.equals("R") || pressed== 0){
JOptionPane.showMessageDialog(null, "Randomized Hand!");
int leftNumber = randomInt.nextInt(4);
int middleNumber = randomInt.nextInt(4);
int rightNumber = randomInt.nextInt(4);
LeftSuit= Suits[leftNumber];
MiddleSuit= Suits[middleNumber];
RightSuit= Suits[rightNumber];}
ThreeSuits = (LeftSuit + MiddleSuit + RightSuit);
}
public String ListOfCards(){
return ThreeSuits;
}
public void GameOver(){
if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit && RightSuit== LeftSuit){
JOptionPane.showMessageDialog(null, "WINNER!!");
}
}
}
现在无可否认,我的睡眠很少,所以我很容易混淆,但我真的不知道你的问题是什么。你问'pressed数字'的值是否大于点击按钮的值,它不应该改变?你说的是把它保持为'0',但是如果你只是想把它保持在'0',为什么你会改变这个值呢? – MadProgrammer
不,我不想让它保持为零,我希望在单击其中一个按钮时更改该值。 – kkkkkkkkkkkkkkkkkkkkkkk
您将'pressed数字'传递给'Hand#Discards',但从未真正使用它? – MadProgrammer