-4

好的,所以我要做的是等待用户选择不同的选择并在程序读取他/她的答案之前点击继续按钮。

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

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

目前我有这个:

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."); 
    }              
}
4

1 回答 1

0

GUI 是事件驱动的环境。也就是说,与通常情况下程序从一个指令序列移动到另一个指令序列的控制台程序不同,GUI 的“等待”某种事件发生然后响应这些事件。

在 Swing 中,您实际上不需要等待,这是为您处理的。您需要能够做的是在适当的事件发生时对其作出响应。

仔细查看使用 Swing 创建 GUI 以了解更多详细信息。

您的基本想法是设计某种方式,您可以 1- 将选项放在屏幕上 2- 在它发生时做出响应并actionEvent在它发生时更新程序的状态。

于 2013-07-22T06:26:40.857 回答