Java 按钮在代码中的错误位置。

爪哇仙人掌

所以我正在为我的科学课制作一个项目,但问题没有奏效。我想要做的是当我点击第一个问题的答案时,我希望它转到第二个问题,但它转到最后一个问题。我对 Java 很陌生(我上周开始),但我确实有其他语言的基本经验。我很感激我能得到的任何帮助,谢谢。

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Window {

    JFrame window;
    Container con;
    JPanel titlenamePanel, startButtonPanel, mainTextPanel, ChoiceButtonPanel;
    JLabel titlenameLabel;
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 28);
    Font normalFont = new Font("Times New Roman", Font.PLAIN, 18);
    JButton startButton, choice1, choice2, choice3, choice4, choice5, choice6;
    JTextArea mainTextArea;
    String Progress, yourChoice, Wind, Rain, Location, Cloud;
    int Temp, Hum;


    TitleScreenHandler tsHandler = new TitleScreenHandler();
    ChoiceHandler chandler = new ChoiceHandler();

    public static void main(String[] args) {


        new Window();
    }

        public Window(){

            window = new JFrame();
            //width x height
            window.setSize(800, 600);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.getContentPane().setBackground(Color.black);
            window.setLayout(null);
            window.setVisible(true);
            con = window.getContentPane();

            titlenamePanel = new JPanel();
            titlenamePanel.setBounds(100, 100, 600, 150);
            titlenamePanel.setBackground(Color.black);
            titlenameLabel = new JLabel("Weather Genie");
            titlenameLabel.setForeground(Color.WHITE);
            titlenameLabel.setFont(titleFont);

            startButtonPanel = new JPanel();
            startButtonPanel.setBounds(300, 400, 200, 100);
            startButtonPanel.setBackground(Color.black);

            startButton = new JButton("Start");
            startButton.setBackground(Color.black);
            startButton.setForeground(Color.white);
            startButton.setFont(normalFont);
            startButton.addActionListener(tsHandler);

            titlenamePanel.add(titlenameLabel);
            startButtonPanel.add(startButton);

            con.add(titlenamePanel);
            con.add(startButtonPanel);

        }

        public void createWindowScreen() {

            titlenamePanel.setVisible(false);
            startButtonPanel.setVisible(false);
            // Text Panel
            mainTextPanel = new JPanel();
            mainTextPanel.setBounds(100, 100, 600, 250);
            mainTextPanel.setBackground(Color.black);
            con.add(mainTextPanel);
            // Add Texts 
            mainTextArea = new JTextArea("Please answer the following questions.");
            mainTextArea.setBounds(100, 100, 600, 250);
            mainTextArea.setBackground(Color.black);
            mainTextArea.setForeground(Color.white);
            mainTextArea.setFont(normalFont);
            mainTextArea.setLineWrap(true);
            mainTextPanel.add(mainTextArea);
            // Button Panel
            ChoiceButtonPanel = new JPanel();
            ChoiceButtonPanel.setBounds(250, 350, 300, 150);
            ChoiceButtonPanel.setBackground(Color.black);
            ChoiceButtonPanel.setLayout(new GridLayout(3,2));
            con.add(ChoiceButtonPanel);
            // Buttons
            choice1 = new JButton("Choice 1");
            choice1.setBackground(Color.DARK_GRAY);
            choice1.setForeground(Color.white);
            choice1.setFont(normalFont);
            choice1.addActionListener(chandler);
            ChoiceButtonPanel.add(choice1);
            choice1.setActionCommand("c1");
            choice2 = new JButton("Choice 2");
            choice2.setBackground(Color.DARK_GRAY);
            choice2.setForeground(Color.white);
            choice2.setFont(normalFont);
            choice2.addActionListener(chandler);
            ChoiceButtonPanel.add(choice2);
            choice2.setActionCommand("c2");
            choice3 = new JButton("Choice 3");
            choice3.setBackground(Color.DARK_GRAY);
            choice3.setForeground(Color.white);
            choice3.setFont(normalFont);
            choice3.addActionListener(chandler);
            ChoiceButtonPanel.add(choice3);
            choice3.setActionCommand("c3");
            choice4 = new JButton("Choice 4");
            choice4.setBackground(Color.DARK_GRAY);
            choice4.setForeground(Color.white);
            choice4.setFont(normalFont);
            choice4.addActionListener(chandler);
            ChoiceButtonPanel.add(choice4);
            choice4.setActionCommand("c4");
            choice5 = new JButton("Choice 5");
            choice5.setBackground(Color.DARK_GRAY);
            choice5.setForeground(Color.white);
            choice5.setFont(normalFont);
            choice5.addActionListener(chandler);
            ChoiceButtonPanel.add(choice5);
            choice5.setActionCommand("c5");
            choice6 = new JButton("Choice 6");
            choice6.setBackground(Color.DARK_GRAY);
            choice6.setForeground(Color.white);
            choice6.setFont(normalFont);
            choice6.addActionListener(chandler);
            ChoiceButtonPanel.add(choice6);
            choice6.setActionCommand("c6");
            varSetup();
        }   

        public void varSetup() {
            Temp = 80;
            Hum = 65; 
            Location = "South";
            Rain = "No";
            Wind = "No";
            Cloud = "Clear";
            CloudQuestion();
        }

        public void CloudQuestion(){
            Progress = "CloudQ";
            mainTextArea.setText("What do the clouds look like?");
            choice1.setText("High and Dark");
            choice2.setText("High and Bright");
            choice3.setText("Low and Dark");
            choice4.setText("Low and Bright");
            choice5.setText("Clear");
            choice6.setText("Foggy");
        }
        public void TempQuestion(){
            Progress = "TempQ";
            mainTextArea.setText("What tmperature is it outside?");
            choice1.setText("90-110");
            choice2.setText("70-89");
            choice3.setText("50-69");
            choice4.setText("30-49");
            choice5.setText("10-29");
            choice6.setText("0-10");
        }
        public void HumQuestion() {
            Progress = "HumQ";
            mainTextArea.setText("How Humid is it outside? \nEx. The Average Household humidity is 65");
            choice1.setText("90-100");
            choice2.setText("70-89");
            choice3.setText("50-69");
            choice4.setText("30-49");
            choice5.setText("10-29");
            choice6.setText("");
            }
        public void WindQuestion(){
            Progress = "WindQ";
            mainTextArea.setText("Has it been windy outside today?");
            choice1.setText("Yes");
            choice2.setText("No");
            choice3.setText("");
            choice4.setText("");
            choice5.setText("");
            choice6.setText("");
            }
        public void RainQuestion() {
            Progress = "RainQ";
            mainTextArea.setText("Has it Rained this week?");
            choice1.setText("1-2 Times");
            choice2.setText("3-4 Times");
            choice3.setText("5 or more Times");
            choice4.setText("None");
            choice5.setText("");
            choice6.setText("");
            }
        public void LocQuestion() {
            Progress = "LocQ";
            mainTextArea.setText("What part of the US are you in? \nExample, florida would be East \nSomebody in Nebraska or Kansas would say Central. ");
            choice1.setText("North");
            choice2.setText("South");
            choice3.setText("East");
            choice4.setText("West");
            choice5.setText("Central");
            choice6.setText("");
            }

        public class ChoiceHandler implements ActionListener{

            public void actionPerformed(ActionEvent event) {

                String yourChoice = event.getActionCommand();

                switch (Progress) {
                case "CloudQ": 
                    switch(yourChoice) {
                        case"c1":
                            Cloud = "HD";
                            TempQuestion();
                            break;
                        case"c2":
                            Cloud = "HB";
                            TempQuestion();
                            break;
                        case"c3":
                            Cloud = "LD";
                            TempQuestion();
                            break;
                        case"c4":
                            Cloud = "LB";
                            TempQuestion();
                            break;
                        case"c5":
                            Cloud = "Clear";
                            TempQuestion();  
                            break;
                        case"c6":
                            Cloud = "Foggy";
                            TempQuestion();
                            break;
                        }
                case "TempQ": 
                    switch(yourChoice) {
                        case"c1":
                            Temp = 90110;
                            HumQuestion();
                            break;
                        case"c2":
                            Temp = 7089;
                            HumQuestion();
                            break;
                        case"c3":
                            Temp = 5069;
                            HumQuestion();
                            break;
                        case"c4":
                            Temp = 3049;
                            HumQuestion();
                            break;
                        case"c5":
                            Temp = 1029;
                            HumQuestion();
                            break;
                        case"c6":
                            Temp = 010;
                            HumQuestion();
                            break;
                        }
                case "HumQ":
                    switch(yourChoice) {
                        case"c1": 
                            Hum = 90100; WindQuestion();
                            break;
                        case"c2": 
                            Hum = 7089; WindQuestion();
                            break;
                        case"c3": 
                            Hum = 5069; WindQuestion();
                            break;
                        case"c4": 
                            Hum = 3049; WindQuestion();
                            break;
                        case"c5": 
                            Hum = 1029; 
                            WindQuestion();
                            break;
                        case"c6": 
                            HumQuestion();
                            break;
                        }
                case "WinQ":
                    switch(yourChoice) {
                        case"c1": 
                            Wind = "Yes"; 
                            RainQuestion();
                            break;
                        case"c2": 
                            Wind = "No"; 
                            RainQuestion();
                            break;
                        case"c3": 
                            WindQuestion();
                            break;
                        case"c4": 
                            WindQuestion();
                            break;
                        case"c5": 
                            WindQuestion();
                            break;
                        case"c6": 
                            WindQuestion();
                            break;
                        }
                case "RainQ":
                    switch(yourChoice) {
                        case"c1": 
                            Rain = "12"; 
                            LocQuestion();
                            break;
                        case"c2": 
                            Rain = "34"; 
                            LocQuestion();
                            break;
                        case"c3": 
                            Rain = "5+"; 
                            LocQuestion();
                            break;
                        case"c4": 
                            Rain = "None"; 
                            LocQuestion();
                            break;
                        case"c5": 
                            RainQuestion();
                            break;
                        case"c6": 
                            RainQuestion();
                            break;
                        }
                case "LocQ":
                    switch(yourChoice) {
                        case"c1": 
                            Location = "North"; results();break;
                        case"c2": 
                            Location = "South"; results();break;
                        case"c3": 
                            Location = "East"; results();break;
                        case"c4": 
                            Location = "West"; results();break;
                        case"c5": 
                            Location = "Central"; results();break;
                        case"c6": 
                            LocQuestion();
                            break;
                        }
                    break;
                }   
            }
        }
        public void results() {

        }
        public class TitleScreenHandler implements ActionListener{

            public void actionPerformed(ActionEvent event) {

                createWindowScreen();
            }
        }
}
标记

