0

ここに画像の説明を入力してください私は新しいプログラマーで、入力デバイスとしてドロップダウン ボックス (選択肢) を使用するテキスト アドベンチャーに取り組んでいます。最初のボックスに、2 番目のメンバーに追加可能なメンバーを設定する itemListener があります。次に、プレーヤーは送信ボタンをクリックできるようになり、最初のボックスがリストの最初の項目にリセットされ、2 番目のボックスがクリアされることになっています。プログラムを実行すると、最初は計画どおりに反応します。2 回目にドロップダウン ボックスを使用して何かを入力しようとすると、ドロップダウン ボックスが応答しません。itemListener 内にマーカーを配置して、トリガーされていないことを確認するためにトリガーされているかどうかを確認しました。考えられるあらゆる方法でプログラムを微調整したような気がしますが、この問題の原因はわかりません。

これは、私がまとめた問題を表したものです。

import java.awt.Choice;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;


public class gui implements ActionListener
{
private static JTextPane outputField = new JTextPane();
private static JPanel mainPanel = new JPanel(new GridBagLayout());
private Choice commandDropDown = new Choice();
private Choice itemDropDown = new Choice();
private JButton submitButton = new JButton();

public gui()
{
    JFrame frame = new JFrame("test");
    mainPanel.setSize(450,585);

    commandDropDown = buildCommandBox(commandDropDown);
    commandDropDown.setBounds(100, 15, 100, 40);
    itemDropDown.setBounds(200, 15, 100, 40);
    submitButton.setText("submit");
    submitButton.setBounds(15, 15, 100, 40);
    submitButton.addActionListener(this);
    frame.add(commandDropDown);
    frame.add(itemDropDown);
    frame.add(submitButton);
                frame.setResizable(false);
        frame.pack();
        frame.setSize(300, 300);
        //frame.setLayout(null);
        //frame.setLocationRelativeTo(null);
        frame.setVisible(true);

}
public void actionPerformed(ActionEvent e)
{
    itemDropDown.removeAll();
    commandDropDown.select(0);
}

private Choice buildCommandBox(Choice custoChoi)
{
   final Choice c = new Choice();

   c.addItem("choices");
   c.addItem("Option1");
   c.addItemListener(new ItemListener()
           {                   
                public void itemStateChanged(ItemEvent ie)
                {
                    System.out.println("the action event for the command"
                            + "box is working.");
                    if(c.getSelectedItem().equals("Option1"))
                    {
                        itemDropDown.addItem("things");
                        itemDropDown.addItem("stuff");
                    }
                }
           });

   return c;
}
}

願わくば、これらの写真が私の投稿に関する混乱を解消してくれることを願っています。 http://imgur.com/a/h9oOX#0

4

1 に答える 1

0

私の意見では、あなたbuildCommandBox( Choice custoChoi)は間違っています。それは次のようなものでなければなりません:

private Choice buildCommandBox(final Choice custoChoi) {

    custoChoi.addItem("choices");
    custoChoi.addItem("Option1");
    custoChoi.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent ie) {
            System.out.println("the action event for the command" + "box is working.");
            if (custoChoi.getSelectedItem().equals("Option1")) {
                itemDropDown.addItem("things");
                itemDropDown.addItem("stuff");
            }
        }
    });

    return custoChoi;
}

許可されている場合は、JComboBoxインスタントを使用することをお勧めします。ChoiceSwing

于 2013-09-06T07:05:39.560 に答える