丁保国的博客

收集整理工作生活,点点滴滴

  :: :: 博问 :: 闪存 :: :: :: :: 管理 ::

第十八讲 Java GUI常用组件

 

主要内容

 

复选框、单选按钮和按钮组

JCheckBoxJRadioButtonButtonGroup

“调查表”示例的设计和解读

组合框和列表框*

JComboBoxJList

 

复选框、单选按钮和按钮组

 

复选框(JCheckBox

 

复选框的作用

给出若干选项,提供用户选择。

复选框有两种状态:选中和未选中。在一组复选框中,同时可以选择多项。

 

复选框的构造方法

JCheckBox()                        无选项文本、无选中

JCheckBox(String text)          指定选项文本、无选中

JCheckBox(String text,boolean select)

 

 

常用方法

isSelected():可测得复选框是否被选中。

 

练习:设计一个继承面板的Favorite类,类别有:运动、电脑、音乐、读书。界面如:,用以下程序测试该类。

import java.awt.*;

import javax.swing.*;

 

class TestPanel{

  public static void main(String[] aa){

     JFrame f=new JFrame("Test");

     f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

     f.getContentPane().add(new Favorite());

     f.pack();

     f.setVisible(true);

  }

}

单选按钮(JRadioButton

 

 

单选按钮的作用

给出若干选项,提供用户选择其中一项。

单选按钮同复选框一样也有两种状态:选中和未选中。

多个单选按钮通常组织在一个按钮组中工作,此时只能选择其中一项。

 

单选按钮的构造方法

JRadioButton()                        无文本、无选中

JRadioButton(String text)          指定选项文本、无选中

JRadioButton(String text, boolean selected)

 

常用方法

isSelected():可测得单选按钮是否被选中。

练习:设计一个继承面板的SexBox类,界面如:

 

存在问题:没有组合的单选按钮象复选框一样都可独立被选中,如何将多个单选按钮组合在一个按钮组中?

 

按钮组(ButtonGroup

按钮组的作用

用于把几个按钮组成一个组,由此可以相互影响。

按钮组允许其中的按钮具有相互排斥的选取行为。

创建:ButtonGroup 按钮组名=new ButtonGroup();

 

常用方法

add(AbstractButton b)                 加入按钮到组中

JCheckBoxJRadioButtonJButton都是AbstractButton的直接和间接子类)

 

设计思路和步骤

²       创建一个按钮组

²       将相关按钮加入到按钮组按钮组名.add(按钮名)(仅是一种逻辑组织,与界面无关)

²       将相关按钮加入到包容器或面板

 

“调查表”示例的设计和解读

设计一个继承面板的Favorite

 

 

设计一个继承JPanelSexBox

 

 

设计一个继承JPanelNameBox

 

 

设计一个继承JPanelThreeButton

 

 

界面的综合设计

分析和调试如下FillTest类,将以上面板有序地组合在同一个窗口中。

import java.awt.*;

import javax.swing.*;

 

class FillTest1 extends JFrame {

  NameBox n;

  SexBox s;

  Favorite f;

  ThreeButton tb;

  JTextArea t;

  FillTest1(){

     super("调查表");

     this.setDefaultCloseOperation(3);

     fillInit();

     Container cc=this.getContentPane();

     cc.setLayout(new FlowLayout());

     cc.add(n);

     cc.add(s);

     cc.add(f);

     cc.add(new JScrollPane(t));

     cc.add(tb);

     this.setSize(300,300);

     this.setVisible(true);

    

  }

  void fillInit(){

    n=new NameBox();

    s=new SexBox();

    f=new Favorite();

     t=new JTextArea(5,24);

    tb=new ThreeButton();

  }

 

  public static void main(String[] aa){

     new FillTest1();

  }

}

 

进行事件的响应处理

 

public void actionPerformed(ActionEvent e){

  JButton b=(JButton)e.getSource();

  if(b.getText().equals("List")){

     String s1="姓名:"+n.t.getText();

     String s2="性别:"+(s.r1.isSelected()?"":"");

     String s3="爱好:"+(f.c1.isSelected()?"运动 ":"")

     +(f.c2.isSelected()?"电脑 ":"")

     +(f.c3.isSelected()?"音乐 ":"")

     +(f.c4.isSelected()?"阅读":"");

  t.setText(s1+"\r\n"+s2+"\r\n"+s3);

}

else if(b.getText().equals("Exit"))

  System.exit(0);

else {

  String s=JOptionPane.showInputDialog("FileName?");

     try{

        FileWriter out=new FileWriter(s);

        out.write(t.getText());

        out.close();

     }

     catch(Exception ee){}

  }

}

组合框和列表框*

组合框(JComboBox

组合框的作用

以下拉列表的方式给出若干选项,提供用户从中选择。

下拉列表在单击箭头时才能浏览其中的项目。

 

组合框的构造方法

JComboBox()创建一个默认数据项的组合框。

JComboBox(Object[] items)创建一个包含指定数组各元素的组合框。

 

常用方法

Object getSelectedItem():返回当前选择项

int getSelectedIndex():返回当前选择项的序数(0开始)

int getItemCount():返回组合框中的项数。

 

练习:修改SexBox类,性别面板上加入组合框,其中选项:男、女。界面如:

 

 

 

列表框(Jlist

列表框的作用

以多行列表的方式给出若干选项,提供用户从中选择。

 

构造方法:

JList()创建一个内容为空的列表框。

JList(Object[] items)创建一个包含指定数组各元素的列表框。

 

常用方法

确定列表框尺寸

void setVisibleRowCount(int num):设置列表可见行数。

void setFixedCellWidth(int width):设置列表框的固定宽度(像素)

void setFixedCellHeight(int height):设置列表框的固定高度。

其它方法

Boolean isSelectedIndex(int index):序数为index的项是否被选中。

 

 

练习:

1. 修改以上Favorite类,界面如:

 

2. 修改以上FillTest.java,使之运行后的界面如下图。

 

3. 参考前面的事件处理方法,修改FillTest.java,使之可以正确响应事件。

   public void actionPerformed(ActionEvent e){

       Object o=e.getSource();

       if(o==tb.b1){

 

 

       }

       else if(o==tb.b2){

          try{

              FileWriter out=new FileWriter("temp.txt",true);

              out.write("\r\n"+t.getText());

              JOptionPane.showMessageDialog(this,"文件已保存!");

              out.close();

          }catch(Exception ee){}

       }

       else System.exit(0);    

   }

 

列表框事件处理

ListSelectionEvent:当从列表框中选定时产生的事件。

²       依靠ListSelectionListener侦听事件(该接口在javax.swing.event包中,前面需要增加import该包)

²       通过addListSelectionListener(this)设置侦听

²       重写public void valueChanged(ListSelectionEvent e)方法,在改变选定项时被呼叫执行。

 

附复选框和单选按钮组成的“调查表”程序清单:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

 

class Favorite extends JPanel{

   JCheckBox c1,c2,c3,c4;

   Favorite(){

       c1=new JCheckBox("运动");

       c2=new JCheckBox("电脑");

       c3=new JCheckBox("音乐");

       c4=new JCheckBox("读书");

       add(new JLabel("爱好"));

       add(c1);add(c2);add(c3);add(c4);  

   }

}

 

class SexBox extends JPanel{

   JRadioButton r1,r2;

   SexBox(){

       r1=new JRadioButton("");

       r2=new JRadioButton("");

       add(new JLabel("性别"));

       ButtonGroup bg=new ButtonGroup();

       bg.add(r1);bg.add(r2);

       add(r1);add(r2);

   }

}

class NameBox extends JPanel{

   JTextField t;

   NameBox(){

       t=new JTextField(10);

       add(new JLabel("姓名"));

       add(t);

   }

}

class ThreeButton extends JPanel{

   JButton b1,b2,b3;

   ThreeButton(){

       b1=new JButton("List");

       b2=new JButton("Save");

       b3=new JButton("Exit");

       add(b1);add(b2);add(b3);

   }

}

 

class Test extends JFrame implements ActionListener{

   Favorite f;

   SexBox s;

   NameBox n;

   JTextArea t;

   ThreeButton tb;

   Test(){

       super("调查表");

       this.setDefaultCloseOperation(3);

       Container cc=this.getContentPane();

       cc.setLayout(new FlowLayout());

       testInit();

       cc.add(n);

       cc.add(s);

       cc.add(f);

       cc.add(new JScrollPane(t));

       cc.add(tb);

       this.setBounds(300,200,280,300);

       this.setVisible(true);

   }

   void testInit(){

       f=new Favorite();

       s=new SexBox();

       n=new NameBox();

       t=new JTextArea(5,22);

       tb=new ThreeButton();

       tb.b1.addActionListener(this);

       tb.b2.addActionListener(this);

       tb.b3.addActionListener(this); 

   }

   public void actionPerformed(ActionEvent e){

       Object o=e.getSource();

       if(o==tb.b1){

          StringBuffer ss=new StringBuffer("\n姓名:"+n.t.getText()+"\n性别:");

          if(s.r1.isSelected()==true)ss.append("");

          else if(s.r2.isSelected()==true)ss.append("");

          ss.append("\n爱好:");

          if(f.c1.isSelected()==true)ss.append("运动");

          if(f.c2.isSelected()==true)ss.append("  电脑");

          if(f.c3.isSelected()==true)ss.append("  音乐");

          if(f.c4.isSelected()==true)ss.append("  读书"); 

          t.setText(ss.toString());      

       }

       else if(o==tb.b2){

          try{

              FileWriter out=new FileWriter("temp.txt",true);

              out.write("\r\n"+t.getText());

              JOptionPane.showMessageDialog(this,"文件已保存!");

              out.close();

          }catch(Exception ee){}

       }

       else System.exit(0);    

   }

   public static void main(String[] aa){

       new Test();

   }

}

 

 

posted on 2007-07-21 10:56  丁保国  阅读(1236)  评论(0编辑  收藏  举报