- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果有人可以帮我检查我的代码,给我一些指示等,我将非常感激!因此,在这款游戏中,如果您从未玩过 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/
我是 Xcode 4.4 和 AppleScriptObjC 世界的新手。我正在尝试扩展和试验 Sanderson 和 Rosenthal 所著的“学习 AppleScript”一书中关于 Apple
我完全迷失在 shell 编程中,主要是因为我使用的每个站点都提供不同的工具来进行模式匹配。所以我的问题是使用什么工具在管道流中进行简单的模式匹配。 上下文:我有named.conf 文件,我需要一个
我对 C 很陌生,我一直在尝试用这种数据结构制作一个程序: struct node { char command[100]; char prereq[100][80]; cha
该程序检查用户输入的数字是否为素数。 我的问题在if语句中。由于某些原因,Boolean永远不会切换。如果数字为质数,则只会给出两个结果。 我想念什么? import java.util.Scanne
我只是在学习 Haskell。我认为这会产生一个阶乘函数...... (在 ghci 内) Prelude> let ft 0 = 1 Prelude> let ft n = n * ft (n -
这个问题已经有答案了: Using bitwise OR 0 to floor a number (7 个回答) 已关闭 6 年前。 我试图在 JavaScript 中使用二分搜索来查找数组元素,并且
使用 Signal R,如果尝试发送对象,传递模型的语法是什么? private async void FormLoaded(object sender, RoutedEventArgs e) {
我需要使用 Javascript 生成一个半金字塔数字系列,其中包含输入的起始数字和 html 页面中的行数,并在 html 页面中显示结果。我已经完成了 Java 脚本编写之类的工作。我不明白的是它
为什么函数名重复 示例: lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry
我花了2天的时间在GGTS中使用grails进行Web开发。我正在跟着一本书。本书使用命令行。到目前为止,这很棒,但是现在这本书正在使用webtest。我已经在命令行上安装了webtest,但是如何在
我正在学习 Clojure,到目前为止我无法理解这个小难题,我确信这是非常基本的。 我有这个文件: (ns cloapp.core (:gen-class)) (defn -main "I d
我在获取图像以显示在我的 J Frame 中时遇到问题。我确信我将文件放在正确的位置并且输入了正确的名称。这是代码 import java.awt.Color; import java.awt.Gra
我正在尝试为我正在做的应用程序创建一个登录窗口。我整天都在寻找一个例子,但我似乎找不到任何有帮助的东西。我的基本结构如下: // App.scala object App extends Simple
坦率地说,我是 Java 新手。我正在开发一个项目,我想找到一种基于数字序列创建多项式函数的方法。 无论如何,我的问题是我创建了一个存储序列的数组。我现在想找出元素之间的差异。例如。我想找到这个计算a
现在添加了 xml 和 logcat,现在自定义 View 代码,不幸的是我远离开发计算机所以我无法检查你的建议,@jems,我的自定义 View 的构造函数可能错误?@Falmarri,我认为构建目
我在这里缺少什么?当我单击“h2 a”链接时,.content ol 应该切换。我不明白为什么它不起作用:( $(document).ready(function(){ $(".content ol
我是 Java 新手,我到处寻找,但我没有得到一个简单的概念。 我将两个变量声明为 int。我希望这两个变量对于所有方法都是全局的。在我的第一个方法中,我想从用户输入中获取第一个变量的值。然后我希望第
我正在抓取 IMDB 页面的数据,但当尝试将其写入 CSV 文件时,我只从结果中获取最后一行。 代码下方: from urllib.request import urlopen as uReq fro
自从我学习 C 语言以来,我决定制作一个简单的程序,用于加、减和计算两个变量的乘积。根据用户的输入是1,2还是3来选择加/减/折叠。 #include int main (void) { in
int main(void) { string n = GetString(); if(n!=NULL){ for(int i=0, j=strlen(n); i
我是一名优秀的程序员,十分优秀!