Java 从组合框中选择和重新选择

Java 从组合框中选择和重新选择,java,user-interface,combobox,Java,User Interface,Combobox,好的,在Java这学期的最后一个项目中,我们正在做一个战斗模拟。我的一个团队合作伙伴说服我,GUI将是一个伟大的想法,到目前为止,它运行良好,除了一件事。我希望最终用户能够单击组合框来选择一件事,并允许它显示在窗口底部的标签中,而这一部分我很擅长。但是,一旦用户从组合框中选择,我希望他们能够更改其选择。我知道有允许多重选择的选项,但我更多的是寻找一个相互排斥的东西,而不是能够选择两个。我的代码无论如何都不完整,但这里有一些: public void setHair() { //H

好的,在Java这学期的最后一个项目中,我们正在做一个战斗模拟。我的一个团队合作伙伴说服我,GUI将是一个伟大的想法,到目前为止,它运行良好,除了一件事。我希望最终用户能够单击组合框来选择一件事,并允许它显示在窗口底部的标签中,而这一部分我很擅长。但是,一旦用户从组合框中选择,我希望他们能够更改其选择。我知道有允许多重选择的选项,但我更多的是寻找一个相互排斥的东西,而不是能够选择两个。我的代码无论如何都不完整,但这里有一些:

public void setHair()
{
        //Hair Options for both size and color displayed in a window
    window.setSize(400,400);
    window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
    window.setTitle("Hair Options");
    window.setLayout(new BorderLayout());
    window.setVisible(true);
    window.setAlwaysOnTop(true);
    buildHairColorPanel();
    window.add(colorPanel);
    window.add(scrollPane);
    buildLengthPanel();
    window.add(lengthPanel);
   } 
以下是构建方法:

private void buildLengthPanel()
{
    lengthList = new JList(hairLengths);
    lengthList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    lengthList.addListSelectionListener(new ListListener());
    lengthList.setVisibleRowCount(6);
    scrollPane = new JScrollPane(lengthList);
    lengthPanel.add(scrollPane);
    lengthPanel.add(colorList);

}

    private void buildHairColorPanel()
    {
        colorList = new JList(hairColors);
        colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        colorList.addListSelectionListener(new ListListener());
        colorList.setVisibleRowCount(6);
        scrollPane = new JScrollPane(colorList);
        colorPanel.add(scrollPane);
        colorPanel.add(colorList);

    }
我知道这是一个语法问题,或者我需要改变一个设置,但我在教科书中找不到关于如何做的参考资料,而且似乎也不能很好地缩小我的问题的范围,找到它的参考资料

顺便说一句,所有18个选项对于颜色都是可见的,而不是我试图通过添加滚动窗格(滚动窗格也不显示)将其设置为的6个选项。虽然这不是我的主要问题,但如果它是快速指出要解决的问题,我将非常感谢它提供的信息

啊,忘了我也得写这个来配合它:

public class ListListener
implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
String selection = (String) colorList.getSelectedValue();
selectedColor.setText(selection);
}
}
首先,如果您希望一次只选择一个项目,那么我将使用一个。然后可以使用
mymbobox.addItemListener(this)
创建一个侦听器,该侦听器在mymbobox对象中等待选择。代码如下所示:

public void myClass implements ItemListener {

    String[] choices = {"Choice 1", "Choice 2", "Choice 3", "etc..."};

    public myClass() {
        JComboBox myComboBox = new JComboBox(choices);
        myComboBox.addItemListener(this);
        JLabel myLabel = new JLabel("Hello World");
        // You still will need to extend JFrame in this class if you so choose
        // The main purpose of showing a class is to show that you need to
        // Implement ItemListener
    }

    // Your other stuff here

    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChanged() == ItemEvent.SELECTED) {
            myLabel.setText(e.getItem().toString());
        }
    }
}

我自己更愿意像Andrew建议的那样做一个组合框,但是如果你想保持相同的列表,我有一个基于你在这里提供的工作原型

首先,我没有太多的运气使用两个面板上的框架,所以我把两个组合成一个面板

buildHairColorPanel();
window.add(colorPanel);
window.add(scrollPane);
buildLengthPanel();
window.add(lengthPanel);

接下来,每个列表都需要自己的
ListListener
和自己的
JScrollPane

