2016-08-31 47 views
0

我無法在動作中執行什麼操作組合框。我的問題是當我從組合框中選擇一個項目時,它將顯示一個列表

從我的代碼中,cmbCollege有一系列學院{「Business」,「Computer」,「Engineer」}。選擇cmbCollege中的項目將有部門列表。例如,當我從cmbCollege選擇「計算機」它將顯示一個列表,lstDepartment,含有strComputer的[] = { 「CS」, 「IT」}並且當我所選擇的其他的它顯示相應部門如「商務」strBusiness「工程師」strEngineerJComboBox和JList

這裏是我的代碼:

import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.*; 
import java.awt.event.*; 

public class OOP_College extends JFrame implements ActionListener, ListSelectionListener 
{ 
    String strCollege[] = {"Business","Computer", "Engineer"}; 
    String strBusiness[] = {"Management", "Marketing"}; 
    String strComputer[] = {"IT", "CS"}; 
    String strEngineer[] = {"Mechanical", "Electrical", "Electronics"}; 

    JPanel pnlCollege, pnlDepartment, pnlFaculty; 
    JLabel lblCollege, lblChoice, lblDepartment, lblFaculty; 
    JComboBox cmbCollege; 
    JList lstDepartment; 
    JButton btnAdd, btnShow; 
    JTextField tfFaculty; 
    JTextArea taFaculty; 
    GridBagConstraints gbcDepartment = new GridBagConstraints(); 

    public OOP_College() 
    { 
     // TODO Auto-generated constructor stub 
     pnlCollege = new JPanel(new FlowLayout()); 
     lblCollege = new JLabel("Collge: "); 
     cmbCollege = new JComboBox(strCollege); 
     lblChoice = new JLabel(" - Choice"); 
     pnlCollege.add(lblCollege); 
     pnlCollege.add(cmbCollege); 
     pnlCollege.add(lblChoice); 

     cmbCollege.setSelectedItem(0); 
     cmbCollege.addActionListener(this); 

     add(pnlCollege, BorderLayout.NORTH); 

     pnlDepartment = new JPanel(new GridBagLayout()); 
     lblDepartment = new JLabel("Department:"); 
     lstDepartment= new JList(); 

     gbcDepartment.gridx = 0; 
     gbcDepartment.gridy = 0; 
     pnlDepartment.add(lblDepartment, gbcDepartment); 

     gbcDepartment.gridx = 1; 
     gbcDepartment.gridy = 1; 
     pnlDepartment.add(lstDepartment, gbcDepartment); 

     add(pnlDepartment, BorderLayout.CENTER); 

     pnlFaculty = new JPanel(new FlowLayout()); 
     lblFaculty = new JLabel("Faculty Name:"); 
     tfFaculty = new JTextField(20); 
     btnAdd = new JButton("ADD"); 
     btnShow = new JButton("SHOW"); 
     taFaculty = new JTextArea(10,20); 
     taFaculty.setEnabled(false); 
     btnAdd.addActionListener(this); 
     btnShow.addActionListener(this); 
     pnlFaculty.add(lblFaculty); 
     pnlFaculty.add(tfFaculty); 
     pnlFaculty.add(btnAdd); 
     pnlFaculty.add(btnShow); 
     pnlFaculty.add(taFaculty); 

     add(pnlFaculty, BorderLayout.SOUTH); 
    } 

    public static void main(String[] args) 
    { 
     OOP_College oop = new OOP_College(); 
     oop.setSize(500,350); 
     oop.setVisible(true); 
     oop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     // TODO Auto-generated method stub 
     if(e.getSource() == cmbCollege) 
     { 
      JComboBox cb = (JComboBox) e.getSource(); 
      String strSelected = (String) cb.getSelectedItem(); 
      switch (strSelected) 
      { 
      } 
     } 
     else if(e.getSource() == btnAdd) 
     { 
      taFaculty.setText(taFaculty.getText() + tfFaculty.getText() + "\n"); 
     } 
     else if (e.getSource() == btnShow) 
     { 
     } 

    } 

    @Override 
    public void valueChanged(ListSelectionEvent e) 
    { 
     // TODO Auto-generated method stub 

    } 
} 
+0

你可以在一個較小的例子中顯示你的問題嗎?看看http://stackoverflow.com/help/mcve。 –

回答

0

您可以添加ItemsinList使用DefaultListModel這裏是DOC click here DefaultListModel

例如:

DefaultListModel listModel = new DefaultListModel(); 
listModel.addElement("Jane Doe"); 
listModel.addElement("John Smith"); 
listModel.addElement("Kathy Green"); 

,並設置DefaultListModelJList

JList list = new JList(listModel); 

,你可以寫在onclicklistener的ComboBox你的邏輯。

switch (strSelected) { 
     case "Computer": 
      for (int i = 0; i < strComputer.length; i++) { 
       model.addElement(strComputer[i]); 
      } 
      break; 
     case "Business": 
      for (int i = 0; i < strBusiness.length; i++) { 
       model.addElement(strBusiness[i]); 
      } 
      break; 
     case "Engineer": 
      for (int i = 0; i < strEngineer.length; i++) { 
       model.addElement(strEngineer[i]); 
      } 
      break; 
    } 

