【发布时间】:2016-09-24 17:35:27
【问题描述】:
我正在编写一个测验应用程序,当你回答正确的问题并且你的分数增加时,你会得到分数,我必须使用 if 语句。请有人知道如何在另一个 if 语句中使用 if 语句中的值!我对此有点困惑,它让我在这里工作....感谢您的帮助!...这是一个小代码示例;
int x = 3;
String xy = Integer.toString(x);
int y = 0;
String yy = Integer.toString(y);
JButton one = new JButton ("Quest 1");
one.addActionListener (new ActionListener (){
public void actionPerformed(ActionEvent p) {
JFrame ex = new JFrame ();
ex.setTitle("Question 1);
ex.setSize(400, 400);
ex.setLayout(new FlowLayout());
ex.setBackground(Color.WHITE);
JLabel ey = new JLabel ("What is the capital of Japan?);
Font tan = new Font ("Script MT Bold", Font.BOLD, 18);
ey.setFont(tan);
ey.setForeground(Color.BLACK);
ex.add(ey, BorderLayout.NORTH);
JButton answ = new JButton("submit");
JTextField g = new JTextField (10);
g.setFont(tan);
String ans = "Tokyo";
String merit = "Correct";
String flop = "wrong";
String mer = merit + ans;
String flip = flop + ans;
answ.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent p) {
if (g.getText.equals("Tokyo") {
JOptionPane.showMessageDialog(null, mer);
one.setText(xy);
}
else {
JOptionPane.showMessageDialog(null,flip);
one.setText(yy);
}
//In my next Action Listener, I would love to
//pick the score from the previous listener....and add to the next score....
//So that we have something like ....
//x(updated from previous listener) + x
ex.add(g, BorderLayout.SOUTH);
}
});
}
});
【问题讨论】:
-
请多花点时间和精力来解释您的问题,因为您提供的信息很少,这很令人困惑。
-
为什么再次投反对票?....这个问题有什么不清楚的地方?
-
我不能肯定地说,但你还没有发布一个minimal reproducible example,并且还没有在一个 if 位内澄清你的 if。
标签: java if-statement int jbutton actionlistener