好吧所以我只需要一个简单的答案/提示/建议,或者如果你们感到慷慨解决我的java GUI代码。
所以基本上我的问题是在这个游戏中,当用户回答我的一个问题时,他/她可以做对,但是,有一个主要的缺陷,用户可以使用我的任何问题的任何答案得到对的。
IK我是这样做的,但我不知道如何以其他方式做到这一点!我做了一些关于这方面的研究,我在谷歌上找不到任何东西,因为我甚至不知道怎么说这个问题,所以我来到这样的论坛,希望得到一些提示或答案。
不,我不希望任何人改变任何事情,只是寻找一些答案来理解该怎么做!
CODE:
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);
}
}
}
}
答案 0 :(得分:1)
答案需要与问题相关联(听起来很明显)......
你可以做的是保留答案和问题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
语句,但“检查答案”按钮除外;)