【Java-GUI】07 Swing01 入门案例
Swing是Java自己开发出的一套GUI组件,不同于AWT去调用操作系统的GUI
正是因为非系统平台的GUI,所以程序运行的要慢一些
涉及的设计模式:MVC模式
Model(组件对象状态)
View(组件可视化表现)
Controller(组件的事件行为)
类体系:
java.awt.Component | java.awtContainer | java.swing.JComponent | java.swing.Jxxxx(各种组件的具体类)
演示案例:
package cn.dzz.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo01 {
JFrame jFrame = new JFrame("Swing");
JMenuBar jmenuBar = new JMenuBar();
JMenu fileJMenu = new JMenu("文件");
JMenu editJMenu = new JMenu("编辑");
JMenuItem autoNextLine = new JMenuItem("自动换行");
// JMenuItem copy = new JMenuItem("复制")
JMenuItem copy = new JMenuItem("复制", new ImageIcon("img/icon01.png"));
JMenuItem paste = new JMenuItem("粘贴", new ImageIcon("img/icon01.png"));
JMenu formatJMenu = new JMenu("格式");
JMenuItem comment = new JMenuItem("注释");
JMenuItem cancelComment = new JMenuItem("取消注释");
// 文本域
JTextArea jTextArea = new JTextArea(8,20);
// 列表框
String[] colorList = {
"红色",
"绿色",
"蓝色",
};
JList<String> jList = new JList<>(colorList);
// 下拉选择框
JComboBox<String> jComboBox = new JComboBox<>();
// 单选
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton male = new JRadioButton("男性", true);
JRadioButton female = new JRadioButton("女性", false);
// 复选框
JCheckBox isMarry = new JCheckBox("是否已婚", true);
// 底部
JTextField jTextField = new JTextField(20);
JButton ok = new JButton("确定", new ImageIcon("img/icon02.png"));
// 风格设置
JPopupMenu styleMenu = new JPopupMenu();
ButtonGroup styleGroup = new ButtonGroup();
JRadioButtonMenuItem metal = new JRadioButtonMenuItem("Metal-Style");
JRadioButtonMenuItem nimbus = new JRadioButtonMenuItem("Nimbus-Style");
JRadioButtonMenuItem windows = new JRadioButtonMenuItem("Windows-Style");
JRadioButtonMenuItem classicWindows = new JRadioButtonMenuItem("ClassicWindows-Style");
JRadioButtonMenuItem motif = new JRadioButtonMenuItem("Motif-Style");
public void init() {
JPanel bottomPanel = new JPanel();
bottomPanel.add(jTextField);
bottomPanel.add(ok);
jFrame.add(bottomPanel, BorderLayout.SOUTH);
JPanel selectPanel = new JPanel();
jComboBox.addItem("red");
jComboBox.addItem("green");
jComboBox.addItem("blue");
selectPanel.add(jComboBox);
buttonGroup.add(male);
buttonGroup.add(female);
selectPanel.add(male);
selectPanel.add(female);
selectPanel.add(isMarry);
Box leftBox = Box.createVerticalBox();
leftBox.add(jTextArea);
leftBox.add(selectPanel);
Box topBox = Box.createHorizontalBox();
topBox.add(leftBox);
topBox.add(jList);
jFrame.add(topBox);
// 顶部菜单设置
formatJMenu.add(comment);
formatJMenu.add(cancelComment);
editJMenu.add(autoNextLine);
editJMenu.addSeparator(); // 分割线
editJMenu.add(copy);
editJMenu.add(paste);
editJMenu.addSeparator(); // 分割线
editJMenu.add(formatJMenu);
jmenuBar.add(fileJMenu);
jmenuBar.add(editJMenu);
jFrame.setJMenuBar(jmenuBar);
styleMenu.add(metal);
styleMenu.add(nimbus);
styleMenu.add(windows);
styleMenu.add(classicWindows);
styleMenu.add(motif);
// 事件监听
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
try {
changeStyle(actionCommand);
} catch (Exception exception) {
exception.printStackTrace();
}
}
};
metal.addActionListener(actionListener);
nimbus.addActionListener(actionListener);
windows.addActionListener(actionListener);
classicWindows.addActionListener(actionListener);
motif.addActionListener(actionListener);
// 设置进按钮组中。表示只能选中一个选项
styleGroup.add(metal);
styleGroup.add(nimbus);
styleGroup.add(windows);
styleGroup.add(classicWindows);
styleGroup.add(motif);
jTextArea.setComponentPopupMenu(styleMenu);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
private void changeStyle(String command) throws Exception {
switch (command) {
case "Metal-Style":
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
break;
case "Nimbus-Style":
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
break;
case "Windows-Style":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
case "ClassicWindows-Style":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
break;
case "Motif-Style":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
break;
}
// 更新界面风格
SwingUtilities.updateComponentTreeUI(jFrame.getContentPane());
SwingUtilities.updateComponentTreeUI(jmenuBar);
SwingUtilities.updateComponentTreeUI(styleMenu);
}
public static void main(String[] args) {
new Demo01().init();
}
}
效果预览:

风格选择:

编辑菜单:


浙公网安备 33010602011771号