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

本文深入分析了JAVA GUI中的Swing组件库,对比了Swing与AWT的区别,指出Swing虽然运行速度较慢,但在功能扩展上的优势。Swing的类层次结构和典型程序结构被详细阐述,同时提供了一个简单的Swing程序示例,展示如何创建和使用Swing组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------------- 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();
			}
		}
	}
}
运行结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值