アクションに入れたいものは何ですか?コンボボックスです。私の問題は、コンボボックスからアイテムを選択すると、リストと表示されます。
私のコードより、cmbCollegeには、 "Business"、 "Computer"、 "Engineer"}のリストがあります。 cmbCollegeのアイテムを選択すると、のリストが表示されます。私はcmbCollegeから"コンピュータ"を選択した場合、例えば、それは、strComputer [] = { "CS"、 "IT"}を含有するとき、私は他のものにそれを選択し、リスト、lstDepartmentを示すであろうにstrEngineerにstrBusinessと"エンジニア" "ビジネス" のように対応する部署を示しています。ここでJComboBoxとJList
は私のコードです:
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("Jane Doe");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
とJList
にDefaultListModel
を設定します。
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を見てください。 –