工作以来,一直都是基于java web方向的开发,并没有java方向GUI相关的开发经验,不过好在之前用过winform开发。有了基础的套路,想来搞一下Swing也没有什么压力!到网上搜了一下相关的学习资料,有拖拖拽拽的控件,也有代码实现的。为了加深印象和理解,直接搞代码吧~
参考了网上的相关博客,先有一个大体的了解,比如:http://www.blogjava.net/jerry-zhaoj/articles/283170.html
选择了一个PDF作为学习的参考:Java Swing图形界面开发与案例详解 .王鹏等.扫描版.pdf
学习Swing之前,先来了解一下AWT:
AWT(Abstract Windows Toolkit):抽象窗口工具集,提供了与本地图形化界面交互的接口,底层调用本地操作系统。由于实现依赖于本地操作系统,所以是重量级组件,Swing则是在实现AWT的基础上做扩展的,组件是以纯java的方式来实现的,不依赖于底层的操作系统,所以是轻量级组件。AWT设计的初衷是开发小的应用程序的图形界面,而非专门用于用户界面(UI)的工具包而设计,所以做UI有些欠缺,比如剪切板、弹出菜单、滚动等功能都不支持。
接着照着第一章的例子做了3个demo,demo中不用关心具体的代码细节,先看大体的框架即可。
demo1:登录窗口
demo2:菜单栏、工具栏
demo3:tab页签
demo1:
package com.comall.fastsvn.test.chapter1; import javax.swing.*; import java.awt.*; /** * 登录界面 */ public class SwingDemo1 extends JPanel { private static final int JFRAME_WIDTH = 300; private static final int JFRAME_HEIGHT= 150; private SwingDemo1(){ //窗口-顶层面板 JFrame jFrame = new JFrame("登录窗口"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //网格布局,容器中的方法 GridBagLayout gridBagLayout = new GridBagLayout(); setLayout(gridBagLayout); jFrame.add(this,BorderLayout.WEST); jFrame.setSize(JFRAME_WIDTH,JFRAME_HEIGHT); //位置:居中 Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); jFrame.setLocation((int)(dimension.getWidth() - JFRAME_WIDTH ) / 2,(int)(dimension.getHeight() - JFRAME_HEIGHT ) / 2); //组件 JLabel tipLabel = new JLabel("欢迎来到Java世界"); JLabel userLabel = new JLabel("用户名:"); JLabel pwdLabel = new JLabel("密码:"); JTextField userText = new JTextField(15); JTextField pwdText = new JTextField(15); JButton submitBtn = new JButton("登录"); JButton cancelBtn = new JButton("取消"); //网格布局 GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.fill = GridBagConstraints.NONE; gridBagConstraints.anchor = GridBagConstraints.EAST; gridBagConstraints.weightx = 3; gridBagConstraints.weighty = 4; //添加组件并指定组件的位置 addComponent(tipLabel,gridBagConstraints,0,0,4,1); addComponent(userLabel,gridBagConstraints,0,1,1,1); addComponent(pwdLabel,gridBagConstraints,0,2,1,1); addComponent(userText,gridBagConstraints,2,1,1,1); addComponent(pwdText,gridBagConstraints,2,2,1,1); addComponent(submitBtn,gridBagConstraints,0,3,1,1); addComponent(cancelBtn,gridBagConstraints,2,3,1,1); //面板可见、不可编辑 jFrame.setResizable(false); jFrame.setVisible(true); } /** * 添加组件 * @param component 组件 * @param gridBagConstraints * @param x 行 * @param y 列 * @param w 宽 * @param h 高 */ private void addComponent(Component component,GridBagConstraints gridBagConstraints,int x,int y,int w,int h){ gridBagConstraints.gridx = x; gridBagConstraints.gridy = y; gridBagConstraints.gridwidth = w; gridBagConstraints.gridheight = h; //Container中的方法 add(component,gridBagConstraints); } public static void main(String[] args) { new SwingDemo1(); } }
运行效果:
demo2:
package com.comall.fastsvn.test.chapter1; import javax.swing.*; import java.awt.*; /** * 菜单栏、工具栏 */ public class SwingDemo2 { private static final int JFRAME_WIDTH = 600; private static final int JFRAME_HEIGHT= 400; public SwingDemo2(){ //窗口-顶层面板 JFrame jFrame = new JFrame("菜单工具栏"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setSize(JFRAME_WIDTH,JFRAME_HEIGHT); //窗口内加入面板 JPanel jPanel = new JPanel(); jFrame.setContentPane(jPanel); //窗口加入菜单栏 JMenuBar menuBar = new JMenuBar(); jFrame.setJMenuBar(menuBar); //菜单栏加菜单 JMenu menu1 = new JMenu("菜单1"); JMenu menu2 = new JMenu("菜单2"); menuBar.add(menu1);menuBar.add(menu2); //菜单加子菜单 JMenuItem menuItem11 = new JMenuItem("子菜单11"); JMenuItem menuItem12 = new JMenuItem("子菜单12"); JMenuItem menuItem21 = new JMenuItem("子菜单21"); JMenuItem menuItem22 = new JMenuItem("子菜单22"); menu1.add(menuItem11); menu1.addSeparator(); menu1.add(menuItem12); menu2.add(menuItem21); menu2.addSeparator(); menu2.add(menuItem22); //面板加入工具栏 JButton jButton1 = new JButton("工具1"); JButton jButton2 = new JButton("工具2"); JButton jButton3 = new JButton("工具3"); JToolBar toolBar = new JToolBar(); toolBar.add(jButton1); toolBar.add(jButton2); toolBar.add(jButton3); //布局 BorderLayout borderLayout = new BorderLayout(); jPanel.setLayout(borderLayout); jPanel.add("North",toolBar); //位置:居中 Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); jFrame.setLocation((int)(dimension.getWidth() - JFRAME_WIDTH ) / 2,(int)(dimension.getHeight() - JFRAME_HEIGHT ) / 2); //面板可见、不可编辑 jFrame.setResizable(false); jFrame.setVisible(true); } public static void main(String[] args) { new SwingDemo2(); } }
运行效果:
demo3:
package com.comall.fastsvn.test.chapter1; import javax.swing.*; import java.awt.*; /** * */ public class SwingDemo3 extends JTabbedPane{ private static final int JFRAME_WIDTH = 600; private static final int JFRAME_HEIGHT= 400; private SwingDemo3(){ //窗口-顶层面板 JFrame jFrame = new JFrame("tab页签"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setSize(JFRAME_WIDTH,JFRAME_HEIGHT); jFrame.setVisible(true); //面板 JPanel jPanel = new JPanel(); jFrame.setContentPane(jPanel); setLayout(new BorderLayout()); //子面板 JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); jPanel1.setLayout(new BorderLayout()); addTab("panel1",jPanel1); setEnabledAt(0,true); setTitleAt(0,"测试页1"); addTab("panel2",jPanel2); setEnabledAt(1,true); setTitleAt(1,"测试页2"); setPreferredSize(new Dimension(500,200)); setTabPlacement(JTabbedPane.TOP); setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); jPanel.add("Center",this); setVisible(true); } public static void main(String[] args) { new SwingDemo3(); } }
运行效果: