Java设计用户注册界面.ppt

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

53 if(e.getSource()==comb){
54
str=comb.getSelectedItem().toString();
55
ta.append("\n籍 贯:"+str+" =>ItemEvent事件 ");
56 }
57 }
58 public void actionPerformed(ActionEvent e){
59 String str;
60 if(e.getSourcHale Waihona Puke Baidu()==comb){
61
str=comb.getSelectedItem().toString();
62
ta.append("\n籍 贯:"+str+" =>ActionEvent事件 ");
63 }
运行效果
举例CheckBoxDemo .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
48 String str;
49 if(e.getSource()==opt1) // 如果是opt1被选择
50
ta.append("\n性 别:"+"男");
51 else if(e.getSource()==opt2) // 如果opt2被选择
52
ta.append("\n性 别:"+"女");
选择事件—— ListSelectionEvent类
首先必须声明实现监听者对象的类接口 ListSelectionListener,并通过JList类的 addListSelectionListener( )方法注册文本框的监听者对 象,
在ListSelectionListener接口的valueChanged (ListSelectionEvent e)方法体中写入有关代码,就可以 响应ListSelectionEvent事件。
举例 ItemeventDemo.java(续)
23 ta=new JTextArea (8,35); 24 comb=new JComboBox(); 25 comb.addItem("北 京"); 26 comb.addItem("上 海"); 27 comb.addItem("南 京"); 28 comb.addItem("广 州"); 29 comb.addItem("成 都"); 30 comb.addItem("昆 明"); 31 comb.addItemListener(this); 32 comb.addActionListener(this); 33 getContentPane().add(sex); 34 getContentPane().add(opt1); 35 getContentPane().add(opt2); 36 getContentPane().add(city); 37 getContentPane().add(comb); 38 getContentPane().add(ta); 39 setTitle(title); 40 setSize(300,250); 41 setVisible(true); 42 }
implements ListSelectionListener{ 6 JList list = null; 7 JLabel label = null; 8 String[] s = {"宝马","奔驰","奥迪","本田","皇冠
","福特","现代"}; 9 public JListDemo(){ 10 JFrame f = new JFrame("JList Demo"); 11 Container contentPane =
list.setBorder(BorderFactory.createTitledBor der("汽车品牌:"));
17 list.addListSelectionListener(this);
18
contentPane.add(label,BorderLayout.NORT
H);
19 contentPane.add(new
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 public class CheckBoxDemo extends JFrame
implements ItemListener{ 5 private JTextField field; 6 private JCheckBox bold, italic; 7 private int valBold = Font.PLAIN; 8 private int valItalic = Font.PLAIN; 9 public CheckBoxDemo(){ 10 super( "JCheckBox Demo" ); 11 Container container = getContentPane(); 12 container.setLayout( new FlowLayout() ); 13 field = new JTextField( "2008,北京欢迎您
式的综合应用。
9.1 任务描述
9.2 技术要点
选择性组件 选择事件 复杂布局管理器
选择性组件——组合框(JComboBox类)
选择性组件——列表框(JList类)
选择性组件——单选按钮(JRadioButton类)
选择性组件——复选框(JCheckbox类)
选择事件——ItemEvent类
学习情境二(考试系统单机版)
任务9——设计用户注册界面
学习目标
掌握JComboBox、JCheckBoxl、JRadioButton组件的 创建及ItemEvent事件处理。
掌握JList组件的创建及ListSelectionEvent事件的处理。 熟悉网格包布局管理器、盒式布局的使用及其多种布局方
!", 20 ); 14 field.setFont( new Font( "隶书",
Font.PLAIN, 14 ) ); 15 container.add(field ); 16 bold = new JCheckBox( "Bold" );
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 }
f.getContentPane(); 12 contentPane.setLayout(new
BorderLayout(0,15)); 13 label = new JLabel(" "); 14 list = new JList(s); 15 list.setVisibleRowCount(5); 16
举例 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 JRadioButton opt1; 6 JRadioButton opt2; 7 ButtonGroup btg; 8 JTextArea ta; 9 JComboBox comb; 10 JLabel sex,city; 11 public ItemeventDemo(String title){ 12 super(title); 13 setLayout(new FlowLayout(FlowLayout.LEFT)); 14 sex=new JLabel("性 别: "); 15 city=new JLabel(" 籍 贯:"); 16 opt1=new JRadioButton(" 男 "); 17 opt2=new JRadioButton(" 女 "); 18 btg=new ButtonGroup(); 19 btg.add(opt1); 20 btg.add(opt2); 21 opt1.addItemListener(this); 22 opt2.addItemListener(this);
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++){
举例 ItemeventDemo.java(续)
43 public static void main(String args[]){
44 new ItemeventDemo("Itemevent Demo");
45 }
46 // ItemEvent事件发生时的处理操作
47 public void itemStateChanged(ItemEvent e){
2 import javax.swing.*;
3 public class GridBagLayoutDemo extends JFrame {
4 protected void addbutton(String name,GridBagLayout gridbag,
5
GridBagConstraints c) {
6
JButton button = new JButton(name);
7
gridbag.setConstraints(button, c);
8
getContentPane().add(button);
9}
10 public GridBagLayoutDemo(){
31
tmp = index[i];
32
stmp = stmp+s[tmp]+" ";
33 }
34 label.setText(stmp);
35 }
36 }
运行效果
复杂布局管理器 ——网格包布局(GridBagLayout类)
举例 GridBagLayoutDemo.java
1 import java.awt.*;
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 }
相关文档
最新文档