java swing 组合框问题 两个组合框,改变其中一个组合框的值,另一个组合框的值也会相应改变。请问各位,在Swing中如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class Demo extends JFrame implements ActionListener{ private static final long serialVersionUID = -6336187772824409988L; private JPanel rootPanel = new JPanel(); private JComboBox box1 = new JComboBox(new String[]{"1","2","3","4"}); private JComboBox box2 = new JComboBox(new String[]{"one","two","three","four"}); public Demo(){ init(); } public void init(){ this.setSize(300,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setContentPane(rootPanel); rootPanel.add(box1); rootPanel.add(box2); box1.addActionListener(this); box2.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == box1){ int selectedIndex = box1.getSelectedIndex(); if(selectedIndex > -1 && selectedIndex < box1.getItemCount()){ box2.setSelectedIndex(selectedIndex); } }else if(e.getSource() == box2){ int selectedIndex = box2.getSelectedIndex(); if(selectedIndex > -1 && selectedIndex < box2.getItemCount()){ box1.setSelectedIndex(selectedIndex); } } } public static void main(String[] args){ Demo d = new Demo(); d.setVisible(true); }} 这里只是简单的写死的item Object,实际的数据加载同样 list.add(new XzBean("1","2","3"));list可以加入对象吗? 循环内存取数组元素为什么不好?(书上原话请看帖子内容) 关于用log4j的简单的问题 关于super关键字的一点思考,请高人进来帮忙,谢谢! 我解决不了的错误 看过来,判断InetAddress的hashCode()有什么意义! 急,急,急,在线等待,用JAVA模拟交通灯的显示。 怎样做一个透明的FRAME 几个关于短信的问题 .net发布的webService程序,java做客户端遇见问题。 Java基础(不断更新中) 我执行applet打印按钮
private static final long serialVersionUID = -6336187772824409988L;
private JPanel rootPanel = new JPanel();
private JComboBox box1 = new JComboBox(new String[]{"1","2","3","4"});
private JComboBox box2 = new JComboBox(new String[]{"one","two","three","four"});
public Demo(){
init();
}
public void init(){
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(rootPanel);
rootPanel.add(box1);
rootPanel.add(box2);
box1.addActionListener(this);
box2.addActionListener(this);
} public void actionPerformed(ActionEvent e) {
if(e.getSource() == box1){
int selectedIndex = box1.getSelectedIndex();
if(selectedIndex > -1 && selectedIndex < box1.getItemCount()){
box2.setSelectedIndex(selectedIndex);
}
}else if(e.getSource() == box2){
int selectedIndex = box2.getSelectedIndex();
if(selectedIndex > -1 && selectedIndex < box2.getItemCount()){
box1.setSelectedIndex(selectedIndex);
}
}
}
public static void main(String[] args){
Demo d = new Demo();
d.setVisible(true);
}
}