| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2055 人关注过本帖
标题:removeAllItems()的用法
只看楼主 加入收藏
傲天游
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-3-11
收藏
 问题点数:0 回复次数:3 
removeAllItems()的用法
请各位高手帮小弟一个忙:JComboBox类的removeAllItems()怎么用啊(谢谢!!)
搜索更多相关主题的帖子: 用法 JComboBox 
2006-03-12 15:19
weizheng
Rank: 1
等 级:新手上路
威 望:2
帖 子:286
专家分:0
注 册:2005-4-21
收藏
得分:0 
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;

class YAHOO extends JFrame implements ActionListener,ItemListener
{
JTextField jtf1,jtf2,jtf3,jtf4,jtf5;
JPasswordField jpf1,jpf2;
JLabel j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13;
CheckboxGroup bg;
Checkbox jrb1,jrb2;
JButton jb1,jb2;
JComboBox jcb1,jcb2,jcb3;
JPanel jp=new JPanel();
String s8;

GridBagLayout gb;
GridBagConstraints gbc;

public YAHOO()
{
gb=new GridBagLayout();
getContentPane().setLayout(gb);
gbc=new GridBagConstraints();
gbc.insets=new Insets(10,10,10,10);

jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jtf4=new JTextField(10);
jtf5=new JTextField(10);

jpf1=new JPasswordField(10);
jpf2=new JPasswordField(10);

Icon ic=new ImageIcon("yahoo.gif");
j13=new JLabel(ic);

j1=new JLabel("Registration Form");
Font f=new Font("Georgia",Font.BOLD,28);
j1.setFont(f);

j2=new JLabel("Yahoo ID:");
j3=new JLabel("Password:");
j4=new JLabel("Retype Password:");
j5=new JLabel("Fist Name:");
j6=new JLabel("Last Name:");
j7=new JLabel("Gender:");
j8=new JLabel("Birthday:");
j9=new JLabel("Country/Region:");
j10=new JLabel("State:");
j11=new JLabel("@Yahooc.com");
j12=new JLabel("(Month,dd,yyyy)");
bg=new CheckboxGroup();

jrb1=new Checkbox ("Male",true,bg);
//jrb1.setSelected(true);

jrb2=new Checkbox ("Female",false,bg);

//jrb1.setActionCommand("temp1");
//jrb2.setActionCommand("temp2");
//jrb1.addItemListener(this);
//jrb2.addItemListener(this);



jb1=new JButton("OK");
jb2=new JButton("Exit");

jcb1=new JComboBox();
jcb2=new JComboBox();
jcb2.setActionCommand("temp");
jcb3=new JComboBox();

jcb1.addItem("Janury");
jcb1.addItem("February");
jcb1.addItem("Mar");
jcb1.addItem("April");
jcb1.addItem("May");
jcb1.addItem("June");
jcb1.addItem("July");
jcb1.addItem("August");
jcb1.addItem("September");
jcb1.addItem("October");
jcb1.addItem("November");
jcb1.addItem("December");

jcb2.addItem("<Select country>");
jcb2.addItem("Austraila");
jcb2.addItem("India");
jcb2.addItem("United Kingdom");
jcb2.addItem("United States");
jcb2.addActionListener(this);


jb1.addActionListener(this);
jb2.addActionListener(this);




gbc.fill=GridBagConstraints.HORIZONTAL;
AddComponent(j1,0,2,1,2);
AddComponent(j13,1,0,3,7);
AddComponent(j2,5,0,1,1);
AddComponent(jtf1,5,1,1,1);
AddComponent(j11,5,2,1,1);
AddComponent(j3,6,0,1,1);
AddComponent(jpf1,6,1,1,1);
AddComponent(j4,7,0,1,1);
AddComponent(jpf2,7,1,1,1);
AddComponent(j5,8,0,1,1);
AddComponent(jtf2,8,1,1,1);
AddComponent(j6,9,0,1,1);
AddComponent(jtf3,9,1,1,1);
AddComponent(j7,10,0,1,1);
AddComponent(jrb1,10,1,1,1);
AddComponent(jrb2,10,2,1,1);
AddComponent(j8,11,0,1,1);
AddComponent(jcb1,11,1,1,1);
//gbc.fill=GridBagConstraints.NONE;
AddComponent(jtf4,11,2,1,1);
//gbc.fill=GridBagConstraints.NONE;
AddComponent(jtf5,11,3,1,1);
//gbc.fill=GridBagConstraints.HORIZONTAL;
AddComponent(j12,11,4,1,1);
AddComponent(j9,12,0,1,1);
AddComponent(jcb2,12,1,1,1);
AddComponent(j10,13,0,1,1);
AddComponent(jcb3,13,1,1,1);
AddComponent(jb1,15,2,1,1);
//gbc.fill=GridBagConstraints.NONE;
//gbc.weightx=0;
AddComponent(jb2,15,3,1,1);


setBounds(0,0,800,700);


}

public void itemStateChanged (ItemEvent e)
{


}


public void actionPerformed(ActionEvent e)
{
String s8="Male";
if(e.getActionCommand()=="Exit")
{
System.exit(0);
}


else if(e.getActionCommand()=="temp")
{
int num=jcb2.getSelectedIndex();
if(num==1)
{
jcb3.removeAllItems();
jcb3.addItem("Austraila1");
jcb3.addItem("Austraila2");
jcb3.addItem("Austraila3");


}

if(num==2)
{
jcb3.removeAllItems();
jcb3.addItem("India1");
jcb3.addItem("India2");
jcb3.addItem("India3");
}
if(num==3)
{
jcb3.removeAllItems(); //就是这里
jcb3.addItem("United Kingdom1");
jcb3.addItem("United Kingdom2");
jcb3.addItem("United Kingdom3");
}
if(num==4)
{
jcb3.removeAllItems();
jcb3.addItem("United Stetas1");
jcb3.addItem("United Stetas2");
jcb3.addItem("United Stetas3");
}
}



else if(e.getActionCommand()=="OK")
{
String s1=new String(jtf1.getText());
String s2=new String(jpf1.getText());
String s3=new String(jpf2.getText());
String s4=new String(jtf2.getText());
String s5=new String(jtf3.getText());
String s6=new String(jtf4.getText());
String s7=new String(jtf5.getText());



if(s1.length()==0)
{

JOptionPane.showMessageDialog(null,"Please Enter login ID");
jtf1.setFocusable(true);
return;

}

if(!((s1.charAt(0)>=´a´ && s1.charAt(0)<=´z´)||(s1.charAt(0)>=´A´ &&s1.charAt(0)>=´Z´)))
{
JOptionPane.showMessageDialog(null,"Please Enter vaild login ID starting With character");
jtf1.setFocusable(true);

return;
}

if(s2.length()<8)
{
JOptionPane.showMessageDialog(null,"Please Enter password of 8 character");
jpf2.setFocusable(true);
return ;
}

if(!(s2.equals(s3))) //直接使用String赋值不对,因为不是对象必须先NEW一下!
{
JOptionPane.showMessageDialog(null,"Please Re-enter password");
jpf2.setFocusable(true);
return;
}

if(jrb1.getState()==true)
{
s8="Male";
}
else
{
s8="Female";
}


System.out.println(e.getSource());

JOptionPane.showMessageDialog(null,"Yahoo ID:"+s1+"\n"
+"Password:"+s2+"\n"
+"Name:"+s4+" "+s5+"\n"
+"Birthday :"+jcb1.getSelectedItem()+"/"+s6+"/"+s7+"\n"
+"Country/Region:"+jcb1.getSelectedItem()+"\n"
+"Gender:"+s8+"\n"
+"State:"+jcb3.getSelectedItem());


}
}

public static void main(String args[])
{
YAHOO frm=new YAHOO();
frm.show();
}
public void AddComponent(Component c,int row,int col,int nrow,int ncol)
{
gbc.gridx=col;
gbc.gridy=row;
gbc.ipadx=3;
gbc.ipady=5;



gbc.gridwidth=ncol;
gbc.gridheight=nrow;

gb.setConstraints(c,gbc);

getContentPane().add(c);

}

}

温和如玉,完美纯正。
2006-03-12 16:32
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 


import javax.swing.*;
import java.awt.event.*;

public class JComboBoxDemo extends JPanel implements ActionListener{
private JComboBox box;

private JButton btn;

private String str[] = {\"a\",\"b\",\"c\",\"d\",\"e\"};

public JComboBoxDemo() {
box = new JComboBox(str);

btn = new JButton(\"Remove All\");
btn.addActionListener(this);

add(box);
add(btn);
}

public void actionPerformed(ActionEvent e) {
box.removeAllItems();
}

public static void main(String [] arg){
JFrame f = new JFrame(\"Demo\");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JComboBoxDemo());
f.setSize(300,200);
f.setVisible(true);
}
}



我觉得这段比较易懂一点……


向着软件工程师的目标前进!
2006-03-12 16:35
傲天游
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-3-11
收藏
得分:0 
谢谢各位的帮忙!!!!!

不在奋斗成功就在奋斗消沉
2006-03-12 21:16
快速回复:removeAllItems()的用法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014561 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved