java - 一个我无法解决的简单错误

标签 java if-statement

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 问题中不返回 ESATTO 而是返回 ERRATO ...

最佳答案

您无法使用 == 来比较 String,它只会比较引用,而引用始终为 false。

改用sr1.equals("Roma")

了解更多

关于java - 一个我无法解决的简单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24116570/

相关文章:

python-3.x - 如何根据列表中的单词创建新的 Pandas 列

java - 在 do-while 循环中将值添加到整数

java - 条件语句不起作用 Java

java - DB4O体验?

java - ExecutorService 未经检查的分配

java - 批处理在 Spring+Hibernate+JPA 中内存不足

javascript - if 语句包括悬停()和窗口宽度

Java - 找不到 hibernate.cfg.xml 文件

java - 尝试同步两个线程时遇到问题

Java 无限循环