Java设计用户注册界面
例9-3 JListDemo.java
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.event.*; 5 public class JListDemo extends JFrame implements ListSelectionListener{ 6 7 8 9 10 11 12 13 14 15 16 list.setBorder(BorderFactory.createTitledBor der("汽车品牌:")); 汽车品牌: 汽车品牌 JList list = null; JLabel label = null; String[] s = {"宝马 奔驰 奥迪 本田 皇冠 宝马","奔驰 奥迪","本田 宝马 奔驰","奥迪 本田","皇冠 ","福特 现代 福特","现代 福特 现代"}; public JListDemo(){ JFrame f = new JFrame("JList Demo"); Container contentPane = f.getContentPane(); contentPane.setLayout(new BorderLayout(0,15)); label = new JLabel(" "); list = new JList(s); list.setVisibleRowCount(5); 17 list.addListSelectionListener(this); 18 contentPane.add(label,BorderLayout.NORT H); 19 contentPane.add(new JScrollPane(list),BorderLayout.CENTER); 20 f.setSize(300,200); 21 f.setVisible(true); 22 } 23 public static void main(String args[]){ 24 new JListDemo(); 25 } 26 public void valueChanged(ListSelectionEvent e){ 27 int tmp = 0; 28 String stmp = "您喜欢的汽车品牌有 "; 您喜欢的汽车品牌有: 您喜欢的汽车品牌有 29 int[] index = list.getSelectedIndices(); 30 for(int i=0; i < index.length ; i++){ 31 tmp = index[i]; 32 stmp = stmp+s[tmp]+" "; 33 } 34 label.setText(stmp); 35 } 36 }
选择性组件——复选框(JCheckbox类) 复选框( 选择性组件 复选框 类
选择事件——ItemEvent类 类 选择事件
举例 ItemeventDemo.java
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 public class ItemeventDemo extends JFrame implements ItemListener,ActionListener{ 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 JRadioButton opt1; JRadioButton opt2; ButtonGroup btg; JTextArea ta; JComboBox comb; JLabel sex,city; public ItemeventDemo(String title){ super(title); setLayout(new FlowLayout(FlowLayout.LEFT)); sex=new JLabel("性 别: "); 性 city=new JLabel(" 籍 贯:"); opt1=new JRadioButton(" 男 "); opt2=new JRadioButton(" 女 "); btg=new ButtonGroup(); btg.add(opt1); btg.add(opt2); opt1.addItemListener(this); opt2.addItemListener(this);
17 container.add( bold ); 18 italic = new JCheckBox( "Italic" ); 19 container.add( italic ); 20 bold.addItemListener(this); 21 italic.addItemListener( this ); 22 setSize( 280, 100 ); 23 setVisible( true ); 24 } 25 public void itemStateChanged(ItemEvent event){ 26 if ( event.getSource() == bold ) 27 valBold = bold.isSelected() ? Font.BOLD : Font.PLAIN; 28 if ( event.getSource() == italic ) 29 valItalic = italic.isSelected() ? Font.ITALIC : Font.PLAIN; 30 field.setFont( new Font( "隶书 valBold + 隶书", 隶书 valItalic, 14 ) ); 31 } 32 public static void main(String args[]){ 33 new CheckBoxDemo(); 34 } 35 }
选择事件—— ListSelectionEvent类 选择事件 类
首先必须声明实现监听者对象的类接口 ListSelectionListener,并通过JList JList类的 ListSelectionListener,并通过JList类的 )方法注册文本框的监听者对 addListSelectionListener( )方法注册文本框的监听者对 象, 在ListSelectionListener接口的valueChanged ListSelectionListener接口的valueChanged 接口的 e)方法体中写入有关代码 方法体中写入有关代码, (ListSelectionEvent e)方法体中写入有关代码,就可以 响应ListSelectionEvent事件。 ListSelectionEvent事件 响应ListSelectionEvent事件。
运行效果
举例CheckBoxDemo .java 举例
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 public class CheckBoxDemo extends JFrame implements ItemListener{ 5 6 7 8 9 10 11 12 13 14 15 16 private JTextField field; private JCheckBox bold, italic; private int valBold = Font.PLAIN; private int valItalic = Font.PLAIN; public CheckBoxDemo(){ super( "JCheckBox Demo" ); Container container = getContentPane(); container.setLayout( new FlowLayout() ); field = new JTextField( "2008,北京欢迎您 , !", 20 ); field.setFont( new Font( "隶书 隶书", 隶书 Font.PLAIN, 14 ) ); container.add(field ); bold = new JCheckBox( "Bold" );
ItemeventDemo.java( 举例 ItemeventDemo.java(续)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 ta=new JTextArea (8,35); comb=new JComboBox(); comb.addItem("北 京"); 北 comb.addItem("上 海"); 上 comb.addItem("南 京"); 南 comb.addItem("广 州"); 广 comb.addItem("成 都"); 成 comb.addItem("昆 明"); 昆 comb.addItemListener(this); comb.addActionListener(this); getContentPane().add(sex); getContentPane().add(opt1); getContentPane().add(opt2); getContentPane().add(city); getContentPane().add(comb); getContentPane().add(ta); setTitle(title); setSize(300,250); setVisible(true); }