java中怎样使JComboBox的对象任意改变成想要的值,求高手补充代码,基本代码如下:
publicclassTestextendsJFrameimplementsItemListener{JComboBoxA,B,C;JPanelpane;privateT...
public class Test extends JFrame implements ItemListener
{
JComboBox A, B, C;
JPanel pane;
private Test()
{
setBounds(400, 200, 400, 300);
pane = new JPanel();
String a[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
A = new JComboBox(a);
A.addItemListener(this);
String b[] = {"1", "2", "3", "4", "5"};
B = new JComboBox(b);
B.addItemListener(this);
pane.add(A);
pane.add(B);
add(pane);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
public void itemStateChanged(ItemEvent e)
{
//这里些什么代码?才能让在第一个下拉列表中改变选项的值n使第二个下拉列表只有n项,比如,选择在第一个列表中选6,那么第二个列表的选项就只有1~6????
}
} 展开
{
JComboBox A, B, C;
JPanel pane;
private Test()
{
setBounds(400, 200, 400, 300);
pane = new JPanel();
String a[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
A = new JComboBox(a);
A.addItemListener(this);
String b[] = {"1", "2", "3", "4", "5"};
B = new JComboBox(b);
B.addItemListener(this);
pane.add(A);
pane.add(B);
add(pane);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
public void itemStateChanged(ItemEvent e)
{
//这里些什么代码?才能让在第一个下拉列表中改变选项的值n使第二个下拉列表只有n项,比如,选择在第一个列表中选6,那么第二个列表的选项就只有1~6????
}
} 展开
1个回答
展开全部
按照你的要求,我帮你完善了你的程序,加入了itemStateChanged事件的程序,你看看吧。
完整的程序如下:
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame implements ItemListener
{
JComboBox A, B, C;
JPanel pane;
private Test()
{
setBounds(400, 200, 400, 300);
pane = new JPanel();
String a[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
A = new JComboBox(a);
A.addItemListener(this);
String b[] = {"1", "2", "3", "4", "5"};
B = new JComboBox(b);
B.addItemListener(this);
pane.add(A);
pane.add(B);
add(pane);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==A){
B.removeAllItems();
String s=(String)e.getItem();
int n=Integer.parseInt(s);
for(int i=1;i<=n;i++){
B.addItem(i);
}
}
}
}
完整的程序如下:
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame implements ItemListener
{
JComboBox A, B, C;
JPanel pane;
private Test()
{
setBounds(400, 200, 400, 300);
pane = new JPanel();
String a[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
A = new JComboBox(a);
A.addItemListener(this);
String b[] = {"1", "2", "3", "4", "5"};
B = new JComboBox(b);
B.addItemListener(this);
pane.add(A);
pane.add(B);
add(pane);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==A){
B.removeAllItems();
String s=(String)e.getItem();
int n=Integer.parseInt(s);
for(int i=1;i<=n;i++){
B.addItem(i);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询