从组合框中选择并重新选择

时间:2013-11-27 20:55:21

标签: java user-interface combobox

好的,对于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个选项都可以看到颜色的东西,而不是我试图通过添加滚动窗格来设置它的六个(并且滚动窗格也没有出现)虽然这不是我的主要问题,如果能够快速指出修复它,我会很感激它。

啊,忘记了我必须写这个也是为了配合它:

public class ListListener
implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
String selection = (String) colorList.getSelectedValue();
selectedColor.setText(selection);
}
}

2 个答案:

答案 0 :(得分:1)

我首先要使用JComboBox,如果你想一次只选择一个项目,那就是要走的路。然后,您可以使用myComboBox.addItemListener(this)创建一个等待myComboBox对象内部选择的侦听器。代码看起来像这样:

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());
        }
    }
}

答案 1 :(得分:1)

我自己宁愿做像安德鲁建议的ComboBox,但是如果你想保留相同的列表,我会根据你在这里提供的工作原型。

首先,我在框架上使用两个面板没有太多运气,所以我将两者合并为一个面板。

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

buildHairPanel();
window.add(hairPanel);

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

colorScrollPane = new JScrollPane(colorList);
lengthScrollPane = new JScrollPane(lengthList);
colorList.addListSelectionListener(//Look Below...
lengthList.addListSelectionListener(//Look Below...

最后,您只想将ScrollPanes添加到面板,而不是List本身。

hairPanel.add(colorScrollPane);

<击> hairPanel.add(colorList);

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

    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);
    }