首页 技术 正文
技术 2022年11月18日
0 收藏 804 点赞 4,512 浏览 5893 个字

Swing介绍:

使用Swing开发图形界面比AWT更加优秀,因为Swing是一种轻量级组件,它采用100% Java实现,不再依赖于本地平台的图形界面,所以可以在所有平台上保持相同的运行效果,对跨平台支持比较出色,实现了真正的一次编写处处运行。除此之外,Swing提供了比AWT更多的图形界面组件,因此可以开发出更美观的图形界面。

Swing组件的基本使用

大部分Swing组件都是JComponent抽象类的直接或间接子类,JComponent类定义了所有子类组件的通用方法。JComponent类是AWT中java.awt.Container类的子类,这也是AWT和Swing的联系之一。绝大部分Swing组件类继承了Container类,所以Swing组件都可作为容器使用。

Swing的继承关系结构如下:

上面的深色部分都是可以在AWT找到对应组件的。比如我们的JButton就和我们Awt中的Button很相似。这些和Awt有关联的组件就是在原来Awt组件的名字前面加了一个J。但是还是有几个例外:

  • JComboBox: 对应AWT里的Choice组件,但比Choice组件功能更丰富
  • JFileChooser: 应于AWT里的FileDialog组件
  • JScrollBar: 对应于AWT里的Scrollbar组件,注意两个组件类名中 字母的大小写差别。
  • JCheckBox 对应于AWT里的checkbox组件,注意两个组件类名中 字母的大小写差
  • JCheckBoxMenuitem: 对应于AWT里的CheckboxMenuItem 组件,注意两个组件类名中字母的大小写差别。

在AWT中的东西,除了Canvas之外,别的所有的Awt组件都提供了相应的实现,Swing组件比AWT组件的功能更加强大。相对于AWT组件,Swing组件具有如下4个额外功能。

  • (1),可以为Swing组件设置提示信息。用setToolTipText()方法。
  • (2),很多Swing组件,比如我们的按钮,标签,菜单项等,除了使用文字之外,还可以用图标来修饰自己。Swing为Icon接口提供了一个实现类:ImageIcon
  • (3),支持插拔式的外观风格。
  • (4),支持设置边框

示例程序截图:

示例代码:

