前言 Java通过Swing实现GUI Swing是基于AWT 构建的新图形库,是AWT的扩展
MacOS UI 1 2 3 4 5 6 7 static { try { UIManager.setLookAndFeel(new MetalLookAndFeel ()); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } }
窗口 创建窗口对象 1 JFrame frame = new JFrame ("窗口标题" );
设置默认关闭操作 1 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
组件
Swing中组件默认位置为不包含 窗口标题的窗口左上角
面板 1 2 JPanel panel = new JPanel ();frame.add(panel);
标签 1 2 JLabel label = new JLabel ("标签" );frame.add(label);
按钮 1 2 JButton button = new JButton ("按钮" );frame.add(button);
文本框 1 2 JTextField textField = new JTextField ("默认值" );frame.add(textField);
文本域 1 2 JTextArea textArea = new JTextArea ("默认值" );frame.add(textArea);
选框 1 2 JCheckBox checkBox = new JCheckBox ("选框" );frame.add(checkBox);
列表 1 2 JList list = new JList (new String []{"列表项1" , "列表项2" });frame.add(list);
菜单栏
Swing中菜单栏永远在应用内,而不是由操作系统决定
1 2 JMenuBar menuBar = new JMenuBar ();frame.setJMenuBar(menuBar);
菜单栏添加菜单 1 2 JMenu menu = new JMenu ("菜单" );menuBar.add(menu);
菜单添加菜单项 1 2 JMenuItem menuItem = new JMenuItem ("菜单项" );menu.add(menuItem);
菜单项添加快捷键 1 menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl a" ));
弹出菜单 1 2 JPopupMenu popupMenu = new JPopupMenu ();frame.add(popupMenu);
对话框 1 2 JDialog dialog = new JDialog ();dialog.setVisible(true );
进度条 1 2 3 4 JProgressBar progressBar = new JProgressBar ();progressBar.setMaximum(100 ); progressBar.setValue(50 ); frame.add(progressBar);
立即重新渲染进度
开关按钮 1 2 JToggleButton toggleButton = new JToggleButton ("开关" );frame.add(toggleButton);
设置开关状态
1 toggleButton.setSelected(true );
获取开关状态 1 boolean isSelected = toggleButton.isSelected();
颜色选择器 1 2 JColorChooser colorChooser = new JColorChooser ();frame.add(colorChooser);
获取颜色 1 Color color = colorChooser.getColor();
文件选择器 1 2 JFileChooser fileChooser = new JFileChooser ();fileChooser.showOpenDialog(frame);
工具提示
1 button.setToolTipText("工具提示" );
文件树 1 2 3 4 5 6 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode ("" );DefaultMutableTreeNode sonNode = new DefaultMutableTreeNode ("" );rootNode.add(sonNode); JTree tree = new JTree (rootNode);frame.add(tree);
多面板 1 2 3 4 5 6 7 JPanel panel1 = new JPanel ();JPanel panel2 = new JPanel ();JTabbedPane tabbedPane = new JTabbedPane ();tabbedPane.addTab("标签1" , panel1); tabbedPane.addTab("标签2" , panel2); frame.add(tabbedPane);
分割面板 上下分割 1 2 3 4 5 6 7 8 JPanel panel1 = new JPanel ();JPanel panel2 = new JPanel ();JSplitPane splitPane = new JSplitPane ();splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); splitPane.setTopComponent(panel1); splitPane.setBottomComponent(panel2); frame.add(splitPane);
左右分割 1 2 3 4 5 6 7 8 JPanel panel1 = new JPanel ();JPanel panel2 = new JPanel ();JSplitPane splitPane = new JSplitPane ();splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT); splitPane.setLeftComponent(panel1); splitPane.setRightComponent(panel2); frame.add(splitPane);
弹窗 确认弹窗
JOptionPane.DEFAULT_OPTION、-1:只有确定按钮JOptionPane.YES_NO_OPTION、0:包含是、否按钮JOptionPane.YES_NO_CANCEL_OPTION、1:包含是、否、取消按钮JOptionPane.OK_CANCEL_OPTION、2:包含确定、取消按钮
1 int result = JOptionPane.showConfirmDialog(frame, "提示信息" , "标题" , JOptionPane.YES_NO_CANCEL_OPTION);
JOptionPane.CLOSED_OPTION、-1:关闭JOptionPane.YES_OPTION、JOptionPane.OK_OPTION、0:是、确定JOptionPane.NO_OPTION、1:否JOptionPane.CANCEL_OPTION、2:取消
提示弹窗
JOptionPane.ERROR_MESSAGE、0:错误JOptionPane.INFORMATION_MESSAGE、1:提示JOptionPane.WARNING_MESSAGE、2:警告JOptionPane.QUESTION_MESSAGE、3:问题JOptionPane.PLAIN_MESSAGE、-1:空白
1 JOptionPane.showMessageDialog(frame, "提示信息" );
1 JOptionPane.showMessageDialog(frame, "提示信息" , "标题" , JOptionPane.INFORMATION_MESSAGE);
输入弹窗
1 String result = JOptionPane.showInputDialog(frame, "提示信息" );
主题
new MetalLookAndFeel():缺省值,默认主题new WindowsLookAndFeel():Windows主题,仅限Windowsnew AquaLookAndFeel():MacOS主题,仅限MacOSnew MotifLookAndFeel()new NimbusLookAndFeel()
1 UIManager.setLookAndFeel(new MetalLookAndFeel ());
完成 参考文献 哔哩哔哩——青空の霞光