黑马程序员----JAVA GUI 之 Swing

来源:互联网 发布:mac如何下载土豆视频 编辑:程序博客网 时间:2024/06/09 15:48

---------------------- android培训 、 java培训 、 期待与您交流! ------------------

1、上篇我们讨论了JAVA GUI 之AWT,这篇来讨论下Swing。在学习Swing之前,我们先来看下他们的不同:

AWT和Swing之间的基本区别:AWT 是基于本地方法的C/C++程序,其运行速度比较快;Swing 是在AWT的基础上构建的一套新的图形界面系统,它提供了AWT 所能够提供的所有功能,并且用纯粹的Java代码对AWT 的功能进行了大幅度的扩充其运行速度比较慢,。对于一个嵌入式应用来说,目标平台的硬件资源往往非常有限,而应用程序的运行速度又是项目中至关重要的因素。在这种矛盾的情况下,简单而高效的AWT 当然成了嵌入式Java的第一选择。而在普通的基于PC或者是工作站的标准Java应用中,硬件资源对应用程序所造成的限制往往不是项目中的关键因素,所以在标准版的Java中则提倡使用Swing, 也就是通过牺牲速度来实现应用程序的功能。

通俗的说就是AWT 是抽象窗口组件工具包,是 java 最早的用于编写图形节目应用程序的开发包。Swing是为了解决 AWT 存在的问题而新开发的包,它以 AWT 为基础的。


2、Swing的类层次结构:

 Swing包的组成内容Com.sum.swing.plaf.motif实现Motif界面样式Com.sum.java.swing.plaf.windows 实现Windows界面样式javax.swingSwing组件和使用工具 javax.swing.border Swing轻量组件的边框javax.swing.colorchooserJcolorChooser的支持类/接口javax.swing.event事件和侦听器类javax.swing.filechooserJFileChooser的支持类/接口javax.swing.plaf抽象类,定义UI代表的行为 javax.swing.plaf.basic实现所有标准界面样式公共基类javax.swing.plaf.metal实现Metal界面样式代表类javax.swing.tableJtable组件javax.swing.text支持文档的显示和编辑javax.swing.text.htmlHTML文档的分析器 javax.swing.text.html.parserHTML文档的分析器javax.swing.text.rtf支持显示和编辑RTF文件javax.swing.treeJtree组件的支持类javax.swing.undo支持取消操作

类的继承关系如下:


3、Swing程序结构简介:

首先,创建顶层容器;然后再容器中创建按钮和标签等组件,并将组件添加到容器中;然后在组件的周围添加边界;最后对组件的事情进行处理。

4、Swing的小例子:

(1)、

import java.awt.*;import javax.swing.*;import java.awt.event.*;//该类作为事件监听者,需要实现对应的接口public class JTextFieldDemo extends JFrame implements ActionListener {private JLabel lb1, lb2;private JTextField t1, t2;public JTextFieldDemo() {this.setLayout(new FlowLayout()); //设置布局管理lb1 = new JLabel("请输入一个正整数:");// 创建标签对象字符串为提示信息lb2 = new JLabel("1到该数的和为:");// 创建标签对象字符串为提示信息t1 = new JTextField(10);// 创建输入文本框,最多显示10个字符t2 = new JTextField(10);this.add(lb1); // 将组件添加到窗口上this.add(t1);this.add(lb2);this.add(t2);t1.addActionListener(this);// 为文本框注册ActionEvent事件监听器// 为窗口注册窗口事件监听程序,监听器以匿名类的形式进行this.addWindowListener(new WindowAdapter() {// 匿名类开始public void windowClosing(WindowEvent e){System.exit(0);} // 窗口关闭});// 匿名类结束this.setTitle("JTextField示例");//设置窗体标题this.setSize(600, 450);//设置窗口大小this.setVisible(true);//设置窗体的可见性}public void actionPerformed(ActionEvent e) { // ActionListener接口中方法的实现// getText()获取文本框输入的内容,转换为整型数值int n = Integer.parseInt(t1.getText());int sum = 0;for (int i = 1; i <= n; i++)sum=sum+ i;t2.setText(String.valueOf(sum)); // 修改文本框输出内容}public static void main(String[] arg) {new JTextFieldDemo();}}
运行结果如下:

2、

