gpt4 book ai didi

java - 我是 Java Swing 的新手,一般来说,我今天编写的代码有一个涉及 button.doClick() 方法的奇怪故障。

转载 作者:行者123 更新时间:2023-12-01 11:28:51 25 4
gpt4 key购买 nike

如果有人可以帮我检查我的代码,给我一些指示等,我将非常感激!因此,在这款游戏中,如果您从未玩过 Mastermind,基本上三种颜色会按特定顺序隐藏,用户必须从四种可能的颜色中猜测它们是什么。该代码似乎工作得相当好,尽管它目前尚未完成,但是一旦用户获胜,就会出现奇怪的故障。当玩家正确猜出三种隐藏颜色时,系统会提示他们按“再次玩”按钮,然后应该重置游戏,看起来确实如此,但是一旦用户在指定的 JTextFields 中输入另一种颜色因此,最后在这些字段中输入的单词就会显示出来。我已经实现了一条小线,在附近的 JLabel 上显示“工作”一词,以表明 doClick() 方法正在激活,但重置按钮无法按预期工作,尽管 newGame 按钮可以。这是一个奇怪的故障,如果有人对此故障有解决办法或原因或任何其他一般性改进,那么您将让我高兴!

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Summative2 extends JFrame {
static int guessNum;
static String colourGuess[] = new String[3];
static String colourArgument;
static int[] solution = new int[3];
public Summative2() {
for (int x=0;x<3;x++)
{
solution[x] = solution(); //Setting the solution for the first game
}
displayIntro(); //Activating the intro screen to start the program

}
public void displayIntro()
{
JPanel introPanel = new JPanel(new GridBagLayout());
this.getContentPane().add(introPanel);

GridBagConstraints c = new GridBagConstraints(); //The variable name being defined for the layout's constraints
introPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); //Setting the orientation of the Grid layout

c.insets = new Insets(2,2,2,2); //Basic spacing and anchoring
c.anchor = GridBagConstraints.CENTER;
JLabel welcome = new JLabel("Welcome to MASTERMIND");
c.ipady = 40;
c.gridx = 1; //Large welcome label, introducing the game
c.gridy = 0;
c.gridwidth = 3;
introPanel.add(welcome, c);
c.ipady = 0; //Resetting the characteristics of the components back to normal after the large intro message
c.gridwidth = 1;

JButton start = new JButton ("Start");
c.gridx = 1; //The start button
c.gridy = 1;
introPanel.add(start, c);

JLabel space = new JLabel(" ");
c.gridx = 2;
c.ipadx = 10; //Space between the Start and Rules buttons
c.gridy = 1;
introPanel.add(space, c);
c.ipadx= 0;

JButton rulesButton = new JButton("Rules");
c.gridx = 3; //Rules button
c.gridy = 1;
introPanel.add(rulesButton, c);

rulesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
introPanel.setVisible(false);
displayRules("intro"); //Makes the intro panel invisible and activates the rules panel, along with an argument that tells the displayRules method to return the user to the intro screen when they press back after reading the rules
}

});
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
introPanel.setVisible(false); //Makes the intro invisible and starts the main game.
displayGame(false);
}

});
}

