Java图形界⾯设计
⼀、总述
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
// ContentPane().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代码
public static void main(String args[]){
JFrame frame = new JFrame("Frame with Panel");
Container contentPane = ContentPane();
contentPane.setBackground(Color.CYAN);
JPanel panel = new JPanel();
panel.llow);
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参考代码
private JFrame frame;
private JButton btn1,btn2,btn3;
public static void main(String args[]){
FlowLayoutDemo that = new FlowLayoutDemo();
<();
}
public void go(){
frame = new JFrame("Flow Layout");
Container contentPane = ContentPane();
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⽰例代码
private JFrame frame;
private JButton be,bw,bn,bs,bc;
public static void main(String args[]){
BorderLayoutDemo that = new BorderLayoutDemo();
<();
}
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.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参考代码
private JFrame frame;
private JButton b1,b2,b3,b4,b5,b6;
public static void main(String args[]){
GridLayoutDemo that = new GridLayoutDemo();
<();
}
public void go(){
frame = new JFrame("Grid Frame");
Container contentPane = ContentPane();
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{
javaswing酷炫界面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();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。