colorScrollPane = new JScrollPane(colorList);
lengthScrollPane = new JScrollPane(lengthList);
colorList.addListSelectionListener(//Look Below...
lengthList.addListSelectionListener(//Look Below...
最后,您只想将滚动窗格添加到面板中,而不是列表本身

hairPanel.add(colorScrollPane);
hairPanel.add(颜色列表)

另外,您可以将其复制粘贴到其自己的类中,并运行它进行测试: 请注意,我必须提供许多未在代码中初始化的字段,因为我只是从main()方法中使用它们,所以我将它们标记为static,这在代码中可能是不必要的

    static JList lengthList;
    static JList colorList;
    static JScrollPane lengthScrollPane;
    static JScrollPane colorScrollPane;
    static JPanel hairPanel = new JPanel();
    static JLabel selectedColor = new JLabel();
    static JLabel selectedLength = new JLabel();

    public static void main(String[] args) {
        JFrame window = new JFrame();
        //Hair Options for both size and color displayed in a window
        window.setSize(400,400);
        window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
        window.setTitle("Hair Options");
        window.setLayout(new BorderLayout());
        window.setAlwaysOnTop(true);

        buildHairPanel();
        window.add(hairPanel);

        window.setVisible(true);
    }


    private static void buildHairPanel()
    {
        //Build Hair Color Selection
        String[] hairColors = new String[] { "brown", "blonde", "black", "red", "green", "blue" };
        colorList = new JList(hairColors);
        colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        colorList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e)
            {
                String selection = (String) optionsSelect.colorList.getSelectedValue();
                optionsSelect.selectedColor.setText(selection);
            }
        });

        colorList.setVisibleRowCount(3);
        colorScrollPane = new JScrollPane(colorList);
        hairPanel.add(colorScrollPane);

        //Build Hair Length Selection
        String[] hairLengths = new String[] { "short1", "short2", "medium1", "medium2", "long1", "long2" };
        lengthList = new JList(hairLengths);
        lengthList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        lengthList.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e)
            {
                String selection = (String) optionsSelect.lengthList.getSelectedValue();
                optionsSelect.selectedLength.setText(selection);
            }
        });

        lengthList.setVisibleRowCount(3);
        lengthScrollPane = new JScrollPane(lengthList);
        hairPanel.add(lengthScrollPane);
    }

到目前为止,我注意到一件事,在
lengthPanel.add(colorList)下你可能想添加
长度列表
。哈!谢谢现在纠正一下,Oye。现在只有一个组合框出现,而不是调试时两个组合框都出现。“我的一个团队合作伙伴让我相信GUI将是一个好主意,……”把他们的头打了个正着。1) 听起来好像GUI不是问题规范的一部分。2) GUI比命令行应用程序更难编写代码。创建GUI是一个高级的话题。我也必须把自己的头往上打,因为我是那个告诉我们的教授我对通常的东西感到厌倦的人,他告诉我给他一个更好的。。。所以我做了。这就是为什么我们有一个战斗模拟而不是骰子模拟我花了这么长时间才接受你的回答。我花了一点时间才有机会在学期快结束的时候回到这个话题上来。你的信息很棒,它解决了我所有的问题!现在用我的新信息实现其他方法和变量!
    static JList lengthList;
    static JList colorList;
    static JScrollPane lengthScrollPane;
    static JScrollPane colorScrollPane;
    static JPanel hairPanel = new JPanel();
    static JLabel selectedColor = new JLabel();
    static JLabel selectedLength = new JLabel();

    public static void main(String[] args) {
        JFrame window = new JFrame();
        //Hair Options for both size and color displayed in a window
        window.setSize(400,400);
        window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
        window.setTitle("Hair Options");
        window.setLayout(new BorderLayout());
        window.setAlwaysOnTop(true);

        buildHairPanel();
        window.add(hairPanel);

        window.setVisible(true);
    }


    private static void buildHairPanel()
    {
        //Build Hair Color Selection
        String[] hairColors = new String[] { "brown", "blonde", "black", "red", "green", "blue" };
        colorList = new JList(hairColors);
        colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        colorList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e)
            {
                String selection = (String) optionsSelect.colorList.getSelectedValue();
                optionsSelect.selectedColor.setText(selection);
            }
        });

        colorList.setVisibleRowCount(3);
        colorScrollPane = new JScrollPane(colorList);
        hairPanel.add(colorScrollPane);

        //Build Hair Length Selection
        String[] hairLengths = new String[] { "short1", "short2", "medium1", "medium2", "long1", "long2" };
        lengthList = new JList(hairLengths);
        lengthList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        lengthList.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e)
            {
                String selection = (String) optionsSelect.lengthList.getSelectedValue();
                optionsSelect.selectedLength.setText(selection);
            }
        });

        lengthList.setVisibleRowCount(3);
        lengthScrollPane = new JScrollPane(lengthList);
        hairPanel.add(lengthScrollPane);
    }