语雀笔记:https://www.yuque.com/huangzhanqi/rhwoir/paaoghdyv0tgksk1https://www.yuque.com/huangzhanqi/rhwoir/paaoghdyv0tgksk1Java图形化界面: Java图形化界面学习demo与资料 (gitee.com)https://gitee.com/zhanqi214/java-graphical-interface
Swing组件继承体系图:
大部分Swing 组件都是 JComponent抽象类的直接或间接子类(并不是全部的 Swing 组件),JComponent 类定义了所有子类组件的通用方法 ,JComponent 类是 AWT 里 java.awt. Container 类的子类 ,这也是 AWT 和 Swing 的联系之一。 绝大部分 Swing 组件类继承了 Container类,所以Swing 组件都可作为 容器使用 ( JFrame继承了Frame 类)。
Swing组件和AWT组件的对应关系:
大部分情况下,只需要在AWT组件的名称前面加个J,就可以得到其对应的Swing组件名称,但有几个例外:
Swing组件按照功能来分类:
Swing 为除 Canvas 之外的所有 AWT 组件提供了相应的实现,Swing 组件比 AWT 组件的功能更加强大。相对于 AWT 组件, Swing 组件具有如下 4 个额外的功能 :
每个 Swing 组件都有一个对应的UI 类,例如 JButton组件就有一个对应的 ButtonUI 类来作为UI代理 。每个 Swing组件的UI代理的类名总是将该 Swing 组件类名的 J 去掉,然后在后面添加 UI 后缀 。 UI代理类通常是一个抽象基类 , 不同的 PLAF 会有不同的UI代理实现类 。 Swing 类库中包含了几套UI代理,分别放在不同的包下, 每套UI代理都几乎包含了所有 Swing组件的 ComponentUI实现,每套这样的实现都被称为一种PLAF 实现 。以 JButton 为例,其 UI 代理的继承层次下图:
如果需要改变程序的外观风格, 则可以使用如下代码:
- //容器:
- JFrame jf = new JFrame();
-
- try {
-
- //设置外观风格
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
-
- //刷新jf容器及其内部组件的外观
- SwingUtilities.updateComponentTreeUI(jf);
- } catch (Exception e) {
- e.printStackTrace();
- }
案例:
使用Swing组件,实现下图中的界面效果:
演示代码:
- package swing;
-
- import awt.day01.Utils;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.InputEvent;
-
- public class SwingComponentDemo {
-
- private JFrame frame = new JFrame("Swimg测试");
- private JMenuBar jMenuBar = new JMenuBar();
- private JMenu fileMenu = new JMenu("文件");
- private JMenuItem openFile = new JMenuItem("打开文件");
- private JMenuItem newFile = new JMenuItem("新建文件");
- private JMenuItem saveFile = new JMenuItem("保存文件");
- private JMenuItem exit = new JMenuItem("退出",new ImageIcon("src/swing/img/component/exit.png"));
- private JMenu editMenu = new JMenu("编辑");
- private JCheckBoxMenuItem autoWrap = new JCheckBoxMenuItem("自动换行");
- private JMenuItem copy = new JMenuItem("复制",new ImageIcon("src/swing/img/component/copy.png"));
- private JMenuItem paste = new JMenuItem("粘贴",new ImageIcon("src/swing/img/component/paste.png"));
- private JMenu formate = new JMenu("格式");
- private JMenuItem annotation = new JMenuItem("注释");
- private JMenuItem deAnnotation = new JMenuItem("取消注释");
- private Box xBox = new Box(BoxLayout.X_AXIS);
- private Box yBox = new Box(BoxLayout.Y_AXIS);
- private Box vBox = new Box(BoxLayout.Y_AXIS);
- private JPanel panel1 = new JPanel();
- private JPanel panel2 = new JPanel();
- private String[] color = {"红色", "绿色", "蓝色"};
- private JComboBox
jComboBox = new JComboBox<>(color); - private JList
jList = new JList<>(color); - private JTextArea textArea = new JTextArea(8, 20);
- private JRadioButton male = new JRadioButton("男", true);
- private JRadioButton female = new JRadioButton("女", false);
- private ButtonGroup genderGroup=new ButtonGroup();
- private JCheckBox isMarried = new JCheckBox("是否已婚?", false);
- private JTextField textField = new JTextField(30);
- private JButton ok = new JButton("确定", new ImageIcon("src/swing/img/component/ok.png"));
- private JPopupMenu popupMenu = new JPopupMenu();
- private ButtonGroup styleGroup = new ButtonGroup();
- JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal 风格",true);
- JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus 风格",true);
- JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows 风格",true);
- JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("Windows 经典风格",true);
- JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif 风格",true);
- // JScrollPane scrollPane = new JScrollPane();
- public void init(){
- //菜单栏
- frame.setJMenuBar(jMenuBar);
- frame.setBackground(Color.GRAY);
- jMenuBar.add(fileMenu);
- jMenuBar.add(editMenu);
- fileMenu.add(openFile);
- fileMenu.add(newFile);
- fileMenu.add(saveFile);
- fileMenu.add(exit);
- editMenu.add(autoWrap);
- editMenu.add(copy);
- editMenu.add(paste);
- editMenu.add(formate);
- formate.add(annotation);
- formate.add(deAnnotation);
- annotation.setToolTipText("将程序代码注释起来");
- exit.addActionListener(e -> System.exit(0));
- newFile.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK));
- newFile.addActionListener(e -> textArea.append("用户点击了新建文件"));
- saveFile.setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_MASK));
- saveFile.addActionListener(this::textAreaClear);
- //X轴box组装
- frame.add(xBox);
- xBox.add(yBox);
- xBox.add(jList);
-
- //Y轴box组装
- yBox.add(vBox);
- vBox.add(textArea);
- vBox.add(panel1);
- vBox.add(panel2);
- panel1.add(jComboBox);
- genderGroup.add(male);
- genderGroup.add(female);
- panel1.add(male);
- panel1.add(female);
- panel1.add(isMarried);
- panel2.add(textField);
- panel2.add(ok);
-
- //右键菜单组装
- this.buildRightMenu();
-
-
- Utils.setJFrame(frame);
- }
-
- private void buildRightMenu() {
- textArea.add(popupMenu);
- popupMenu.add(metalItem);
- popupMenu.add(nimbusItem);
- popupMenu.add(windowsItem);
- popupMenu.add(classicItem);
- popupMenu.add(motifItem);
- styleGroup.add(metalItem);
- styleGroup.add(nimbusItem);
- styleGroup.add(windowsItem);
- styleGroup.add(classicItem);
- styleGroup.add(motifItem);
- //给组件设置右键菜单,不需要使用监听器,只需要调用setComponentPopupMenu()方法即可,更简单。
- textArea.setComponentPopupMenu(popupMenu);
- // textArea.add(scrollPane);
- metalItem.addActionListener(e -> {
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
- this.updateUI();
- } catch (ClassNotFoundException ex) {
- throw new RuntimeException(ex);
- } catch (InstantiationException ex) {
- throw new RuntimeException(ex);
- } catch (IllegalAccessException ex) {
- throw new RuntimeException(ex);
- } catch (UnsupportedLookAndFeelException ex) {
- throw new RuntimeException(ex);
- }
- });
- nimbusItem.addActionListener(e -> {
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
- this.updateUI();
- } catch (ClassNotFoundException ex) {
- throw new RuntimeException(ex);
- } catch (InstantiationException ex) {
- throw new RuntimeException(ex);
- } catch (IllegalAccessException ex) {
- throw new RuntimeException(ex);
- } catch (UnsupportedLookAndFeelException ex) {
- throw new RuntimeException(ex);
- }
- });
- windowsItem.addActionListener(e -> {
- try {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- this.updateUI();
- } catch (ClassNotFoundException ex) {
- throw new RuntimeException(ex);
- } catch (InstantiationException ex) {
- throw new RuntimeException(ex);
- } catch (IllegalAccessException ex) {
- throw new RuntimeException(ex);
- } catch (UnsupportedLookAndFeelException ex) {
- throw new RuntimeException(ex);
- }
- });
- classicItem.addActionListener(e -> {
- try {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
- this.updateUI();
- } catch (ClassNotFoundException ex) {
- throw new RuntimeException(ex);
- } catch (InstantiationException ex) {
- throw new RuntimeException(ex);
- } catch (IllegalAccessException ex) {
- throw new RuntimeException(ex);
- } catch (UnsupportedLookAndFeelException ex) {
- throw new RuntimeException(ex);
- }
- });
- motifItem.addActionListener(e -> {
- try {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
- this.updateUI();
- } catch (ClassNotFoundException ex) {
- throw new RuntimeException(ex);
- } catch (InstantiationException ex) {
- throw new RuntimeException(ex);
- } catch (IllegalAccessException ex) {
- throw new RuntimeException(ex);
- } catch (UnsupportedLookAndFeelException ex) {
- throw new RuntimeException(ex);
- }
- });
-
-
- }
-
- private void updateUI() {
- SwingUtilities.updateComponentTreeUI(frame.getContentPane());
- SwingUtilities.updateComponentTreeUI(jMenuBar);
- SwingUtilities.updateComponentTreeUI(popupMenu);
- }
-
- public static void main(String[] args) {
- new SwingComponentDemo().init();
- }
-
- private void textAreaClear(ActionEvent e) {
- textArea.setText("");
- }
- }