一、总述
Java的图形用户界面由各种组件(component)构成,它们主要位于java.awt包与javax.swing包中。Swing与AWT最大的不同是,Swing在实现时,不包含任何本地代码(native),是一种“轻量级(lightweight)”的组件
Swing具有状态的组件。
二、容器
1.顶层容器:
JFrame、JApplet、JDialog 和 JWindow
2.JFrame创建的一个程序
2.1代码
import java.awt.*; import javax.swing.*; public class JFrameDemo{ public static void main(String args[]){ JFrame frame = new JFrame("JFrameDemo"); JButton button = new JButton("Press Me"); //first way to do that // frame.getContentPane().add(button,BorderLayout.CENTER); //another way to set the Pane JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(button,BorderLayout.CENTER); frame.setContentPane(contentPane); //frame.pack(); frame.setVisible(true); } }
2.2执行结果
3.面板(JPanel)
可以相互嵌套,不能独立存在,只能添加到其他窗口内部。
3.1代码
import java.awt.*; import javax.swing.*; public class FrameWithPanel{ public static void main(String args[]){ JFrame frame = new JFrame("Frame with Panel"); Container contentPane = frame.getContentPane(); contentPane.setBackground(Color.CYAN); JPanel panel = new JPanel(); panel.setBackground(Color.yellow); JButton button = new JButton("Press me"); panel.add(button); //add JButton instance to JPanel //add JPanel instance to JFrame's south contentPane.add(panel,BorderLayout.SOUTH); frame.setSize(300,200); frame.setVisible(true); } }
3.2执行结果
三、布局
1.总述
组件的布局(包括位置与大小)通常由Layout Manager负责安排。Java平台提供了多种布局管理器,以下对其部分,进行说明。
2.FlowLayout Layout Manager
2.1FlowLayout 的三种构造方法
public FlowLayout() public FlowLayout(int align) public FlowLayout(int align,int hgap,int vgap)
构造方法中,提供了一个对齐方式的可选项align,取值有三种形式:FlowLayout.LEFT、 FlowLayout.CENTER 、FlowLayout.RIGHT。hgap和vgap可以设定组件的水平间距和垂直距离。
2.2参考代码
import java.awt.*; import javax.swing.*; public class FlowLayoutDemo{ private JFrame frame; private JButton btn1,btn2,btn3; public static void main(String args[]){ FlowLayoutDemo that = new FlowLayoutDemo(); that.go(); } public void go(){ frame = new JFrame("Flow Layout"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); btn1 = new JButton("OK"); btn2 = new JButton("Open"); btn3 = new JButton("Close"); contentPane.add(btn1); contentPane.add(btn2); contentPane.add(btn3); frame.setSize(300,200); frame.setVisible(true); } }
2.3执行结果
改变Frame的大小,Frame中组件的布局也会随之改变。
3.BorderLayout 布局管理器
3.1概述
BorderLayout是顶层窗口中内容窗格的默认布局管理器,被划分为BorderLayout.NORTH、BorderLayout.SOUTH、BorderLayout.WEST、BorderLayout.EAST、BorderLayout.CENTER 五个区域。
3.2构造函数
BorderLayout() BorderLayout(int,int)
3.3示例代码
import java.awt.*; import javax.swing.*; public class BorderLayoutDemo{ private JFrame frame; private JButton be,bw,bn,bs,bc; public static void main(String args[]){ BorderLayoutDemo that = new BorderLayoutDemo(); that.go(); } public void go(){ frame = new JFrame("Border Layout"); be = new JButton("East"); bw = new JButton("West"); bn = new JButton("North"); bs = new JButton("South"); bc = new JButton("Center"); frame.getContentPane().add(be,"East"); frame.getContentPane().add(bw,BorderLayout.WEST); frame.getContentPane().add(bn,BorderLayout.NORTH); frame.getContentPane().add(bs,BorderLayout.SOUTH); frame.getContentPane().add(bc,BorderLayout.CENTER); frame.setSize(350,200); frame.setVisible(true); } }
3.4执行结果
4.GridLayout布局管理器
4.1总述
是一种网格式的布局管理器,将窗口空间分割成若干行,乘若干列的网格。组件依次放入,占一格。
4.2构造函数
public GridLayout()
public GridLayout(int rows, int cols)
public GridLayout(int rows, int cols, int hgap, int vgap)
4.3参考代码
import java.awt.*; import javax.swing.*; public class GridLayoutDemo{ private JFrame frame; private JButton b1,b2,b3,b4,b5,b6; public static void main(String args[]){ GridLayoutDemo that = new GridLayoutDemo(); that.go(); } public void go(){ frame = new JFrame("Grid Frame"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(3,2)); b1 = new JButton("grid_1"); b2 = new JButton("grid_2"); b3 = new JButton("grid_3"); b4 = new JButton("grid_4"); b5 = new JButton("grid_5"); b6 = new JButton("grid_6"); contentPane.add(b1); contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); contentPane.add(b6); frame.pack(); frame.setVisible(true); } }
4.4执行结果
5.CardLayout 布局管理器
5.1总述
是一种卡片式的布局管理器,它将容器中的组件处理成一系列卡片,每一时刻只显示出其中一张。
5.2参考代码:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CardLayoutDemo extends MouseAdapter{ JPanel p1,p2,p3,p4,p5; JLabel l1, l2, l3, l4, l5; //declare a Cardlayout object CardLayout myCard; JFrame frame; Container contentPane; public static void main(String args[]){ CardLayoutDemo that = new CardLayoutDemo(); that.go(); System.out.println("test"); } public void go(){ frame = new JFrame("Card Test"); contentPane = frame.getContentPane(); myCard = new CardLayout(); contentPane.setLayout(myCard); p1=new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); p5 = new JPanel(); //set the different bk color for each JPanel label l1 = new JLabel("This is the first JPanel"); p1.add(l1); p1.setBackground(Color.yellow); l2 = new JLabel("This is the second JPanel"); p2.add(l2); p2.setBackground(Color.green); l3 = new JLabel("This is the third JPanel"); p3.add(l3); p3.setBackground(Color.magenta); l4 = new JLabel("This is the forth JPanel"); p4.add(l4); p4.setBackground(Color.white); l5 = new JLabel("This is the fifth JPanel"); p5.add(l5); p5.setBackground(Color.cyan); //set mouseListener p1.addMouseListener(this); p2.addMouseListener(this); p3.addMouseListener(this); p4.addMouseListener(this); p5.addMouseListener(this); //set each JPanle as a Card to insert frame contentPane.add(p1,"First"); contentPane.add(p2,"Sencond"); contentPane.add(p3,"Third"); contentPane.add(p4,"Forth"); contentPane.add(p5,"Fifth"); //display the first pic myCard.show(contentPane,"First"); frame.setSize(300,200); frame.show(); } public void mouseClicked(MouseEvent e){ myCard.next(contentPane); } }
5.2执行结果
6.BoxLayout布局管理器
6.1总述
将容器中的组件,按水平排成一行,或按垂直方向排成一列。
6.2构造函数
BoxLayout(Container target, int axis)
axis = BoxLayout.X_AXIS | BoxLayout.Y_AXIS
6.3参考代码
import java.awt.*;
import javax.swing.*;
public class BoxLayoutDemo{
private JFrame frame;
private JPanel pv,ph;
public static void main(String args[]){
BoxLayoutDemo that = new BoxLayoutDemo();
that.go();
}
public void go(){
frame = new JFrame("Box Layout example");
Container contentPane = frame.getContentPane();
pv = new JPanel();
pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS));
pv.add(new JLabel("Monday"));
pv.add(new JLabel("Tuesday"));
pv.add(new JLabel("Wednesday"));
pv.add(new JLabel("Thursday"));
pv.add(new JLabel("Friday"));
pv.add(new JLabel("Saturday"));
pv.add(new JLabel("Sunday"));
contentPane.add(pv,BorderLayout.CENTER);
ph = new JPanel();
ph.setLayout(new BoxLayout(ph,BoxLayout.X_AXIS));
ph.add(new JButton("Yes"));
ph.add(new JButton("No"));
ph.add(new JButton("Cancle"));
contentPane.add(ph,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
6.4执行结果
6.5专门使用BoxLayout的特殊容器—Box类
6.5.1创建实例的静态方法
public static Box createHorizontalBox() public static Box createVerticalBox()
6.5.2|6.3的代码,改写如下
//the specific Box class for BoxLayout Container import java.awt.*; import javax.swing.*; public class BoxDemo{ private JFrame frame; private Box bv,bh; public static void main(String arg[]){ BoxDemo that = new BoxDemo(); that.go(); } void go(){ frame = new JFrame("Box Layout example"); Container contentPane = frame.getContentPane(); bv = Box.createVerticalBox(); bv.add(new JLabel("Monday")); bv.add(new JLabel("Tuesday")); bv.add(new JLabel("Wednesday")); bv.add(new JLabel("Thursday")); bv.add(new JLabel("Friday")); bv.add(new JLabel("Saturday")); bv.add(new JLabel("Sunday")); contentPane.add(bv,BorderLayout.CENTER); bh = Box.createHorizontalBox(); bh.add(new JButton("Yes")); bh.add(new JButton("No")); bh.add(new JButton("Cancel")); contentPane.add(bh,BorderLayout.SOUTH); frame.setSize(300,200); //frame.pack(); frame.setVisible(true); } }
6.6Box类提供的创建不可组件方法
public static Component createHorizontalGlue() public static Component createHorizontalStrut(int width) public static Component createVerticalGlue() public static Component createVerticalStrut(int height) public static Component createRigidArea(Dimension d)
6.6.1参考代码
//to create invisible component import java.awt.*; import javax.swing.*; public class GlueAndStrut{ private JFrame frame; private Box b1,b2,b3,b4; public static void main(String args[]){ GlueAndStrut that = new GlueAndStrut(); that.go(); } void go(){ frame = new JFrame("Glue And Strut"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(4,1)); b1 = Box.createHorizontalBox(); b1.add(new JLabel("Box 1:")); b1.add(new JButton("Yes")); b1.add(new JButton("No")); b1.add(new JButton("Cancel")); b2 = Box.createHorizontalBox(); b2.add(new JLabel("Box 2:")); b2.add(new JButton("Yes")); b2.add(new JButton("No")); b2.add(Box.createHorizontalGlue()); b2.add(new JButton("Cancel")); b3 = Box.createHorizontalBox(); b3.add(new JLabel("Box 3:")); b3.add(new JButton("Yes")); b3.add(new JButton("No")); b3.add(Box.createHorizontalStrut(20)); b3.add(new JButton("Cancel")); b4 = Box.createHorizontalBox(); b4.add(new JLabel("Box 4:")); b4.add(new JButton("Yes")); b4.add(new JButton("No")); b4.add(Box.createRigidArea(new Dimension(10,10))); b4.add(new JButton("Cancel")); contentPane.add(b1); contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); frame.setSize(300,200); frame.setVisible(true); } }
6.6.2执行结果
7.不使用布局管理器
7.1一般做法
a.setLayout(null)将布局管理器设置为空
b.setBounds(int x,int y,int width,int height) 设置组件的位置与大小
7.2参考代码
import java.awt.*; import javax.swing.*; public class NullLayoutDemo{ private JFrame frame; private JButton b1,b2,b3; public static void main(String args[]){ NullLayoutDemo that = new NullLayoutDemo(); that.go(); } private void go(){ frame = new JFrame("Null Layout Demo"); Container contentPane = frame.getContentPane(); //set the layout as null; contentPane.setLayout(null); //add the buttons b1 = new JButton("Yes"); contentPane.add(b1); b2 = new JButton("No"); contentPane.add(b2); b3 = new JButton("Cancel"); contentPane.add(b3); //set the buttons' pos and size; b1.setBounds(30,15,75,20); b2.setBounds(150,15,75,50); b3.setBounds(150,100,75,20); frame.setSize(300,200); frame.setVisible(true); } }
7.3执行结果