JAVA进行GUI程序设计之一
2010-12-14 21:25
253 查看
近期正在学习Java的Swing编程,所以打算写一个关于Swing编程的系列文章,今天先写个开篇。
一般来说,使用Java进行GUI程序设计流程如下:
1. 引入Swing等相关的包;
2. 选择观感(LookAndFeel);
3. 设置顶层容器;
4. 声明和创建GUI组件;
5. 设置GUI组件
6. 添加边框;
7. 向容器中添加组件及管理布局;
8. 进行事件处理。
搬一个例子上来,先来张最终效果预览图,如下:
源码:
一般来说,使用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); } }
相关文章推荐
- JAVA进行GUI程序设计之布局管理(中)
- JAVA进行GUI程序设计之布局管理(上)
- JAVA进行GUI程序设计之布局管理(下)
- Java学习13 GUI程序设计(Swing)
- 用Java进行GUI设计,出现对必需的库 rt.jar 具有一定限制,因此无法访问类型 JButton等问题
- Eclipse进行JavaGUI开发的环境配置
- java 图形程序设计(GUI)
- Java GUI程序设计的初步上
- Java GUI编程中如何进行事件处理机制
- 一个地址薄的小程序,JAVA GUI界面,对文件进行随机读写。
- 使用RMI和CORBA进行分布式java程序设计
- 使用JD-GUI进行java反编译
- JAVA程序设计(15.2)----- 图形用户界面GUI 制作正玄曲线图
- Java进行Gui开发过程
- eclipse GUI 项目: visual editor 安装 (用Eclipse进行可视化Java界面设计)
- java学习笔记图形程序设计及GUI应用小结(1)
- Java程序设计11——GUI设计与事件处理B
- java -- GUI程序设计之无布局管理器布局
- 课程 Java程序设计10:Building Java GUI (怀旧版)
- Java GUI程序设计的初步上