public class Quiz {
private static void homePage() {
JFrame homePage = new JFrame("QUIZ");
Container c = homePage.getContentPane();
homePage.setVisible(true);
homePage.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
homePage.setBounds(600,180,700,400);
homePage.setResizable(false);
homePage.setContentPane(c);
JLabel benvenuto = new JLabel("Benvenuto!");
benvenuto.setFont(new Font("Serif", Font.BOLD, 30));
benvenuto.setBounds(265,0,180,120);
benvenuto.setOpaque(true);
JLabel label1 = new JLabel("Inserisci il tuo nome:");
label1.setBounds(215,100,120,100);
label1.setOpaque(true);
JTextArea areaNome = new JTextArea();
areaNome.setEditable(true);
areaNome.setFont(new Font("Serif", Font.BOLD, 16));
areaNome.setBounds(345,142,120,20);
javax.swing.border.Border borderP = BorderFactory.createLineBorder(Color.BLACK);
areaNome.setBorder(borderP);
JLabel labelStarting = new JLabel();
labelStarting.setBounds(230,230,280,120);
labelStarting.setForeground(Color.RED);
JButton startButton = new JButton("Inizia");
startButton.setBounds(280,200,120,50);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String gt1 = areaNome.getText();
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(Quiz.class.getName()).log(Level.SEVERE, null, ex);
}
labelStarting.setText("Preparati "+gt1+", il gioco sta per iniziare.");
JFrame quizPage = new JFrame("QUIZ");
quizPage.setBounds(573,150,750,450);
quizPage.setVisible(true);
quizPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
quizPage.setResizable(false);
Container c2 = quizPage.getContentPane();
quizPage.setContentPane(c2);
JLabel domanda1 = new JLabel("1) Qual è la capitale d'Italia?");
domanda1.setBounds(0,0,280,120);
JLabel domanda2 = new JLabel("2) Quanto fa 2^8?");
domanda2.setBounds(0,55,280,120);
JLabel domanda3 = new JLabel("3) Quando è stata scoperta l'America da Colombo?");
domanda3.setBounds(0,110,380,120);
JLabel domanda4 = new JLabel("4) Dove si trova l'Empire State Building?");
domanda4.setBounds(0,165,280,120);
JLabel domanda5 = new JLabel("5) Qual è il nome dell'autore della Divina Commedia?");
domanda5.setBounds(0,220,380,120);
JLabel labelNull2 = new JLabel();
labelNull2.setBounds(230,230,280,120);
JTextArea r1 = new JTextArea();
r1.setBounds(320,50,120,20);
javax.swing.border.Border border1 = BorderFactory.createLineBorder(Color.BLACK);
r1.setBorder(border1);
JTextArea r2 = new JTextArea();
r2.setBounds(320,105,120,20);
javax.swing.border.Border border2 = BorderFactory.createLineBorder(Color.BLACK);
r2.setBorder(border2);
JTextArea r3 = new JTextArea();
r3.setBounds(320,160,120,20);
javax.swing.border.Border border3 = BorderFactory.createLineBorder(Color.BLACK);
r3.setBorder(border3);
JTextArea r4 = new JTextArea();
r4.setBounds(320,217,120,20);
javax.swing.border.Border border4 = BorderFactory.createLineBorder(Color.BLACK);
r4.setBorder(border4);
JTextArea r5 = new JTextArea();
r5.setBounds(320,270,120,20);
javax.swing.border.Border border5 = BorderFactory.createLineBorder(Color.BLACK);
r5.setBorder(border5);
JButton br1 = new JButton("Conferma");
br1.setBounds(475,49,120,25);
JButton br2 = new JButton("Conferma");
br2.setBounds(475,105,120,25);
JButton br3 = new JButton("Conferma");
br3.setBounds(475,160,120,25);
JButton br4 = new JButton("Conferma");
br4.setBounds(475,216,120,25);
JButton br5 = new JButton("Conferma");
br5.setBounds(475,270,120,25);
JLabel lp1 = new JLabel();
lp1.setBounds(635,45,100,30);
lp1.setForeground(Color.GREEN);
JLabel lp2 = new JLabel();
lp2.setBounds(635,103,100,30);
lp2.setForeground(Color.GREEN);
JLabel lp3 = new JLabel();
lp3.setBounds(635,158,100,30);
lp3.setForeground(Color.GREEN);
JLabel lp4 = new JLabel();
lp4.setBounds(635,213,100,30);
lp4.setForeground(Color.GREEN);
JLabel lp5 = new JLabel();
lp5.setBounds(635,269,100,30);
lp5.setForeground(Color.GREEN);
JLabel lp11 = new JLabel();
lp11.setBounds(635,45,100,30);
lp11.setForeground(Color.RED);
JLabel lp22 = new JLabel();
lp22.setBounds(635,103,100,30);
lp22.setForeground(Color.RED);
JLabel lp33 = new JLabel();
lp33.setBounds(635,158,100,30);
lp33.setForeground(Color.RED);
JLabel lp44 = new JLabel();
lp44.setBounds(635,213,100,30);
lp44.setForeground(Color.RED);
JLabel lp55 = new JLabel();
lp55.setBounds(635,269,100,30);
lp55.setForeground(Color.RED);
br1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr1 = r1.getText();
if(sr1=="Roma") {
lp1.setText("ESATTO");
}else {
lp11.setText("ERRATO");
}
}
});
br2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr2 = r2.getText();
if(sr2=="256") {
lp2.setText("ESATTO");
}else {
lp22.setText("ERRATO");
}
}
});
br3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr3 = r3.getText();
if(sr3=="1492") {
lp3.setText("ESATTO");
}else {
lp33.setText("ERRATO");
}
}
});
br4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr4 = r4.getText();
if(sr4=="New York"||sr4=="new york") {
lp4.setText("ESATTO");
}else {
lp44.setText("ERRATO");
}
}
});
br5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr5 = r5.getText();
if(sr5=="Dante"||sr5=="dante") {
lp5.setText("ESATTO");
}else {
lp55.setText("ERRATO");
}
}
});
c2.add(domanda1);
c2.add(domanda2);
c2.add(domanda3);
c2.add(domanda4);
c2.add(domanda5);
c2.add(r1);
c2.add(r2);
c2.add(r3);
c2.add(r4);
c2.add(r5);
c2.add(br1);
c2.add(br2);
c2.add(br3);
c2.add(br4);
c2.add(br5);
c2.add(lp1);
c2.add(lp2);
c2.add(lp3);
c2.add(lp4);
c2.add(lp5);
c2.add(lp11);
c2.add(lp22);
c2.add(lp33);
c2.add(lp44);
c2.add(lp55);
c2.add(labelNull2);
}
});
JLabel labelNull = new JLabel();
c.add(benvenuto);
c.add(label1);
c.add(areaNome);
c.add(startButton);
c.add(labelStarting);
c.add(labelNull);
}
public static void main(String[] args) {
homePage();
}
}
当我运行我的应用程序时,我在Action Event
中收到错误:if if问题不返回ESATTO
但返回ERRATO
...
答案 0 :(得分:3)
您无法使用String
对==
进行比较,它只会比较引用,而该引用始终为false。
改为使用sr1.equals("Roma")
。
了解更多
答案 1 :(得分:1)
您的问题似乎有点令人困惑,When I run my application I get an error in the Action Event: in the if question don't return ESATTO but return ERRATO.
您是否在代码中遇到了一些运行时错误,或者您将错误的返回类型声明为错误。你的代码也太长了,总是发布产生错误的代码部分。
对于if条件的返回部分,不应使用==运算符比较字符串。原因是无论何时我们创建新的字符串EX: - String s ="hello"
,java都会将字符串文字添加到字符串池中。所以下次我创建String n= "hello"
时它不会创建一个新的字符串对象,而是指定n
从字符串池本身指向hello。因此,如果我这样做: -
String h="hello";
String n="hello";
if(n==h){//return true because they are pointing to the same string object in the pool};
但如果我这样做: -
String h="hello";
String n=new String("hello");//explicitly creating a new string object although there is one already in the pool
if(n==h){//return false because they are now poiting to the different string object in the pool};
虽然s和n在值上相等但它们在引用方面不相等,而且==用于比较对象中的引用(在int,float等基元的情况下它可以正常工作,但是字符串是对象)。
要实际比较字符串的值(通常是这种情况),必须始终使用.equals()方法。这仅仅根据其内容比较字符串,与引用无关。
所以根据这个改变你的代码,它应该工作(如果假设错误仅在逻辑中)。您的代码中的示例代码段将从此
更改String sr5 = r5.getText();
if(sr5=="Dante"||sr5=="dante") {
lp5.setText("ESATTO");
}else {
lp55.setText("ERRATO");
}
到此: -
String sr5 = r5.getText();
if(sr5.equals("Dante")||sr5.equals("dante")) {
lp5.setText("ESATTO");
}else {
lp55.setText("ERRATO");
}