Swing01-概述
1.Swing概述
Swing百分之百由Java本身实现,是一套轻量级组件(完全由Java实现的组件叫做轻量级套件,依赖于本地平台的套件称之为重量级套件)。Swing不再依赖于平台的GUI,因此真正做到了平台外观的一致性。
但也因为Swing不再依赖平台,所以它的速度比AWT要慢。
2.Swing的优势
1.Swing使用MVC(Module-View-Controller,即模型-视图-控制器)设计模式。
模型(Module):用于维护组件的各种状态;
视图(View):是组件可视化的表现;
控制器(Controller):用于控制对各种事件、组件做出响应。
当模型发生改变时,它会通知所有依赖它的视图,视图会根据模型数据来更新自己。Swing使用UI代理来包装视图和控制器,还有一个模型对象来维护该组件的状态。例如,按钮JButton有一个维护其状态信息的模型ButtonModel对象。Swing组件的模型是自动设置的,因此可以直接使用JButton而无需关心ButtonModule。
2.Swing在各平台显示一致。
3.Swing可以使用外观插件。
3.Swing组件层次
对应关系
1.Swing中大部分直接或者间接继承自Java.awt.Container,因此既可以作为组件也可以作为容器。
2.Swing中大部分组件都是直接在awt组件名前加一个J,但也有几个需要注意的:
-JComboBox:对应awt中的Choice,但是功能更强。
-JFileChooser:对应awt中的FileDialog
-JScrollBar:对应awt中的Scrollbar
-JCheckBox:对应awt中的Checkbox
-JCheckBoxMenuItem:对应awt中的CheckboxMenuItem
功能层次
1.顶层容器:JFrame、JWindow、JDialog、JApplet
2.中间容器:JPanel、JScrollPane、JSplitPane、JToolBar等。
3.特殊容器:在用户界面上具有特殊作用的中间容器,如JIntemalFrame、JRootPane、JLayeredPane、JDesktopPane等。
4.基本组件:实现人机交互的组件,如JButton、JComboBox、JList、JMenu等。
5.不可编辑信息的显示组件:向用户显示不可编辑信息的组件,如JLabel、JProgressBar、JToolTip等。
6.可编辑信息的显示组件:向用户显示能被编辑的格式化信息的组件,如JTable、JTextArea、JTextField等。
7.特殊对话框组件:可以直接显示特殊对话框的组件,如JColorChooser和JFileChooser等。
4.AWT组件的Swing实现
Swing为除Canvas之外的所有AWT组件都提供了实现,Swing组件比AWT组件功能更加强大,相对于AWT组件,Swing组件有4个额外功能。
1.可以为Swing组件设置提示信息,使用setToolTipText()方法,为组件设置对用户有帮助的提示信息。
2.很多Swing组件,如按钮、标签、菜单项等,除了可以使用文字,还可以使用图标。为了允许使用图标,Swing为Icon接口提供了一个实现类ImageIcon,该实现类代表一个图像图标。
3.支持插拔式的外观风格。每一个JComponent对象都有一个ComponentUI对象,为它完成所有绘画、事件处理、决定尺寸大小等工作。ComponentUI对象依赖于当前使用的PLAF,使用UIManager.setLookAndFeel()方法可以改变外观风格。
4.支持设置边框。Swing组件可以设置一个或者多个边框,Swing提供了各种边框,用户也可以组合边框来自己设计边框。一种空白边框用于增大组件,同时协助布局管理器对容器内的组件进行合理布局。
每一个Swing组件都有一个对应的UI类,如JButton有ButtonUI类来作为UI代理。每一个组件的UI类都是将组件的J去掉然后加UI后缀。UI代理通常是个抽象基类,不同的PLAF会有不同的UI代理实现类,Swing类库包含了几套UI代理分别放在了不同的包下,每套UI代理都几乎实现了所有组件的ComponentUI实现,每套这样的实现都被称为一种PLAF实现。
1 import javax.swing.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 import java.awt.event.WindowAdapter; 5 import java.awt.event.WindowEvent; 6 import java.io.IOException; 7 8 public class SwingTestDemo1 { 9 //创建窗口 10 JFrame frame = new JFrame("Swing窗口程序"); 11 12 //创建菜单栏 13 JMenuBar menubar = new JMenuBar(); 14 JMenu fileMenu = new JMenu("文件"); 15 16 JMenu editMenu = new JMenu("编辑"); 17 JMenuItem auto = new JMenuItem("自动换行"); 18 JMenuItem copy = new JMenuItem("复制"); 19 JMenuItem paste = new JMenuItem("粘贴"); 20 21 JMenu formatMenu = new JMenu("格式"); 22 JMenuItem comment = new JMenuItem("注释"); 23 JMenuItem uncomment = new JMenuItem("取消注释"); 24 25 //创建文本域 26 JTextArea jta = new JTextArea(8,20); 27 28 //太丑了,我选择不要,留空 29 //创建颜色列表框 30 JList<String> colorList = new JList(); 31 32 //创建选择相关组件 33 JComboBox<String> colorSelect = new JComboBox<>(); 34 35 ButtonGroup sexGroup = new ButtonGroup(); 36 JRadioButton male = new JRadioButton("男",true); 37 JRadioButton female = new JRadioButton("女",false); 38 39 JCheckBox isMarried = new JCheckBox("是否已婚?", false); 40 41 //创建底部 42 JTextField textField = new JTextField(20); 43 JButton submit = new JButton("Submit"); 44 45 //创建右键菜单 46 JPopupMenu popMenu = new JPopupMenu(); 47 48 ButtonGroup PopButtonGroup = new ButtonGroup(); 49 JRadioButtonMenuItem metal = new JRadioButtonMenuItem("metal风格"); 50 JRadioButtonMenuItem nimbus = new JRadioButtonMenuItem("nimbus风格"); 51 JRadioButtonMenuItem windows = new JRadioButtonMenuItem("windows风格"); 52 JRadioButtonMenuItem windowsClassic = new JRadioButtonMenuItem("windows经典风格"); 53 JRadioButtonMenuItem motif = new JRadioButtonMenuItem("motif风格"); 54 55 public void init(){ 56 //组装底部 57 JPanel ButtonPanel = new JPanel(); 58 ButtonPanel.add(textField); 59 ButtonPanel.add(submit); 60 61 //组合中部下部 62 Box CDown = Box.createVerticalBox(); 63 64 JPanel centerPanel = new JPanel(); 65 66 //创建颜色选项 67 centerPanel.add(colorSelect); 68 colorSelect.addItem("红色"); 69 colorSelect.addItem("绿色"); 70 colorSelect.addItem("蓝色"); 71 72 73 sexGroup.add(male); 74 sexGroup.add(female); 75 centerPanel.add(male); 76 centerPanel.add(female); 77 centerPanel.add(isMarried); 78 79 CDown.add(centerPanel); 80 CDown.add(ButtonPanel); 81 82 //组合上部左右 83 Box Top = Box.createHorizontalBox(); 84 Top.add(jta); 85 Top.add(colorList); 86 87 //组合菜单栏 88 //组合格式化菜单 89 formatMenu.add(comment); 90 formatMenu.add(uncomment); 91 92 //组合编辑菜单 93 editMenu.add(auto); 94 editMenu.addSeparator(); 95 editMenu.add(copy); 96 editMenu.add(paste); 97 editMenu.addSeparator(); 98 editMenu.add(formatMenu); 99 100 //组合菜单栏 101 menubar.add(fileMenu); 102 menubar.add(editMenu); 103 104 //组合右键菜单 105 PopButtonGroup.add(metal); 106 PopButtonGroup.add(nimbus); 107 PopButtonGroup.add(windows); 108 PopButtonGroup.add(windowsClassic); 109 PopButtonGroup.add(motif); 110 111 popMenu.add(metal); 112 popMenu.add(nimbus); 113 popMenu.add(windows); 114 popMenu.add(windowsClassic); 115 popMenu.add(motif); 116 117 118 119 //添加到窗口 120 frame.add(popMenu); 121 //组合整个界面 122 Box mainBox = Box.createVerticalBox(); 123 mainBox.add(Top); 124 mainBox.add(CDown); 125 frame.add(mainBox); 126 frame.setJMenuBar(menubar); 127 128 //设置最佳大小 129 frame.pack(); 130 //设置可见 131 frame.setVisible(true); 132 133 //设置右键监听 134 ActionListener RightMouse = new ActionListener() { 135 @Override 136 public void actionPerformed(ActionEvent e) { 137 String command = e.getActionCommand(); 138 try { 139 changePLAF(command); 140 } catch (IOException ioException) { 141 ioException.printStackTrace(); 142 } catch (ClassNotFoundException classNotFoundException) { 143 classNotFoundException.printStackTrace(); 144 } catch (UnsupportedLookAndFeelException unsupportedLookAndFeelException) { 145 unsupportedLookAndFeelException.printStackTrace(); 146 } catch (InstantiationException instantiationException) { 147 instantiationException.printStackTrace(); 148 } catch (IllegalAccessException illegalAccessException) { 149 illegalAccessException.printStackTrace(); 150 } 151 } 152 }; 153 154 //注册右键监听器 155 metal.addActionListener(RightMouse); 156 nimbus.addActionListener(RightMouse); 157 windows.addActionListener(RightMouse); 158 windowsClassic.addActionListener(RightMouse); 159 motif.addActionListener(RightMouse); 160 161 //设置鼠标事件 162 mainBox.setComponentPopupMenu(popMenu); 163 164 //添加关闭功能 165 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 166 } 167 168 //添加改变外观的方法 169 private void changePLAF(String Command) throws IOException, ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException { 170 switch (Command){ 171 case "metal风格": 172 UIManager.setLookAndFeel("javax.swing.plaf.MetalLookAndFeel"); 173 break; 174 case "nimbus风格": 175 UIManager.setLookAndFeel("javax.swing.plaf.NimbusLookAndFeel"); 176 break; 177 case "windows风格": 178 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 179 break; 180 case "windows经典风格": 181 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); 182 break; 183 case "notif风格": 184 UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 185 break; 186 } 187 188 SwingUtilities.updateComponentTreeUI(frame.getContentPane()); 189 SwingUtilities.updateComponentTreeUI(menubar); 190 SwingUtilities.updateComponentTreeUI(popMenu); 191 } 192 public static void main(String[] args) { 193 new SwingTestDemo1().init(); 194 } 195 }

浙公网安备 33010602011771号