這裏是舉例:

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.DefaultListModel; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class s extends JFrame implements ActionListener, ListSelectionListener { 
String strCollege[] = {"Business", "Computer", "Engineer"}; 
String strBusiness[] = {"Management", "Marketing"}; 
String strComputer[] = {"IT", "CS"}; 
String strEngineer[] = {"Mechanical", "Electrical", "Electronics"}; 
DefaultListModel model; 


JPanel pnlCollege, pnlDepartment, pnlFaculty; 
JLabel lblCollege, lblChoice, lblDepartment, lblFaculty; 
JComboBox cmbCollege; 
JList lstDepartment; 
JButton btnAdd, btnShow; 
JTextField tfFaculty; 
JTextArea taFaculty; 
GridBagConstraints gbcDepartment = new GridBagConstraints(); 

public s() { 
    // TODO Auto-generated constructor stub 
    pnlCollege = new JPanel(new FlowLayout()); 
    lblCollege = new JLabel("Collge: "); 
    cmbCollege = new JComboBox(strCollege); 
    lblChoice = new JLabel(" - Choice"); 
    pnlCollege.add(lblCollege); 
    pnlCollege.add(cmbCollege); 
    pnlCollege.add(lblChoice); 

    cmbCollege.setSelectedItem(0); 
    cmbCollege.addActionListener(this); 

    add(pnlCollege, BorderLayout.NORTH); 

    model = new DefaultListModel(); 

    pnlDepartment = new JPanel(new GridBagLayout()); 
    lblDepartment = new JLabel("Department:"); 
    lstDepartment = new JList(model); 

    gbcDepartment.gridx = 0; 
    gbcDepartment.gridy = 0; 
    pnlDepartment.add(lblDepartment, gbcDepartment); 

    gbcDepartment.gridx = 1; 
    gbcDepartment.gridy = 1; 
    pnlDepartment.add(lstDepartment, gbcDepartment); 

    add(pnlDepartment, BorderLayout.CENTER); 

    pnlFaculty = new JPanel(new FlowLayout()); 
    lblFaculty = new JLabel("Faculty Name:"); 
    tfFaculty = new JTextField(20); 
    btnAdd = new JButton("ADD"); 
    btnShow = new JButton("SHOW"); 
    taFaculty = new JTextArea(10, 20); 
    taFaculty.setEnabled(false); 
    btnAdd.addActionListener(this); 
    btnShow.addActionListener(this); 
    pnlFaculty.add(lblFaculty); 
    pnlFaculty.add(tfFaculty); 
    pnlFaculty.add(btnAdd); 
    pnlFaculty.add(btnShow); 
    pnlFaculty.add(taFaculty); 

    add(pnlFaculty, BorderLayout.SOUTH); 
} 

public static void main(String[] args) { 
    s oop = new s(); 
    oop.setSize(500, 350); 
    oop.setVisible(true); 
    oop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    if (e.getSource() == cmbCollege) { 
     JComboBox cb = (JComboBox) e.getSource(); 
     String strSelected = (String) cb.getSelectedItem().toString(); 
     model.removeAllElements(); 
     switch (strSelected) { 
      case "Computer": 
       for (int i = 0; i < strComputer.length; i++) { 
        model.addElement(strComputer[i]); 
       } 
       break; 
      case "Business": 
       for (int i = 0; i < strBusiness.length; i++) { 
        model.addElement(strBusiness[i]); 
       } 
       break; 
      case "Engineer": 
       for (int i = 0; i < strEngineer.length; i++) { 
        model.addElement(strEngineer[i]); 
       } 
       break; 

     } 
    } else if (e.getSource() == btnAdd) { 
     taFaculty.setText(taFaculty.getText() + tfFaculty.getText() + "\n"); 
    } else if (e.getSource() == btnShow) { 
    } 

} 

@Override 
public void valueChanged(ListSelectionEvent e) { 
    // TODO Auto-generated method stub 

    } 
} 
+0

謝謝先生!它真的幫助我很多! :) –

0

從我在你的代碼中看到的,我想你想基於所選大學的值來填充lstDepartment。在actionPerformed方法,開關應該是這樣的:

switch (strSelected) { 
    case "Business": 
     lstDepartment.setListData(strBusiness); 
     break; 
    case "Computer": 
     lstDepartment.setListData(strComputer); 
     break; 
    case "Engineer": 
     lstDepartment.setListData(strEngineer); 
     break; 
} 

基於所選擇的值,該列表將與從相應的數組中的值被更新。

我建議你創建一個Map<String,String[]>存儲顯示在comboBox(地圖鍵)和相關部門(數組)中的值。波紋管應該是這樣的代碼:

// The map declaration 
private final Map<String, String[]> colleges = new HashMap<>(); 

// Populating the map and the ComboBox 
colleges.put("", new String[]{""}); // Empty combo box selection 
colleges.put("Business", new String[]{"Management", "Marketing"}); 
colleges.put("Computer", new String[]{"IT", "CS"}); 
colleges.put("Engineer", new String[]{"Mechanical", "Electrical", "Electronics"}); 
cmbCollege = new JComboBox(colleges.keySet().toArray()); 

// The refactored switch 
lstDepartment.setListData(colleges.get(strSelected)); 
+0

謝謝先生,它的工作原理與我想要的一樣:) –

+0

很高興幫助。你應該標記接受的答案。 – Slimu