public void displayRules(String previousScreen) {
JPanel rulesPanel = new JPanel(new GridBagLayout());
GridBagConstraints r = new GridBagConstraints();
rulesPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.getContentPane().add(rulesPanel);

JTextField rulesText = new JTextField("insert rules here");
rulesText.setEditable(false);
r.gridx = 0;
r.gridy = 0;
r.gridwidth = 3;
r.ipady=100; //Big rules text box` NOT YET FINISHED
r.ipadx = 100;
rulesPanel.add(rulesText, r);
r.gridwidth =1;
r.ipadx=1;
r.ipady=1;

JButton backFromRules = new JButton("Back");
r.gridx = 2; //back button
r.gridy=1;
rulesPanel.add(backFromRules, r);

backFromRules.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rulesPanel.setVisible(false);
if (previousScreen.compareTo("intro")==0) //When the user presses the back button they are returned to the screen from which they activated the rules screen
{
displayIntro();
}
if (previousScreen.compareTo("game")==0)
displayGame(false);
}

});
}
public void displayGame(boolean restart)
{
JPanel gamePanel = new JPanel(new GridBagLayout());
GridBagConstraints g = new GridBagConstraints();
gamePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.getContentPane().add(gamePanel);

g.anchor=GridBagConstraints.WEST;

g.weightx =1;

int coloursY = 0;
JButton redButton = new JButton("Red");
redButton.setBackground(Color.RED); //the red button, which is red
g.gridx= 0;
g.gridy=coloursY;
gamePanel.add(redButton, g);

JButton greenButton = new JButton("Green");
greenButton.setBackground(Color.GREEN);
g.gridx = 1; //The green button, which is green
g.gridy = coloursY;
gamePanel.add(greenButton, g);

JButton blueButton = new JButton("Blue");
blueButton.setBackground(Color.CYAN); //The blue button, which is cyan
g.gridx =2;
g.gridy=coloursY;
gamePanel.add(blueButton, g);

JButton yellowButton = new JButton("Yellow");
yellowButton.setBackground(Color.yellow); //The yellow button which is yellow
g.gridx=3;
g.gridy=coloursY;
gamePanel.add(yellowButton, g);

g.weightx=0;
g.weighty=0;


JLabel firstGuess = new JLabel("First Block Guess:");//The label for the first guess in each guessing sequence, followed by labels for the seconds and third
g.gridx = 0;
g.gridy=1;
g.gridwidth = 2;
gamePanel.add(firstGuess, g);

JLabel secondGuess = new JLabel("Second Block Guess:");
g.gridx = 0;
g.gridy=2;
gamePanel.add(secondGuess, g);

JLabel thirdGuess = new JLabel("Third Block Guess:");
g.gridx = 0;
g.gridy=3;
gamePanel.add(thirdGuess, g);

JTextField guessOne = new JTextField(""); //The text field where the user can enter thier guess for the first colour in a possible solution
guessOne.setPreferredSize(new Dimension(50,24));
g.gridx = 2;
g.gridy = 1;
guessOne.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
gamePanel.add(guessOne, g);

JTextField guessTwo = new JTextField(""); //Second colour
guessTwo.setPreferredSize(new Dimension(50,24));
g.gridx = 2;
g.gridy = 2;
gamePanel.add(guessTwo, g);

JTextField guessThree = new JTextField(""); //Third
guessThree.setPreferredSize(new Dimension(50,24));
g.gridx = 2;
g.gridy = 3;
gamePanel.add(guessThree, g);

JButton update = new JButton();//The update button, which doesn't exist but I used it as a type of repeatable method for whenever the user presses a colour button

JLabel GOneIndicator = new JLabel("<--"); //These arrows move when the user presses a colour button, letting them know where their next colour guess will be applied
g.gridx = 3;
g.gridy = 1;
gamePanel.add(GOneIndicator, g);

JLabel GTwoIndicator = new JLabel("<--");
g.gridx = 3;
g.gridy = 2;
gamePanel.add(GTwoIndicator, g);
GTwoIndicator.setVisible(false);

JLabel GThreeIndicator = new JLabel("<--");
g.gridx = 3;
g.gridy = 3;
gamePanel.add(GThreeIndicator, g);
GThreeIndicator.setVisible(false);

g.gridwidth = 2;
g.fill = GridBagConstraints.HORIZONTAL;

JButton submitButton = new JButton("SUBMIT"); //Submit guess
g.gridx = 0;
g.gridy = 4;
gamePanel.add(submitButton, g);

JButton resetButton = new JButton("Reset"); //Reset your guess JTextFields
g.gridx = 2;
g.gridy = 4;
gamePanel.add(resetButton, g);

JButton newGame = new JButton("New Game"); //Generates a new solution and presses the reset button
g.gridx = 0;
g.gridy = 5;
gamePanel.add(newGame, g);

JButton rulesButton = new JButton("Rules");
g.gridx = 2; //Displays the rules
g.gridy = 5;
gamePanel.add(rulesButton, g);

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String guess[] = new String[3];
boolean proper = true;
guess[0]= guessOne.getText();
guess[1] = guessTwo.getText();
guess[2] = guessThree.getText();
for (int y = 0;y<3;y++)
{
if ((guess[y].compareToIgnoreCase("blue")!=0) && (guess[y].compareToIgnoreCase("red")!=0) && (guess[y].compareToIgnoreCase("green")!=0) && (guess[y].compareToIgnoreCase("yellow")!=0))
{
proper = false; //If one of the text fields had a word that wasn't one of the colours available for guessing, the user will be told so.
}
}
if (proper)
check(guess);//If everything was in order, check the guess against the solution
else
errorWindow();//Otherwise, nope
}
});

resetButton.addActionListener(new ActionListener() {//Sets all of the textFields blank, the guessnumber to 1 and makes the fiirst arrow appear as if it were the first guess.
@Override
public void actionPerformed(ActionEvent e) {
guessOne.setText("");
guessTwo.setText("");
guessThree.setText("");
guessNum=1;
GOneIndicator.setVisible(true);
GTwoIndicator.setVisible(false);
GThreeIndicator.setVisible(false);
}
});

newGame.addActionListener(new ActionListener () {
@Override
public void actionPerformed(ActionEvent e) {//Clicks the reset and generates a new solution set.
resetButton.doClick();
for(int d=0;d<3;d++)
solution[d]= solution();
}
});

if (restart)//If this screen was generated from the user pressing the play again button after winning, the game should be automatically reset and new solutions generated, this is bugging out somehow.
{
newGame.doClick();
GOneIndicator.setText("working");
resetButton.doClick();
restart=false;
}

rulesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//more rules display that isnt finished yet
gamePanel.setVisible(false);
displayRules("game");
}
});

update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//When a colour button is pressed, the next guess Field is filled in order, depending on the last one that was filled
colourGuess[guessNum-1] = colourArgument;
if (guessNum ==1 ){
guessOne.setText(colourArgument);
GOneIndicator.setVisible(false);
GTwoIndicator.setVisible(true);
guessNum++;
}
else if (guessNum ==2){
guessTwo.setText(colourArgument);
GTwoIndicator.setVisible(false);
GThreeIndicator.setVisible(true);
guessNum++;
}
else if (guessNum==3){
guessThree.setText(colourArgument);
GThreeIndicator.setVisible(false);
GOneIndicator.setVisible(true);
guessNum = 1;
}
}

});


redButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) { //Red is put into the next guess slot0
colourArgument = "Red";
update.doClick();
}

});

blueButton.addActionListener(new ActionListener() {//Then blue
@Override
public void actionPerformed(ActionEvent arg0) {
colourArgument = "Blue";
update.doClick();
}
});

yellowButton.addActionListener(new ActionListener() {//Or yellow
@Override
public void actionPerformed(ActionEvent arg0) {
colourArgument = "Yellow";
update.doClick();
}
});

greenButton.addActionListener(new ActionListener() {//or green
@Override
public void actionPerformed(ActionEvent arg0) {
colourArgument = "Green";
update.doClick();
}
});
}
public void check(String guess[])
{
JFrame checkWindow = new JFrame();
checkWindow.setSize(300,100);
JPanel pane = new JPanel(new GridBagLayout());
checkWindow.getContentPane().add(pane); //This is all to set up the initial window generally, and activate the layouts and whatnot

GridBagConstraints c = new GridBagConstraints();
pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
checkWindow.setVisible(true);

c.insets = new Insets(2,2,2,2);

int numGuess[] = new int[3]; //
int colourCount=0;
int positionCount=0; //Converts the information in the textFields to numbers which can be more easily compared
for (int x =0;x<3;x++)
{
if (guess[x].compareToIgnoreCase("red")==0)
{
numGuess[x] = 1;
}
if (guess[x].compareToIgnoreCase("blue")==0)
{
numGuess[x] = 2;
}
if (guess[x].compareToIgnoreCase("green")==0)
{
numGuess[x] = 3;
}
if (guess[x].compareToIgnoreCase("yellow")==0)
{
numGuess[x] = 4;
}
}

for (int z=0;z<3;z++) //Runs through the inputs compared to the solution, finding out how many of the colours were correct, and any of those colours that were in the correct positions
{
boolean guessed = false;
for (int k=0;k<3;k++)
{
if (solution[z] == numGuess[k])
{
guessed = true;
}
}
if (solution[z] == numGuess[z])
positionCount++;
if (guessed)
colourCount++;
}

c.fill=GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
if (positionCount ==3) //If all three positions are correct the user wins
{
JLabel colours = new JLabel(guess[0] + ", " + guess[1] + ", " + guess[2] + " is correct!");
c.gridx=0;
c.gridy=0;
pane.add(colours, c);

JLabel winner = new JLabel("You WIN!");
c.gridx=0;
c.gridy=1;
pane.add(winner, c);

JButton playAgain = new JButton("Play Again");
c.gridx=0;
c.gridy=1;
pane.add(playAgain, c);

playAgain.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//Glitch causing button
for(int x=0;x<3;x++)
{
solution[x] = (int) (Math.random()*4)+1;
}
checkWindow.dispose();
displayGame(true);
}
});
}
else
{

JLabel labelOne = new JLabel(guess[0] + ", " + guess[1] + ", " +guess[2]);//If the user doesn't win, how many correct colours and positions they had are displayed
c.gridx = 0;
c.gridy = 0;
pane.add(labelOne, c);

JLabel colourMessage = new JLabel("You had " + colourCount + " correct colours.");
c.gridx=0;
c.gridy=1;
pane.add(colourMessage, c);

JLabel positionMessage = new JLabel("You had " + positionCount + " in the correct positions");
c.gridx=0;
c.gridy=2;
pane.add(positionMessage, c);
}

}
public void errorWindow()
{
JFrame checkWindow = new JFrame();
checkWindow.setSize(200,100);
JPanel pane = new JPanel(new GridBagLayout());//This window is displayed if the user inputs impossible values for their colour guesses
checkWindow.getContentPane().add(pane);

GridBagConstraints c = new GridBagConstraints();
pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
checkWindow.setVisible(true);

JLabel whoops = new JLabel("Try Again with valid colours.");
c.gridx=0;
c.gridy=0;
pane.add(whoops, c);
}

public static void main(String[] args) {
Summative2 frame = new Summative2(); //main method making the JFrame work properly
guessNum = 1;
frame.pack();
frame.setVisible(true);
frame.setSize(300, 225);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static int solution()
{
return (int) (Math.random()*4+1);//Solution method returning random numbers between 1 and 4 for solutions.
}

}

最佳答案

每次您调用 check()方法,您正在创建新的 ActionListener并在 playAgain 上注册按钮。

通过这种方式,您可以获得大量的操作监听器,一旦用户最终单击 playAgain按钮,它们都被一一调用。

您可以通过在 ActionListener 内设置断点来查看这一点编写代码并调试应用程序。

解决方案是将这个逻辑移出 check()方法,到初始化表单的地方。

关于java - 我是 Java Swing 的新手,一般来说,我今天编写的代码有一个涉及 button.doClick() 方法的奇怪故障。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30607305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com