2013-11-27 99 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()); 
     } 
    } 
}