2013-11-27 49 views
0

对于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); 
} 
} 
+0

有一件事我到目前为止已经注意到,在'lengthPanel下。add(colorList);'你可能的意思是增加'lengthList'。 – DoubleDouble

+0

哈!谢谢。现在纠正。 –

+0

Oye。现在只有一个组合框出现,而不是在调试时显示它们。 –

回答

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添加到面板上,不在名单本身。

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