我無法在動作中執行什麼操作組合框。我的問題是當我從組合框中選擇一個項目時,它將顯示一個列表。
從我的代碼中,cmbCollege有一系列學院{「Business」,「Computer」,「Engineer」}。選擇cmbCollege中的項目將有部門列表。例如,當我從cmbCollege選擇「計算機」它將顯示一個列表,lstDepartment,含有strComputer的[] = { 「CS」, 「IT」}並且當我所選擇的其他的它顯示相應部門如「商務」至strBusiness和「工程師」至strEngineer。JComboBox和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
}
}
你可以在一個較小的例子中顯示你的問題嗎?看看http://stackoverflow.com/help/mcve。 –