一:绘制图形界面的两套工具包
1)awt
1.awt是Abstract Window Toolkit(抽象组件窗口工具包)的缩写,是java最早的(JDK1.4之前)用于编写图形界面应用程序的开发包。
2.我们通常称awt控件为重量级控件,awt的代码由c/c++以及java构成。
3.也就是说,它所使用的图形组件是基于本地操作系统的图形库,绘制出的图形不具有跨平台性,操作系统不同时,绘制出的图形很有可能不同而引起混乱。
2)swing
1.swing是在awt的基础上构建的一套新的图形界面系统,在JDK1.4版本之后出现。
2.我们通常称swing控件为轻量级控件,swing控件完全是由100%java代码实现的。
3.由swing控件绘制出的图形,真正实现了java跨平台的特性,与本地操作系统的图形库没有任何联系。
3)区别:awt是基于本地方法的c/c++程序,其运行速度较快。
swing是基于awt的java程序,其运行速度比较慢。
ps:以上区别较简略,更详细的讨论请见http://www.douban.com/group/topic/3360549/
二:常用的组件
1)容器组件
1.JFrame (窗体类) 顶级容器
2.Jpanel (面板类) 最灵活的容器
3.JMenuBar (菜单栏)
4.JMenu (菜单项)
2)元素组件
1.JLabel (标签)
2.JComboBox (下拉框)
3.JTextField (输入框)
4.JMenuItem (菜单)
5.JCheckBox (复选框)
6.JButton (按钮)
7.JPasswordField (密码输入框)
ps:以上类名均为"J+名称",表示是属于javax.swing包下的类;若只有"名称",则表示是java.awt包下的类。
三:常用的布局
1)BorderLayout (边框布局)
2)GridLayout (网格布局)
3)FlowLayout (流式布局)
ps:在javax.swing和java.awt包下的所有以Layout结尾的类都是布局类,以上三种只是使用较为频繁。
四:界面实现
1)QQ登陆界面
import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; public class QQlanding { /** * QQ登陆 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub QQlanding qq = new QQlanding(); qq.window(); } public void window() { JFrame win = new JFrame("QQ登陆");// 实例化窗口对象并对标题命名 win.setSize(379, 260);// 设置窗口大小 win.setResizable(false);// 不可调节窗口大小 win.setLocationRelativeTo(null);// 窗口出现在屏幕中央 win.setDefaultCloseOperation(3);// 按叉关闭程序 win.setLayout(new BorderLayout()); // 设置北方 JPanel North = new JPanel(); North.setPreferredSize(new Dimension(379, 117)); win.add(North, BorderLayout.NORTH); // 设置西方 JPanel West = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0)); JLabel West1 = new JLabel(new ImageIcon("picture/qqlanding/qie.jpg")); West.setPreferredSize(new Dimension(100, 0)); West.add(West1); win.add(West, BorderLayout.WEST); // 设置中间 JPanel Center = new JPanel(); Center.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 6)); JComboBox Text1 = new JComboBox(); Text1.setPreferredSize(new Dimension(649,25)); Text1.setEditable(true); Text1.addItem("562828767"); JPasswordField Text2 = new JPasswordField(16); JCheckBox Box1 = new JCheckBox("记住密码"); JCheckBox Box2 = new JCheckBox("自动登录"); Box1.setOpaque(false); Box2.setOpaque(false); Center.add(Text1); Center.add(Text2); Center.add(Box1); Center.add(Box2); win.add(Center, BorderLayout.CENTER); // 设置东方 JPanel East = new JPanel(); East.setPreferredSize(new Dimension(105, 0)); East.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 6)); JLabel button1 = new JLabel("注册账号"); JLabel button2 = new JLabel("找回密码"); button1.setOpaque(false); button2.setOpaque(false); button1.setPreferredSize(new Dimension(100, 22)); button2.setPreferredSize(new Dimension(100, 22)); East.add(button1); East.add(button2); win.add(East, BorderLayout.EAST); // 设置南方 JPanel South = new JPanel(); JButton button3 = new JButton("登录"); button3.setPreferredSize(new Dimension(95, 22)); South.add(button3); win.add(South, BorderLayout.SOUTH); // 设置背景 ImageIcon background = new ImageIcon("picture/qqlanding/backgroung.jpg"); JLabel background2 = new JLabel(background); background2.setBounds(0, 0, background.getIconWidth(), background.getIconHeight()); win.getLayeredPane().add(background2, new Integer(Integer.MIN_VALUE)); Container P4 = win.getContentPane(); //JComponent // 顶层设置透明 ((JPanel) P4).setOpaque(false); West.setOpaque(false); Center.setOpaque(false); East.setOpaque(false); South.setOpaque(false); North.setOpaque(false); win.setVisible(true);// 设置窗口为可见 } }
2)计算器界面
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator { /**计算器界面 * @param args */ public static void main(String[] args) { //211 283 Calculator Cal = new Calculator(); Cal.window(); } public void window(){ JFrame JF = new JFrame("计算器"); //标题 JF.setSize(208,308); //大小 JF.setResizable(false); //大小不可变 JF.setLocationRelativeTo(null); //出现在中间 JF.setDefaultCloseOperation(3); //按叉 JF.setLayout(new BorderLayout()); JMenuBar JMB = new JMenuBar(); JMenu JM1 = new JMenu("查看(V)"); //加菜单栏 JMenuItem JMI1 = new JMenuItem("科学型"); //加菜单项 JMenuItem JMI2 = new JMenuItem("等等等"); JM1.add(JMI1); JM1.add(JMI2); JMenu JM2 = new JMenu("编辑(E)"); JMenu JM3 = new JMenu("帮助(H)"); JMB.add(JM1); JMB.add(JM2); JMB.add(JM3); JF.setJMenuBar(JMB); //北方 JTextField NJT = new JTextField("0"); NJT.setHorizontalAlignment(JTextField.RIGHT); NJT.setPreferredSize(new Dimension(190,57)); NJT.setFont(new Font("Arial",Font.BOLD,36)); NJT.setEditable(false); JF.add(NJT,BorderLayout.NORTH); //东方 JPanel E_JP = new JPanel(); E_JP.setPreferredSize(new Dimension(44,0)); String[] eimg = {"picture/calculator/m-.jpg","picture/calculator/genhao.jpg", "picture/calculator/%.jpg","picture/calculator/daoshu.jpg", "picture/calculator/=.jpg"}; for(int j=0;j < eimg.length;j++){ ImageIcon Ima = new ImageIcon(eimg[j]); JButton Jbu = new JButton(Ima); Jbu.setPreferredSize(new Dimension(Ima.getIconWidth(),Ima.getIconHeight())); E_JP.add(Jbu); } JF.add(E_JP,BorderLayout.EAST); //中间 JPanel C_JP = new JPanel(); C_JP.setPreferredSize(new Dimension(160,0)); String[] cimg = {"picture/calculator/mc.jpg","picture/calculator/mr.jpg", "picture/calculator/ms.jpg","picture/calculator/m+.jpg", "picture/calculator/jiantou.jpg","picture/calculator/ce.jpg", "picture/calculator/c.jpg","picture/calculator/+-.jpg", "picture/calculator/7.jpg","picture/calculator/8.jpg", "picture/calculator/9.jpg","picture/calculator/chu.jpg", "picture/calculator/4.jpg","picture/calculator/5.jpg", "picture/calculator/6.jpg","picture/calculator/cheng.jpg", "picture/calculator/1.jpg","picture/calculator/2.jpg", "picture/calculator/3.jpg","picture/calculator/-.jpg", "picture/calculator/0.jpg","picture/calculator/+.jpg", "picture/calculator/+.jpg"}; for(int i = 0;i < cimg.length ; i++){ // (1)第一种方法 // JButton Jbu = new JButton(new ImageIcon(cimg[i])); // if(cimg[i] == "picture/calculator/0.jpg"){ // Jbu.setPreferredSize(new Dimension(73,27)); // }else{ // Jbu.setPreferredSize(new Dimension(34,27)); // } // (2)第二种方法 ImageIcon Ima = new ImageIcon(cimg[i]); JButton Jbu = new JButton(Ima); Jbu.setPreferredSize(new Dimension(Ima.getIconWidth(),Ima.getIconHeight())); C_JP.add(Jbu); } JF.add(C_JP,BorderLayout.CENTER); JF.setVisible(true); } }