一、总述
Java的图形用户界面由各种组件(component)构成,它们主要位于java.awt包与javax.swing包中。Swing与AWT最大的不同是,Swing在实现时,不包含任何本地代码(native),是一种“轻量级(lightweight)”的组件
Swing具有状态的组件。
二、容器
1.顶层容器:
JFrame、JApplet、JDialog 和 JWindow
2.JFrame创建的一个程序
2.1代码
importjava.awt.*;
importjavax.swing.*;
publicclassJFrameDemo{
publicstaticvoidmain(String args[]){
JFrame frame = newJFrame("JFrameDemo");
JButton button = newJButton("Press Me");
//first way to do that
// frame.getContentPane().add(button,BorderLayout.CENTER);
//another way to set the Pane
JPanel contentPane = newJPanel();
contentPane.setLayout(newBorderLayout());
contentPane.add(button,BorderLayout.CENTER);
frame.setContentPane(contentPane);
//frame.pack();
frame.setVisible(true);
}
}
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代码
importjava.awt.*;
importjavax.swing.*;
publicclassFrameWithPanel{
publicstaticvoidmain(String args[]){
JFrame frame = newJFrame("Frame with Panel");
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.CYAN);
JPanel panel = newJPanel();
panel.setBackground(Color.yellow);
JButton button = newJButton("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);
}
}
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 的三种构造方法
publicFlowLayout()
publicFlowLayout(intalign)
publicFlowLayout(intalign,inthgap,intvgap)
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参考代码
importjava.awt.*;
importjavax.swing.*;
publicclassFlowLayoutDemo{
privateJFrame frame;
privateJButton btn1,btn2,btn3;
publicstaticvoidmain(String args[]){
FlowLayoutDemo that = newFlowLayoutDemo();
that.go();
}
publicvoidgo(){
frame = newJFrame("Flow Layout");
Container contentPane = frame.getContentPane();
contentPane.setLayout(newFlowLayout());
btn1 = newJButton("OK");
btn2 = newJButton("Open");
btn3 = newJButton("Close");
contentPane.add(btn1);
contentPane.add(btn2);
contentPane.add(btn3);
frame.setSize(300,200);
frame.setVisible(true);
}
}
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 JButto