您在另一个 switch 语句中有一个 switch 语句,在您正确使用的内部 switch 语句中break,但是在内部 switch 语句之后您永远不会中断,请参阅下面的代码(仅部分代码)以查看您的内容重新失踪。我用 标记了这些break语句// Add this

不使用breakthere 会导致它casecase它匹配的每一个执行它将这样做,直到break达到第一个switch已经结束。

switch (Progress) {
case "CloudQ":
    switch (yourChoice) {
    case "c1":
        Cloud = "HD";
        TempQuestion();
        break;
    case "c2":
        Cloud = "HB";
        TempQuestion();
        break;
    case "c3":
        Cloud = "LD";
        TempQuestion();
        break;
    case "c4":
        Cloud = "LB";
        TempQuestion();
        break;
    case "c5":
        Cloud = "Clear";
        TempQuestion();
        break;
    case "c6":
        Cloud = "Foggy";
        TempQuestion();
        break;
    }
    break; // Add this
case "TempQ":
    switch (yourChoice) {
    case "c1":
        Temp = 90110;
        HumQuestion();
        break;
    case "c2":
        Temp = 7089;
        HumQuestion();
        break;
    case "c3":
        Temp = 5069;
        HumQuestion();
        break;
    case "c4":
        Temp = 3049;
        HumQuestion();
        break;
    case "c5":
        Temp = 1029;
        HumQuestion();
        break;
    case "c6":
        Temp = 010;
        HumQuestion();
        break;
    }
    break; // Add this
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

属性中的Java按钮位置

来自分类Dev

简单java代码中的错误

来自分类Dev

Java Grid Layout错误的按钮大小和错误的位置

来自分类Dev

通过Java设置布局中按钮的位置

来自分类Dev

在Android中更改高度按钮Java代码

来自分类Dev

如何在代码中查找语义格式错误的参数化类型的位置。JBoss Weld抛出java.lang.reflect.MalformedParameterizedTypeException

来自分类Dev

如何在代码中查找语义格式错误的参数化类型的位置。JBoss Weld抛出java.lang.reflect.MalformedParameterizedTypeException

来自分类Dev

记录器代码中的Java错误

来自分类Dev

简单的Java代码中的某些错误

来自分类Dev

此Java代码中的错误是什么?

来自分类Dev

代码中的Java本机链接错误

来自分类Dev

简单的Java代码中的硒错误

来自分类Dev

Java中错误代码的同步代码输出

来自分类Dev

将按钮添加到Android / Eclipse中的Java代码

来自分类Dev

基本的Java代码错误

来自分类Dev

Dr Java代码错误

来自分类Dev

Java代码错误

来自分类Dev

Java接口代码错误?

来自分类Dev

编译Java代码错误

来自分类Dev

Java代码逻辑错误

来自分类Dev

为什么在jframe java错误中删除按钮

来自分类Dev

我的Java代码中的两个错误

来自分类Dev

如何在Java中获取SQLiteConstraintException的错误代码

来自分类Dev

Java代码中绑定异常错误的数组索引

来自分类Dev

在Java中执行javascript代码时发生错误

来自分类Dev

代码返回堆栈溢出错误在Java中的递归

来自分类Dev

Java代码中的InputMismatchException错误。请给点建议

来自分类Dev

Java代码中绑定异常错误的数组索引

来自分类Dev

在Java中执行javascript代码时发生错误

Related 相关文章

热门标签

归档