package swing;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class MainFrame extends JFrame {Container container;public MainFrame() {this.setTitle("欢迎使用图书管理系统 ");// 设置标题container = this.getContentPane(); // 获取默认的内容窗格container.setLayout(new BorderLayout()); // 设置布局格式JMenuBar menuBar = new JMenuBar(); // 创建菜单栏buildMainMenu(menuBar); // 最定义组建菜单的方法this.setJMenuBar(menuBar); // 把菜单栏挂到该窗口上this.show(true); // 显示窗口this.setSize(600, 450); // 设置窗口大小}/* 构建菜单,由于篇幅有限,创建三个菜单文件、图书信息查询和帮助菜单 */protected void buildMainMenu(JMenuBar menuBar) {JMenu fileMenu = new JMenu("文件(F)"); // 文件菜单的创建JMenuItem exitMenuItem = new JMenuItem("退出");/* 为事件注册,对ActionEvent事件作出处理 */exitMenuItem.addActionListener(new ExitActionListener());fileMenu.add(exitMenuItem); // 把退出菜单项添加到菜单上menuBar.add(fileMenu);// 文件菜单添加到菜单栏上/** 建立图书信息查询菜单* */JMenu libMenu = new JMenu("馆藏检索(B)");libMenu.setMnemonic(KeyEvent.VK_B);// 给图书检索菜单定义助记键JMenuItem libMenuItem = new JMenuItem("书目检索");JMenuItem myBorrowMenuItem = new JMenuItem("我的借阅");libMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));// 设定快捷键// 为事件注册,BookInLibraryActionListener为内部类libMenuItem.addActionListener(new BookInLibraryActionListener());myBorrowMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,ActionEvent.CTRL_MASK));// 设定快捷键myBorrowMenuItem.addActionListener(new MyBorrowActionListener());libMenu.add(libMenuItem); // 把菜单项添加到查询菜单上libMenu.add(myBorrowMenuItem);menuBar.add(libMenu); // 查询菜单添加到菜单栏上/* 帮助菜单的创建 */JMenu helpMenu = new JMenu("帮助(H)");helpMenu.setMnemonic(KeyEvent.VK_H);//给帮助菜单定义助记键JMenuItem aboutMenuItem = new JMenuItem("关于");aboutMenuItem.setMnemonic(KeyEvent.VK_A); aboutMenuItem.addActionListener(new AboutActionListener());// 为事件注册helpMenu.add(aboutMenuItem); // 把关于菜单项添加到帮助菜单上menuBar.add(helpMenu); // 帮助菜单添加到菜单栏上}/* libMenuItem事件源的事件监听器,打开例8-16添加Jlist后的窗口 */class BookInLibraryActionListener implements ActionListener {public void actionPerformed(ActionEvent event) {new JComboBoxDemo();}}/* 我的借阅的事件监听器,打开例8-16添加JTable后的窗口 */class MyBorrowActionListener implements ActionListener {public void actionPerformed(ActionEvent event) {new JRadioButtonDemo();}}/* 帮助菜单中关于菜单项的事件监听者 */class AboutActionListener implements ActionListener {public void actionPerformed(ActionEvent event) {/* 暂时为空后加对话框 */}}/* 文件菜单中退出菜单项的事件监听者 */class ExitActionListener implements ActionListener {public void actionPerformed(ActionEvent event) {dispose();System.exit(0);}}public static void main(String[] args) {new MainFrame();}}class JRadioButtonDemo extends JFrame {protected JPanel topPanel;private Container container;public JRadioButtonDemo() {container = this.getContentPane();//获取内容窗格topPanel = new JPanel();topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));// 设置边框文本提示信息topPanel.setBorder(BorderFactory.createTitledBorder("借阅查询选项"));JRadioButton currBorrowButton = new JRadioButton("当前借阅");JRadioButton oldBorrowButton = new JRadioButton("历史借阅");topPanel.add(currBorrowButton); //添加组件到面板容器topPanel.add(oldBorrowButton); //添加组件到面板容器// 注册事件监听程序,对ActionEvent事件作出处理currBorrowButton.addActionListener(new CurrentBorrowInfoListener());oldBorrowButton.addActionListener(new OldBorrowInfoListener());/** 将2个RadioButton对象放进ButtonGroup中,以实现二选一 */ButtonGroup buttonGroup1 = new ButtonGroup();buttonGroup1.add(currBorrowButton);buttonGroup1.add(oldBorrowButton);this.add(BorderLayout.NORTH, topPanel); //把面板容器添加到内容窗格上this.setTitle("图书管理系统我的借阅"); //设置标题this.setSize(600, 450);//设置大小this.setVisible(true);//设置可见性}class CurrentBorrowInfoListener implements ActionListener {public void actionPerformed(ActionEvent event) {// 把当前借阅信息从数据库取出,以表格的形式显示出来,代码实现见13章}}class OldBorrowInfoListener implements ActionListener {public void actionPerformed(ActionEvent event) { // 把历史借阅信息从数据库取出,以表格的形式显示出来,代码实现见13章}}}class JComboBoxDemo extends JFrame {protected JLabel selectionLabel;protected JComboBox fieldComboBox;protected JPanel topPanel;protected JButton retrievalButton;protected JTextField keywordText;private Container container;protected String fieldSelected;public JComboBoxDemo() {container = this.getContentPane();selectionLabel = new JLabel("检索方式"); // 标签fieldComboBox = new JComboBox(); // 分类检索下拉列表fieldComboBox.addItem("请选择...");fieldComboBox.addItem("书名");fieldComboBox.addItem("ISBN号");fieldComboBox.addItem("作者");fieldComboBox.addItem("出版");//注册事件监听者FieldSelectedListener为内部类fieldComboBox.addItemListener(new FieldSelectedListener());keywordText = new JTextField("java", 20); // 显示检索关键字的文本框retrievalButton = new JButton("检索"); //提交命令按钮topPanel = new JPanel();topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));keywordText.setSize(topPanel.getWidth() / 2, topPanel.getWidth());topPanel.add(selectionLabel);topPanel.add(fieldComboBox);topPanel.add(keywordText);topPanel.add(retrievalButton);//添加后面的代码JList显示检索内容this.add(BorderLayout.NORTH, topPanel);this.setTitle("图书管理系统图书查询");this.setSize(600, 450);this.setVisible(true);}class FieldSelectedListener implements ItemListener {public void itemStateChanged(ItemEvent event) {if (event.getStateChange() == ItemEvent.SELECTED) {fieldSelected = (String) fieldComboBox.getSelectedItem();}}}}
运行结果如下:


0 0