JAVA进行GUI程序设计之一

近期正在学习Java的Swing编程,所以打算写一个关于Swing编程的系列文章,今天先写个开篇。 一般来说,使用Java进行GUI程序设计流程如下: 1. 引入Swing等相关的包; 2. 选择观感(LookAndFeel); 3. 设置顶层容器; 4. 声明和创建GUI组件; 5. 设置GUI组件 6. 添加边框; 7. 向容器中添加组件及管理布局; 8. 进行事件处理。 搬一个例子上来,先来张最终效果预览图,如下:

源码:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Aiml
 *
 */
public class SwingDemo extends SimpleFrame {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private JPanel topPanel=new JPanel();
	private Box box=Box.createVerticalBox();
	private JButton button=new JButton("OK");
	private JCheckBox checkBox=new JCheckBox("少数民族");
	private JRadioButton
					rb1=new JRadioButton("男"),
					rb2=new JRadioButton("女"),
					rb3=new JRadioButton("不详",true);
	private ButtonGroup buttonGroup=new ButtonGroup();
	private JComboBox comboBox=new JComboBox(degrees);
	private JList list=new JList(educationalBackground);
	private JTextArea textArea=new JTextArea();
	private JLabel nameLabel=new JLabel("姓名:");
	private JTextField nameText=new JTextField("张三");

	private static String[] degrees={"无","学生","硕士","工程硕士","MBA","博士"};
	private static String[] educationalBackground={"小学","初中","高中","大学大专","大学本科","硕士研究生","博士研究生"};

	//Create a new instance of SwingDemo
	public SwingDemo(int width,int height){

		//设置顶层容器
		super(width, height);
		setTitle("演示GUI容器、组件、事件及布局");

		//JPanel容器采用默认的FlowLayout布局
		topPanel.add(nameLabel);
		topPanel.add(nameText);
		topPanel.add(new JLabel("学历"));
		topPanel.add(comboBox);

		//设置互斥按钮组
		buttonGroup.add(rb1);
		buttonGroup.add(rb2);
		buttonGroup.add(rb3);

		//添加边框
		list.setBorder(BorderFactory.createTitledBorder("文化程度:"));
		textArea.setBorder(BorderFactory.createTitledBorder("演示效果:"));

		//设置背景色
		textArea.setBackground(new Color(200,220,180));

		//Box容器采用默认的BoxLayout布局
		box.add(new JLabel("性别:"));
		box.add(rb1);
		box.add(rb2);
		box.add(rb3);
		box.add(checkBox);

		//JFrame容器采用默认的BorderLayout布局
		Container c=this.getContentPane();
		c.add(topPanel,"North");
		c.add(list,"East");
		c.add(box,"West");
		c.add(button,"South");
		c.add(new JScrollPane(textArea),"Center");

		//演示JList组件选择列表项事件
		list.addListSelectionListener(new ListSelectionListener(){
			@Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				if(e.getValueIsAdjusting())return;
				textArea.append("\n"+(String)(list.getSelectedValue()));
			}
		});

		//演示JCheckBox组件单击事件
		checkBox.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String c=" 未选";
				if(checkBox.isSelected())
					c=" 已选";
				textArea.append("\n"+(String)(checkBox.getText()+c));
			}
		});

		//演示JComboBox组件选择列表项事件
		comboBox.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				textArea.append("\n"+(String)(comboBox.getSelectedItem()));
			}
		});

		//演示JButton组件点击事件
		button.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null, "演示消息框。",button.getText(),JOptionPane.INFORMATION_MESSAGE);
			}
		});

		//演示JTextField组件文本改变事件
		nameText.getDocument().addDocumentListener(
			new DocumentListener(){

				@Override
				public void changedUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void insertUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					textArea.append("\n"+nameText.getText());
				}

				@Override
				public void removeUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					textArea.append("\n"+nameText.getText());
				}
			}
		);

		//演示JradioButton组件单击事件
		rb1.addActionListener(rbListener);
		rb2.addActionListener(rbListener);
		rb3.addActionListener(rbListener);
	}

	//演示3个JradioButton组件注册的一个公用监听器
	private ActionListener rbListener=new ActionListener(){
		public void actionPerformed(ActionEvent e){
			textArea.append("\n"+((JRadioButton)e.getSource()).getText());
		}
	};

	public static void main(String args[]){
		try{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}catch(Exception e){
			System.out.println("程序异常:"+e.getMessage());
		}
		SwingDemo frame = new SwingDemo(500,400);
		frame.setVisible(true);
	}
}
posted @ 2010-12-14 21:25  SillyCoder  阅读(330)  评论(0编辑  收藏  举报