1个回答
展开全部
我给你个例子,你看看就知道了。
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DDD extends JFrame implements ItemListener
{
JComboBox A, B;
JPanel pane;
String a[] = {"动物", "植物"};
String b[] = {"狗", "猫", "人"};
String c[] = {"花", "草", "树"};
DefaultComboBoxModel category = new DefaultComboBoxModel(a);
DefaultComboBoxModel animals = new DefaultComboBoxModel(b);
DefaultComboBoxModel plants = new DefaultComboBoxModel(c);
private DDD()
{
setBounds(400, 200, 400, 300);
pane = new JPanel();
A = new JComboBox(category);
A.addItemListener(this);
B = new JComboBox(animals);
pane.add(A);
pane.add(B);
add(pane);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new DDD();
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==A){
if(A.getSelectedIndex()==0){
B.setModel(animals);
}else if(A.getSelectedIndex()==1){
B.setModel(plants);
}
}
}
}
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DDD extends JFrame implements ItemListener
{
JComboBox A, B;
JPanel pane;
String a[] = {"动物", "植物"};
String b[] = {"狗", "猫", "人"};
String c[] = {"花", "草", "树"};
DefaultComboBoxModel category = new DefaultComboBoxModel(a);
DefaultComboBoxModel animals = new DefaultComboBoxModel(b);
DefaultComboBoxModel plants = new DefaultComboBoxModel(c);
private DDD()
{
setBounds(400, 200, 400, 300);
pane = new JPanel();
A = new JComboBox(category);
A.addItemListener(this);
B = new JComboBox(animals);
pane.add(A);
pane.add(B);
add(pane);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new DDD();
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==A){
if(A.getSelectedIndex()==0){
B.setModel(animals);
}else if(A.getSelectedIndex()==1){
B.setModel(plants);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询