Java 等待更换收音机

Java 等待更换收音机,java,wait,Java,Wait,好的,我要做的是等待用户选择不同的选项,然后在程序读取他/她的答案之前点击continue按钮 目前,程序不会等待用户更改答案并点击“选择”按钮 它不会更改选项文本,但它将是不同的道路名称 目前我有: import java.awt.event.ActionListener; import java.awt.event.ContainerEvent; import java.awt.event.ContainerListener; import java.io.FileWriter; impor

好的,我要做的是等待用户选择不同的选项,然后在程序读取他/她的答案之前点击continue按钮

目前,程序不会等待用户更改答案并点击“选择”按钮

它不会更改选项文本,但它将是不同的道路名称

目前我有:

import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GameScreen extends javax.swing.JFrame implements ActionListener, ContainerListener {

    static String classString;
    static int classInt = 0; // 1- wiz 2-warr 3-arch 4-lock  
    static int option;

    public static void classStore() throws IOException {

    FileWriter classChose = new FileWriter("classChose.txt");
    System.out.print(classInt);
    System.out.print(classString);
    classChose.write(classString);
    classChose.close();
    start.setVisible(true);

    }
    public GameScreen() {
        initComponents();
    }
    @SuppressWarnings("unchecked")

    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jScrollPane1 = new javax.swing.JScrollPane();
        info = new javax.swing.JTextArea();
        opt1 = new javax.swing.JRadioButton();
        opt2 = new javax.swing.JRadioButton();
        opt3 = new javax.swing.JRadioButton();
        opt4 = new javax.swing.JRadioButton();
        optionOneText = new javax.swing.JTextField();
        optionTwoText = new javax.swing.JTextField();
        optionThreeText = new javax.swing.JTextField();
        optionFourText = new javax.swing.JTextField();
        classButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        optionButton = new javax.swing.JButton();
        start = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        info.setColumns(20);
        info.setRows(5);
        info.setText("Welcome. Please choose a class.");
        jScrollPane1.setViewportView(info);

        buttonGroup1.add(opt1);
        opt1.setText("Option One");

        buttonGroup1.add(opt2);
        opt2.setText("Option Two");

        buttonGroup1.add(opt3);
        opt3.setText("Option Three");

        buttonGroup1.add(opt4);
        opt4.setText("Option Four");

        optionOneText.setEditable(false);
        optionOneText.setBackground(new java.awt.Color(255, 255, 255));
        optionOneText.setText("Wizard");

        optionTwoText.setEditable(false);
        optionTwoText.setBackground(new java.awt.Color(255, 255, 255));
        optionTwoText.setText("Warrior");

        optionThreeText.setEditable(false);
        optionThreeText.setBackground(new java.awt.Color(255, 255, 255));
        optionThreeText.setText("Archer");

        optionFourText.setEditable(false);
        optionFourText.setBackground(new java.awt.Color(255, 255, 255));
        optionFourText.setText("Warlock");

        classButton.setText("Choose");
        classButton.addActionListener(this);

        jButton2.setText("Exit");
        jButton2.addActionListener(this);

        optionButton.setText("Choose");
        optionButton.setEnabled(false);
        optionButton.addContainerListener(this);
        optionButton.addActionListener(this);

        start.setText("Begin");
        start.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(classButton)
                .addGap(34, 34, 34)
                .addComponent(optionButton)
                .addGap(34, 34, 34)
                .addComponent(start)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2))
            .addGroup(layout.createSequentialGroup()
                .addComponent(opt3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(optionThreeText, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(opt4)
                    .addComponent(opt2)
                    .addComponent(opt1, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(optionFourText)
                    .addComponent(optionOneText)
                    .addComponent(optionTwoText, javax.swing.GroupLayout.Alignment.TRAILING)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opt1)
                    .addComponent(optionOneText, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opt2)
                    .addComponent(optionTwoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opt3)
                    .addComponent(optionThreeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(opt4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(optionFourText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(classButton)
                    .addComponent(jButton2)
                    .addComponent(optionButton)
                    .addComponent(start))
                .addContainerGap())
        );

        pack();
    }

    public void actionPerformed(java.awt.event.ActionEvent evt) {
        if (evt.getSource() == classButton) {
            GameScreen.this.classButtonActionPerformed(evt);
        }
        else if (evt.getSource() == jButton2) {
            GameScreen.this.jButton2ActionPerformed(evt);
        }
        else if (evt.getSource() == start) {
            GameScreen.this.startActionPerformed(evt);
        }
        else if (evt.getSource() == optionButton) {
            GameScreen.this.optionButtonActionPerformed(evt);
        }
    }

    public void componentAdded(java.awt.event.ContainerEvent evt) {
        if (evt.getSource() == optionButton) {
            GameScreen.this.optionButtonComponentAdded(evt);
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.exit(0);
    }                                        

    private void classButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        //Check option
        while (classInt < 1)
        {
        if (opt1.isSelected())
        {
         classString = "Wizard";
         classInt = 1;
         optionButton.setVisible(true);
         classButton.setVisible(false);
        }
        else if (opt2.isSelected())
        {
        classString = "Warrior";
         classInt = 2;
         optionButton.setVisible(true);
         classButton.setVisible(false);
        }
        else if(opt3.isSelected())
        {
            classString = "Archer";
         classInt = 3;
         optionButton.setVisible(true);
         classButton.setVisible(false);
        }
        else if(opt4.isSelected())
        {
            classString = "Warlock";
         classInt = 4;
         optionButton.setVisible(true);
         classButton.setVisible(false);
        }
        else
        {
        info.setText("Please select a class below.");
        }

            break;
        }
        try {
            buttonGroup1.clearSelection();
            classStore();
            info.setText("You have chosen to be an: " + classString);
        } catch (IOException ex) {
            Logger.getLogger(GameScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                           

    private void optionButtonComponentAdded(java.awt.event.ContainerEvent evt) {                                            
        optionButton.setVisible(false);
    }                                           

    private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
        start.setVisible(false);
        optionButton.setEnabled(true);

        wizardStory();

    }                                     

    private void optionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        //Check option
        if (opt1.isSelected())
        {option = 1;

        }
        else if (opt2.isSelected())
            option = 2;
        else if (opt3.isSelected())
            option = 3;
        else 
            option = 4;


    }     
    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GameScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GameScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GameScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GameScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GameScreen().setVisible(true);
                optionButton.setVisible(false);
                start.setVisible(false);
            }
        });
    }
    public int getOption()
    {
    optionButton.isSelected();
    optionButton.isEnabled();
    return option;
    }
    public void wizardStory()
    {
    info.setText("This is where your story begins.\nPlease select the road you wish to travel.");

    if (option == 1)
    {
        info.setText("Didn't work.");
        System.out.print("didnt work");
    }
    else if (option == 2)
    {
        info.setText("Did work.");
        System.out.print("worked");
    }
    }                 
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton classButton;
    private javax.swing.JTextArea info;
    private javax.swing.JButton jButton2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton opt1;
    private javax.swing.JRadioButton opt2;
    private javax.swing.JRadioButton opt3;
    private javax.swing.JRadioButton opt4;
    public static javax.swing.JButton optionButton;
    private javax.swing.JTextField optionFourText;
    private javax.swing.JTextField optionOneText;
    private javax.swing.JTextField optionThreeText;
    private javax.swing.JTextField optionTwoText;
    private static javax.swing.JButton start;
    @Override
    public void componentRemoved(ContainerEvent ce) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }              
}
导入java.awt.event.ActionListener;
导入java.awt.event.ContainerEvent;
导入java.awt.event.ContainerListener;
导入java.io.FileWriter;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.Scanner;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类GameScreen扩展了javax.swing.JFrame实现了ActionListener、ContainerListener{
静态字符串类字符串;
静态int classInt=0;//1-wiz 2-warr 3-arch 4-lock
静态int选项;
public static void classStore()引发IOException{
FileWriter classselect=newFileWriter(“classselect.txt”);
系统输出打印(classInt);
系统输出打印(类字符串);
classselect.write(classString);
classselect.close();
start.setVisible(true);
}
公共游戏屏幕(){
初始化组件();
}
@抑制警告(“未选中”)
私有组件(){
buttonGroup1=newjavax.swing.ButtonGroup();
jPopupMenu1=newjavax.swing.JPopupMenu();
jScrollPane1=newjavax.swing.JScrollPane();
info=newjavax.swing.JTextArea();
opt1=newjavax.swing.JRadioButton();
opt2=newjavax.swing.JRadioButton();
opt3=newjavax.swing.JRadioButton();
opt4=newjavax.swing.JRadioButton();
optionOneText=newjavax.swing.JTextField();
optionTwoText=newjavax.swing.JTextField();
optionThreeText=newjavax.swing.JTextField();
optionFourText=newjavax.swing.JTextField();
classButton=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
optionButton=newjavax.swing.JButton();
start=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
信息设置栏(20);
信息设置行(5);
info.setText(“欢迎。请选择一个类。”);
jScrollPane1.setViewportView(信息);
按钮组1.添加(opt1);
opt1.setText(“选项一”);
按钮组1.添加(opt2);
opt2.setText(“选项二”);
按钮组1.添加(opt3);
opt3.setText(“选项三”);
按钮组1.添加(opt4);
opt4.setText(“选项四”);
optionOneText.setEditable(假);
setBackground(新java.awt.Color(255、255、255));
optionOneText.setText(“向导”);
optionTwoText.setEditable(false);
setBackground(新java.awt.Color(255、255、255));
setText(“战士”);
optionThreeText.setEditable(假);
setBackground(新java.awt.Color(255、255、255));
选择treetext.setText(“Archer”);
optionFourText.setEditable(假);
optionFourText.setBackground(新java.awt.Color(255、255、255));
选项FourText.setText(“术士”);
classButton.setText(“选择”);
classButton.addActionListener(这个);
jButton2.setText(“退出”);
jButton2.addActionListener(这个);
optionButton.setText(“选择”);
optionButton.setEnabled(错误);
optionButton.addContainerListener(此选项);
optionButton.addActionListener(此);
start.setText(“开始”);
start.addActionListener(this);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGap(11,11,11)
.addComponent(类按钮)
.addGap(34,34,34)
.addComponent(选项按钮)
.addGap(34,34,34)
.addComponent(开始)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addComponent(opt3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
.addComponent(optionThreeText,javax.swing.GroupLayout.PREFERRED_SIZE,302,javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.training)
.addComponent(opt4)
.addComponent(opt2)
.addComponent(opt1、javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(选项FourText)
.addComponent(optionOneText)
.addComponent(optionTwoText,javax.swing.GroupLayout.Alignment.training)))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,115,javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18,18,18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(opt1)
.addC