package swingtest;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;/**
* @ClassName SwingComponent
* @projectName: object1
* @author: Zhangmingda
* @description: XXX
* date: 2021/5/8.
*/
public class SwingComponent {
public static void main(String[] args) {
JFrame jFrame = new JFrame("Swing基本组件");
//菜单栏
JMenuBar jMenuBar = new JMenuBar();
//菜单项
JMenu fileMenu = new JMenu("文件");
JMenu editMenu = new JMenu("编辑");
//菜单子项
JMenuItem newMenuItem = new JMenuItem("新建",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
JMenuItem saveMenuItem = new JMenuItem("保存",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
JMenuItem exitMenuItem = new JMenuItem("退出",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
fileMenu.add(newMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(exitMenuItem);
//编辑菜单子项
//菜单子项
JMenuItem autoLenMenuItem = new JMenuItem("自动换行",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
JMenuItem copyMenuItem = new JMenuItem("复制",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
JMenuItem pasteMenuItem = new JMenuItem("粘贴",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
JMenuItem noteMenuItem = new JMenuItem("注释",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
JMenuItem cancleNoteMenuItem = new JMenuItem("取消注释",new ImageIcon("图形化编程/src/swingtest/ico/new.png"));
JMenuItem formatJMenuItem = new JMenu("格式");
formatJMenuItem.add(noteMenuItem);
formatJMenuItem.add(cancleNoteMenuItem);
editMenu.add(autoLenMenuItem);
editMenu.addSeparator();//换行符
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
editMenu.addSeparator();//换行符
editMenu.add(formatJMenuItem);
//菜单栏添加菜单
jMenuBar.add(fileMenu);
jMenuBar.add(editMenu);
jFrame.setJMenuBar(jMenuBar);
//底部菜单
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JTextField(30));
bottomPanel.add(new JButton("确定"));
jFrame.add(bottomPanel,BorderLayout.SOUTH);
/**
* 中间部分
*/
Box centerBox = Box.createHorizontalBox(); //水平盒子
/**
* 中间左
*/
Box centerLeftBox = Box.createVerticalBox(); //垂直盒子
/**
* 中间左上
*/
JTextArea jTextArea = new JTextArea(8, 40); //文本框
//带滚动条的容器,创建时传入一个文本框
JScrollPane jScrollPane = new JScrollPane(jTextArea);
centerLeftBox.add(jScrollPane); //中间左边的垂直盒子添加 带滚动条的容器
/**
* 中间左下
*/
JPanel leftBottomPanel = new JPanel();
//复选框
leftBottomPanel.add(new JComboBox<String>(new String[]{"红色","绿色", "蓝色"})); //复选框
//单选框、
JRadioButton male = new JRadioButton("男",true);
JRadioButton female = new JRadioButton("女");
ButtonGroup sexGroup = new ButtonGroup();
sexGroup.add(male); //单选组添加选项
sexGroup.add(female); //单选组添加选项
leftBottomPanel.add(male);leftBottomPanel.add(female); //左下容器添加选项
//勾选框
leftBottomPanel.add(new JCheckBox("婚否"));
//中间左添加中间左下
centerLeftBox.add(leftBottomPanel);
/**
* 中间右
*/
Box centerRightBox = Box.createVerticalBox(); //后边的垂直盒子
centerRightBox.add(new JList<String>(new String[]{"红色", "粉色","黄色"}));
/**
* 水平容器添加左边盒子,窗口添加水平容器
*/
centerBox.add(centerLeftBox);// 水平盒子添加左边的盒子
centerBox.add(centerRightBox);// 水平盒子添加右边的盒子
jFrame.add(centerBox); // 窗口添加水平盒子
/**
* 右键菜单
*/
JPopupMenu popupMenu = new JPopupMenu();
//右键单选框菜单项
JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal风格", true);
JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus风格");
JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows风格");
JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("Windows经典风格");
JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif风格");
ButtonGroup flavorGroup = new ButtonGroup();//单选的菜单项
flavorGroup.add(nimbusItem);
flavorGroup.add(metalItem);
flavorGroup.add(windowsItem);
flavorGroup.add(classicItem);
flavorGroup.add(motifItem);
popupMenu.add(motifItem);
popupMenu.add(classicItem);
popupMenu.add(windowsItem);
popupMenu.add(nimbusItem);
popupMenu.add(metalItem);
//右键菜单鼠标单击监听器
ActionListener flavorListener = e -> {
try {
switch (e.getActionCommand()) {
// 设置Metal风格
case "Metal风格":
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
break;
// 设置Nimbus风格
case "Nimbus风格":
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
break;
// 设置Windows风格
case "Windows风格":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
// 设置Windows经典风格
case "Windows经典风格":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
break;
// 设置Motif风格
case "Motif风格":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
break;
}
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
//更新各个组件的风格,jframe.getContentPane()就是获取frame的各个组件
SwingUtilities.updateComponentTreeUI(jFrame.getContentPane());
SwingUtilities.updateComponentTreeUI(jMenuBar);
SwingUtilities.updateComponentTreeUI(popupMenu);
}
};
//右键菜单绑定监听器
metalItem.addActionListener(flavorListener);
nimbusItem.addActionListener(flavorListener);
windowsItem.addActionListener(flavorListener);
classicItem.addActionListener(flavorListener);
motifItem.addActionListener(flavorListener);
//文本框设置右键菜单
jTextArea.setComponentPopupMenu(popupMenu); //关闭窗口自动退出程序
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setLocation(400,300);
jFrame.setVisible(true);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,961
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,485
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,746
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,780