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);
}
}

最新文章

  1. linux权限系统
  2. OpenStack 计算节点关机,虚拟机状态解决办法
  3. django-grappelli 安装配置
  4. nRF52系列——nRF52832来袭
  5. 在VHDL中,&ldquo;传输延迟&rdquo;和&ldquo;惯性延迟&rdquo;
  6. CSS3立体文字最佳实践
  7. Ping azure
  8. JSP EL表达式使用
  9. 201521123104 《JAVA程序设计》第二周学习总结
  10. HashMap HashTable和ConcurrentHashMap的区别
  11. zTree根据json选中节点,并且设置其他节点不可选
  12. mass
  13. Python【每日一问】03
  14. Android NDK编译之undefined reference to &#39;JNI_CreateJavaVM&#39;
  15. python构建bp神经网络_鸢尾花分类(一个隐藏层)__1.数据集
  16. Win32进程创建、进程快照、进程终止用例
  17. Java面向对象---接口
  18. 简话Angular 08 Angular ajax
  19. 四、java面向对象编程_2
  20. NerdTree 学习

热门文章

  1. Atcoder Grand Contest 038 E - Gachapon(Min-Max 容斥+背包)
  2. 『学了就忘』Linux文件系统管理 — 61、使用parted命令进行分区
  3. LeetCode一维数组的动态和
  4. 案例 stm32的dma传输过程
  5. LeetCode1579题——圆圈中最后剩下的数字
  6. [项目总结]关于调用系统照相机Activity被销毁问题解决
  7. redis入门到精通系列(三):key的通用操作和redis内部db的通用操作
  8. Redis数据类型内部编码规则及优化方式
  9. jenkins+Gitlab安装及初步使用
  10. maven常用命令(待补充)