图形界面:
JFrame对象
例:
Import javax.swing.JFrame;//提供一组轻量级java组件(图形界面组件)
Public class TestWindow{
JFrame f; //窗体类
PublicTestwindow(){
F =newJFrame(“这是一个新窗口”);//初始不可见的窗口
f.setSize(300,300);//设置窗体的宽和高
f.setVisible(true);//设置窗体为可见的
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体后进程结束
}
Publicstatic void main(String[] args){
New TestWindow();
}
}
Jpanel和jbutton类:
Japanel:面板组件
JButton:按钮组件
面板放在窗体上,按钮放在面板上
例:
Import javax.swing.JFrame;
Import javax.swing.JPanel;
Import java.awt.color;
Public class TestJbutton extends JFrame{
JPanel p = new JPanel();//新建面板
JButton b1,b2;
Public JButtonTest(){
B1 = new JButton(“按钮1”);
B2 = new JButton(“按钮3”);
This.getcontentPane().add(p);//将面板添加到窗体上,默认将面板铺满窗体
p.add(b1);//将按钮1添加到面板上
p.add(b2);
p.setBackground(color.red);//将面板对象设置为红色
This.setSize(300,300);
This.setVisible(true);
This.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Publicstatic void main(String[] args){
New JButtonTest;
}
}
Jtextfield组件:
JTextField:文本框类
容器组件:JPanel,JFrame
例:
Public class JTextFieldTest {
JFrame frame = new JFrame();
JPanel p = new JPanel();
JTextField txt = new JTextField(“默认显示”,20);
JLabel lbl = new JLabel(“请输入用户姓名:”);
Public JTextFieldTest(){
Frame.getcontentPane().add(p);
p.add(lbl);
p.add(txt);
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Public static void main(String[] args){
New JTextFieldTest();
}
}
用户事件:
用户和程序的交互
事件源
事件
事件处理程序
例:
Public class TestEvent implementsActionListener{
JFrameframe = new JFrame();
JPanel p = new JPanel();
JButton b1,b2;
Public TestEvent(){
B1 = new JButton(“按钮1”);//按钮b1是事件源
Frame.getContentPane().add(p);
p.add(b1);
p.add(b2);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);//给事件源注册监听
}
Publicstatic void main(String[] args){
New TestEvent();
}
//事件处理程序,继承接口后要重写此方法
Public void actionPerformed(ActionEvent e){
//弹出一个对话框
JOptionPane.showmessageDialog(this,”你点击了按钮1”);
}
}
事件类型:
ActionEvent:动作事件类型(事件源:点击按钮,点击文本框…)ActionListenr
FocusEvent:焦点事件类型
ItemEvent:选项事件类型
MouseEvent:鼠标事件类型
TextEvent:文本事件
WindowEvent:窗口事件
…
Flowlayout与gridlaylout布局:
Flowlayout:流式布局:一个接一个的放在面板上,一行放满自动切入下一行(JPanel默认布局就是流式布局)
GridLayout:网格布局:类似于表格的布局
例:
Public class GuiTest1 extends JFrame{
Jbutton b1,b2,b3,b4,b5;
Jpanel p;
PublicGuiTest1(){
B1 = new JButton(“Button1”);
B2 = new JButton(“Button2”);
B3 = new JButton(“Button3”);
B4 = new JButton(“Button4”);
B5 = new JButton(“Button5”);
P new JPanel();
This.getContentPane().add(p);
This.setSize(300,300);
This.setVisible(true);
This.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置面板p的布局是流式布局
//FlowLayout layout = new FlowLayout();
//p.setLayout(layout);
//设置面板p的布局是网格布局:两行3列
GridLayoutlayout = new GridLayout(2,3);
//p.setLayout(layout);
//设置水平间距和垂直间距
Layout.setEgap(10);
Layout.setVgap(10);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
}
Public static void main(String[] args){
New GuiTest1();
}
}
Cardlayout布局:
卡片布局:一页一页,每一页都是一个组件,默认显示第一张卡片(也就是第一个组件)
例:cardlayout配合动作事件完成翻页效果
Public class GUITest extends JFrameimplement ActionListener{
JPanelp1,p2,p3;
JButton b1,b2;
CardLayout layout;
PublicGUitest2{
P1 = new JPanel();
P2 = new JPanel();
P3 = new JPanel();
B1 = new JButton(“显示绿色面板”);
B2 = new JButton(“显示红色面板”);
//初始化cardlayout
//cardlayout的两种构造方法:不带参数:创建一个间距大小为0的新卡片布局
// 带参数:创建一个具有指定水平间距和垂直间距的新卡片布局
layout =new CardLayout();
p1.setLayout(layout);
p2.setBackground(Color.red);
p3.setBackground(Color.green);
p2.add(b1);
p3.add(b2);
p1.add(“panel2”,p2);
p1.add(“panel3”,p3);
this.getContentPane().add(p1);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//注册监听
B1.addActionListener(this);
B2.addActionListener(this);
}
Public static void main(String[] args){
New GuiTest2();
}
Public void actionPerformed(ActionEvent e){
//将获取的e强制转换成JButton类型
JButton b= (JButton)e.getSource();
If(b == b1){
//展示第几张卡片
layout.show(p1,”panel3”);
}if(b == b2){
layout.show(p1,”panel2”);
}
}
}
Borderlayout布局:
边界布局:东南西北中的布局方式
例:
Public class GUITest4 extends JFrame{
JPanel p1,p2,p3,p4,p5,p;
PublicGuiTest4{
P1 = new JPanel();
P2 = new JPanel();
P3 = new JPanel();
P4 = new JPanel();
P5 = new JPanel();
P = new JPanel();
P1.setBackground(Color.red);
P2.setBackground(Color.black);
P3.setBackground(Color.green);
P4.setBackground(Color.orange);
P5.setBackground(Color.white);
This.getContentPane().add(p);
//初始化构造方法:不带参数:没有间距
带两个参数:水平间距,垂直间距
BorderLayout layout = new BorderLayout();
p.setLayout(layout);
//放在东南西北中的哪个位置
p.add(“North”,p1);//或p.add(p1,BorderLayout.North);
p.add(“South”,p2);
p.add(“Center”,p3);
p.add(“West”,p4);
p.add(“East”,p5);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Public static void main(String[] args){
New GuiTest4();
}
}
Gridbaglayout布局
网格带布局(以x轴,y轴定位的布局)
例:
Public class GuiTest5{
JButton b1,b2,b3,b4,b5;
JPanel p;
PublicGuiTest5(){
B1 = new JButton(“button1”);
B2 = new JButton(“button2”);
B3 = new JButton(“button3”);
B4 = new JButton(“button4”);
B5 = new JButton(“button5”);
P = newJPanel();
This.getContentPane().add(p);
//初始化构造方法:只有一个不带参数的
GridBagLayout layout = new GridBagLayout();
p.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
Gbc.gridx = 0;
Gbc.gridy = 0;
p.add(b1,gbc);
Gbc.gridx = 0;
Gbc.gridy = 1;
p.add(b2,gbc);
Gbc.gridx = 1;
Gbc.gridy = 1;
p.add(b3,gbc);
…
p.add(b4,gbc);
p.add(b5,gbc);
This.setSize(400,400);
This.setVisible(true);
This.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Publicstatic void nain(String[] args){
New GuiTest5();
}
}