gpt4 book ai didi

java - GUI JButton,如何将按钮(问题)分配给答案

转载 作者:行者123 更新时间:2023-12-01 13:10:30 26 4
gpt4 key购买 nike

好吧,所以我只需要一个简单的答案/提示/建议,或者如果你们愿意修复我的 java GUI 代码。

所以基本上我的问题是在这个游戏中,当用户回答我的一个问题时,他/她可以答对,但是,有一个主要缺陷,用户可以使用我的任何问题的任何答案来获得对的。

我就是这样做的,但我不知道如何以其他方式做到这一点!我对此做了一些研究,但在谷歌上找不到任何东西,因为我什至不知道如何表达这个问题,所以我来到这样的论坛,希望得到一些提示或答案。

不,我不希望任何人改变任何事情,只是寻找一些答案来了解该怎么做!

代码:

public void init () //method name init
{
Container pane = getContentPane();
pane.setLayout (new GridLayout (6,0));

//set component fonts
Font titleFont = new Font ("TimesRoman", Font.BOLD, 35);
Font otherFont = new Font ("TimesRoman", Font.BOLD, 35);

//gives a title for program
lblTitle = new JLabel ("GEOGRAPHY JEOPARDY", JLabel.CENTER);
lblTitle.setFont (titleFont);

//use color constant to set colour
lblTitle.setForeground (Color.blue);
lblTitle.setLocation (0,0);

//makes the intro empty for now
lblIntro = new JLabel ("");
//gives the label a font
lblIntro.setFont (otherFont);

//the buttons are initialized and contains strings to be compared in action listener
btn1 = new JButton ("Question 1, 100 points");
btn2 = new JButton ("Question 2, 200 points");
btn3 = new JButton ("Question 3, 300 points");
btn4 = new JButton ("Question 4, 400 points");
btn5 = new JButton ("Question 5, 500 points");
btnCheck = new JButton ("Check answer");


//sets the text to nothing
txtInput = new JTextField ("", JTextField.CENTER);
//gives the text in textbox a font
txtInput.setFont (otherFont);


//add actionListener to buttons
btn1.addActionListener (this);
btn2.addActionListener (this);
btn3.addActionListener (this);
btn4.addActionListener (this);
btn5.addActionListener (this);
btnCheck.addActionListener (this);

//makes the label info empty
lblInfo = new JLabel ("", JLabel.CENTER);

//Split button into 2 seperate buttons
panelButtons = new JPanel (new FlowLayout ());
panelButtons.add (btn1);
panelButtons.add (btn2);
panelButtons.add (btn3);
panelButtons.add (btn4);
panelButtons.add (btn5);
panelButtons.add (btnCheck);


//the items to add on the panel
pane.add (lblTitle);
pane.add (lblIntro);
pane.add (panelButtons);
pane.add (lblInfo);
pane.add (btn1);
pane.add (btn2);
pane.add (btn3);
pane.add (btn4);
pane.add (btn5);
pane.add (btnCheck);
pane.add (txtInput);



}
public void actionPerformed (ActionEvent event)
{


//which buttons are being pressed
String command = event.getActionCommand();
//if statements regarding what happens when the buttons are clicked
if (command.equals("Question 1, 100 points"))
{
//set the text to blank
btn1.setText("");
//set the label with the question
lblInfo.setText(q1);

}
//if this button is clicked...
else if (command.equals("Question 2, 200 points"))
{
//set the text empty
btn2.setText("");
//display question 2
lblInfo.setText(q2);
}
//if this buttton is clicked...
else if (command.equals("Question 3, 300 points"))
{
//empty text
btn3.setText("");
//show question 3
lblInfo.setText(q3);
}
else if (command.equals("Question 4, 400 points"))
{
//make button 4 text empty to indicate that it has been already picked
btn4.setText("");
//show question 4 in the label called info
lblInfo.setText(q4);
}
//the action listener is lisening for this button to be clicked
else if (command.equals("Question 5, 500 points"))
{
//make emtpy
btn5.setText("");
//display question 5
lblInfo.setText(q5);
}
else
{

}
//listening for that button
if (command.equals("Check answer"))
{
//the textfield is equal to the variable input
input = txtInput.getText();
//***THE PROBLEM***
if (input.equals(a1)||input.equals(a2)||input.equals(a3)||input.equals(a4)||input.equals(a5))
{
//if the user input is equal to the answer do...
if (input.equals(a1))
{
//say its correct
lblIntro.setText("Corect!");
//display it in green colour
lblIntro.setForeground (Color.green);
//give user 100 points
points = points + 100;
//display points
lblTitle.setText("You have: " + points);
}
//if the user input is equal to the answer do...
else if (input.equals(a2))
{
//say its correct
lblIntro.setText("Corect!");
//display it in green colour
lblIntro.setForeground (Color.green);
//give user points
points = points + 200;
//display the points on panel
lblTitle.setText("You have: " + points);
}
//if the user input is equal to the answer do...
else if (input.equals(a3))
{
//correct answer
lblIntro.setText("Corect!");
//set green colour-
lblIntro.setForeground (Color.green);
//give points for correct answer
points = points + 300;
//display those points
lblTitle.setText("You have: " + points);
}
//if the user input is equal to the answer do...
else if (input.equals(a4))
{
//say the user is correct
lblIntro.setText("Corect!");
//show it in green
lblIntro.setForeground (Color.green);
//give points for reward
points = points + 400;
//show on title
lblTitle.setText("You have: " + points);
}
//if the user input is equal to the answer do...
else if (input.equals(a5))
{
//say he or she is correct
lblIntro.setText("Corect!");
//make the text to display it in green
lblIntro.setForeground (Color.green);
//give them points for their efforts
points = points + 500;
//display the points awarded
lblTitle.setText("You have: " + points);
}
}
// if any other answer is given
else
{
//say they are incorrect
lblIntro.setText("Incorrect!");
//right it in red
lblIntro.setForeground (Color.red);
}
}
}
}

最佳答案

答案需要与问题相关联(听起来很明显)...

您可以做的是将答案和问题保存在某种Map中,这将允许您根据活跃的问题循环答案。

private Map<String, String> mapQA;
private String currentQuestion;

//...

mapQA = new HashMap<>(4);
mapQA.put(q1, a1); // Or how ever you want to do it...
//...

if (command.equals("Question 1, 100 points"))
{
//set the text to blank
btn1.setText("");
//set the label with the question
lblInfo.setText(q1);
currentQuestion = q1;

}
//if this button is clicked...
else if (command.equals("Question 2, 200 points"))

//...

if (command.equals("Check answer"))
{
//the textfield is equal to the variable input
input = txtInput.getText();
String answer = mapQA.get(currentQuestion);
if (answer.equals(txtInput.getText()) {...

现在,如果是我,我可能还会将每个问题与每个 JButton 相关联...

 Map<JButton, String> mapButtons;

//...
btn1 = new JButton ("Question 1, 100 points");
//...
mapButtons = new HashMap<JButton, String>(4);
mapButtons.put(btn1, "..."); // Question 1...

这样,您就可以简单地使用...

JButton btn = (JButton)event.getSource();

//set the text to blank
btn .setText("");
//set the label with the question
currentQuestion = mapButtons.get(btn)
lblInfo.setText(currentQuestion);

并取消 if 语句,除了“检查答案”按钮;)

关于java - GUI JButton,如何将按钮(问题)分配给答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903385/

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