Swing 组件的应用实例

 

1.       概述

Swing组件是抽象窗口工具(AWT)库的一个扩展,用于建立更为复杂的GUI,它提供了比AWT更多的特性和工具。Javax.swing包提供了数量众多的接口、类和组件类等。它主要分为定时器类(javax.swing.Timer)、图标类(javax.swing.Icon)、按钮组类(java.swing.ButtonGroup)、快捷键类(javax.swing.KeyStroke),这几个类都是继承了java.lang.Object,还有组件类(java.swing.JComponent),它继承于(java.awt.Container),框架类(javax.swing.JFrame)继承于(java.awt.Frame,对话框类(javax.swing.JDialog)继承于(java.awt.Dialog,因此除了前四种,其余的Swing组件都是容器。

2.       实例(输入用户信息)

本类演示了网格布局的使用方法,演示了文本区、单选按钮,组合框等组件的使用方法。

程序的运行结果如下:

import java.awt.*;

import java.awt.event.*;

 

import javax.swing.*;

public class UserJFrame extends JFrame implements ActionListener,ItemListener

{

         private int number = 1;

         private JTextField textNumber,textName;

         private static Label NAME = new Label("姓名");

         private static Label NUMBER = new Label("编号");

         private static Label PROVINCE = new Label("省份");

         private static Label CITY = new Label("城市");

         private static Label SEX = new Label("性别");

         private JRadioButton buttonMale,buttonFemale;

         private JComboBox boxProvince,boxCity;

         private JButton buttonAdd;

         private JTextArea textUsers;

        

         public UserJFrame()

         {

                   super("输入用户信息");

                   this.setSize(360,200);

                   this.setLocation(300, 240);

                   this.setDefaultCloseOperation(EXIT_ON_CLOSE);

                   this.setLayout(new GridLayout(1,2));

                   textUsers = new JTextArea();

                   this.add(textUsers);

                   JPanel panel = new JPanel(new GridLayout(6,2));

                   this.add(panel);

                  

                   panel.add(NUMBER);

                   textNumber = new JTextField("1");

                   textNumber.setEditable(false);

                   panel.add(textNumber);

                  

                   panel.add(NAME);

                   textName = new JTextField("");

                   panel.add(textName);

                  

                   panel.add(SEX);

                   JPanel panelRadioButton = new JPanel(new GridLayout(1,2));

                   panel.add(panelRadioButton);

                   ButtonGroup buttonGroup = new ButtonGroup();

                   buttonMale = new JRadioButton("",true);

                   buttonGroup.add(buttonMale);

                   panelRadioButton.add(buttonMale);

                   buttonFemale = new JRadioButton("",false);

                   buttonGroup.add(buttonFemale);

                   panelRadioButton.add(buttonFemale);

                  

                  

                   panel.add(PROVINCE);

                   Object province[] = {"江苏省","浙江省"};

                   boxProvince = new JComboBox(province);

                   boxProvince.addItemListener(this);

                   panel.add(boxProvince);

                  

                   panel.add(CITY);

                   Object city[]= {"南京市","无锡市","苏州市"};

                   boxCity = new JComboBox(city);

                   panel.add(boxCity);

                  

                   buttonAdd = new JButton("添加");

                   buttonAdd.addActionListener(this);

                   panel.add(buttonAdd);

                   this.setVisible(true);

         }

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

                   new UserJFrame();

         }

 

         @Override

         public void actionPerformed(ActionEvent e) {

                   // TODO Auto-generated method stub

                   String aline="";

                   aline = number + ". " + textName.getText();

                   if(buttonMale.isSelected())

                            aline += "," + buttonMale.getText();

                   if(buttonFemale.isSelected())

                            aline += "," + buttonFemale.getText();

                   aline += "," + boxProvince.getSelectedItem();

                   aline += boxCity.getSelectedItem();

                   textUsers.append(aline + '/n');

                   this.number++;

                   textNumber.setText(""+this.number);

         }

 

         @Override

         public void itemStateChanged(ItemEvent e) {

                   // TODO Auto-generated method stub

                   switch(boxProvince.getSelectedIndex())

                   {

                   case 0:{

                            boxCity.removeAllItems();

                            boxCity.addItem("南京市");

                            boxCity.addItem("苏州市");

                            boxCity.addItem("无锡市");

                   };break;

                   case 1:{

                            boxCity.removeAllItems();

                            boxCity.addItem("杭州市");

                            boxCity.addItem("宁波市");

                            boxCity.addItem("温州市");

                   };break;

                   default:;break;

                   }

         }

}

3.       总结

在本类中,简要介绍了Swing组件的使用方法,利用Swing组件可以完全代替AWT组件,且比AWT组件具有更丰富的特性,建议使用Swing组件替代AWT组件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值