GUI(Graphical User Interface),图形用户界面
根据作用分为基本组件(按钮等)和容器(窗口等)
所有容器都是java.awt.Container的子类
AWT(Abstract Window Toolkit),抽象窗口工具集,第一代的Java GUI组件,
重量级的(绘制图形时基于底层操作系统完成的,每种操作系统界面是不一样的)
Swing,不依赖底层细节,轻量级的组件
Container两个主要类型是Window和Panel
java.awt.Window
有两种形式Frame(框架)和Dialog(对话框)
BorderLayout默认布局
java.awt.Panel
Panel必须放在Window或者Window的子类中,一遍显示出来
FlowLayout默认布局
布局管理器
BorderLayout 边界布局,五个区域,东南西北中
FlowLayout 流式布局
GridLayout 网格布局
CardLayout
GridBagLayout
每个Container都有一个默认布局
事件概念
事件:描述了反生了什么的对象
事件源:事件的产生器
事件处理器:接受事件,解释事件并处理用户交互的方法
AWT事件模型
JDK1.0的事件模型:层次模型(已经废弃)
JDK1.1的时间模型:委托模型
Java中的组件若想添加事件处理器,都会使用如addXxxListener的方法添加
许多事件位于java.awt.event包中,也有一些事件在API的其他地方
几类具有典型意义的事件
MouseEvent 鼠标事件 鼠标按下,释放,点击等
WindowEvent 窗口事件 点击关闭/最小化窗口,得到/失去焦点等
ActionEvent 动作事件
事件Adapters(适配器)
定义的Listener继承Adapter类,那么我们只重写需要的方法,简化了编程
Swing分类:顶层容器,中间容器,原子组件
Java提供了三个顶层容器:JFrame,JDialog,JApplet
JFrame是一个带有标题和可变大小边框的窗口,任何一个图形界面程序必须至少有一个JFrame
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GUIFrame {
JFrame jf;
JButton jb1, jb2, jb3, jb4, jb5;
public void go() {
jf = new JFrame("BorderLayout");
jb1 = new JButton("东");
jb2 = new JButton("南");
jb3 = new JButton("西");
jb4 = new JButton("北");
jb5 = new JButton("中");
jf.setLayout(new BorderLayout(2, 2));// 上边距,下边距
jf.add(jb1, BorderLayout.EAST);
jf.add(jb2, BorderLayout.SOUTH);
jf.add(jb3, BorderLayout.WEST);
jf.add(jb4, BorderLayout.NORTH);
jf.add(jb5, BorderLayout.CENTER);
jf.setSize(new Dimension(400, 200));
jf.setLocation(new Point(600, 250));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new GUIFrame().go();
}
}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GUIFrame {
JFrame jf;
JButton jb1, jb2, jb3, jb4, jb5;
public void go() {
jf = new JFrame("FlowLayout");
//中间对齐方式,上边距,下边距
jf.setLayout(new FlowLayout(FlowLayout.CENTER,2, 2));
jb1 = new JButton("东");
jb2 = new JButton("南");
jb3 = new JButton("西");
jb4 = new JButton("北");
jb5 = new JButton("中");
jf.add(jb1);
jf.add(jb2);
jf.add(jb3);
jf.add(jb4);
jf.add(jb5);
jf.setSize(new Dimension(400, 200));
jf.setLocation(new Point(600, 250));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new GUIFrame().go();
}
}
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GUIFrame {
JFrame jf;
JButton jb1, jb2, jb3, jb4, jb5;
public void go() {
jf = new JFrame("GridLayout");
//两行三列,上边距,下边距
jf.setLayout(new GridLayout(2,3,5,5));
jb1 = new JButton("东");
jb2 = new JButton("南");
jb3 = new JButton("西");
jb4 = new JButton("北");
jb5 = new JButton("中");
jf.add(jb1);
jf.add(jb2);
jf.add(jb3);
jf.add(jb4);
jf.add(jb5);
jf.setSize(new Dimension(400, 200));
jf.setLocation(new Point(600, 250));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new GUIFrame().go();
}
}
package com.itlwc;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MySwing {
JFrame jf;
JPanel jp;
JTextArea jta;
JComboBox jcb;
JTextField jtf;
JButton jb;
JScrollPane jsp;
StringBuffer sb = new StringBuffer();
public void go() {
jf = new JFrame();
jp = new JPanel();
String[] str = new String[] { "李文超", "牛小俊" };
jcb = new JComboBox(str);
jtf = new JTextField(10);
jb = new JButton("send");
jb.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Object name = jcb.getSelectedItem();
String content = jtf.getText();
String nc = name+":"+content+"\n";
sb.append(nc);
jta.setText(sb.toString());
jtf.setText("");
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
jp.add(jcb);
jp.add(jtf);
jp.add(jb);
jf.add(jp, BorderLayout.SOUTH);
jta = new JTextArea();
jsp = new JScrollPane(jta);// 把textarea当参数输入滚动条
jf.add(jsp);// 增加滚动条就行了
jf.setSize(new Dimension(300, 200));
jf.setLocation(new Point(600, 250));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new MySwing().go();
}
}
效果图
package com.itlwc;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MySwing {
private JFrame jf;
private JPanel jp1, jp2, jp3;
private JButton jb1, jb2;
private JLabel jl1, jl2;
private JTextField jtf1;
private JPasswordField jpf1;
public static void main(String[] args) {
new MySwing().go();
}
public void go() {
jf = new JFrame();
jf.setTitle("登陆");
jf.setSize(new Dimension(300, 150));
jf.setLocation(new Point(100, 100));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
jf.setLayout(new GridLayout(3, 2));
jl1 = new JLabel("用户名:");
jtf1 = new JTextField(20);
jp1 = new JPanel();
jp1.add(jl1);
jp1.add(jtf1);
jf.add(jp1);
jl2 = new JLabel("密 码:");
jpf1 = new JPasswordField(20);
jp2 = new JPanel();
jp2.add(jl2);
jp2.add(jpf1);
jf.add(jp2);
jb1 = new JButton("登陆");
jb1.addActionListener(new MyButtonListener());
jb2 = new JButton("取消");
jb2.addActionListener(new MyButtonListener());
jp3 = new JPanel();
jp3.add(jb1);
jp3.add(jb2);
jf.add(jp3);
jf.setVisible(true);
}
private class MyButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
System.out.println("用户名:" + jtf1.getText());
char[] c = jpf1.getPassword();
System.out.println("密 码:" + new String(c));
}
if (e.getSource() == jb2) {
jtf1.setText("");
jpf1.setText("");
}
}
}
}
效果图
package com.itlwc;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MySwing {
private static final long serialVersionUID = 1L;
private JFrame jf;
private JMenuBar jmb1;
private JMenu jm1, jm2, jm3;
private JMenuItem jmi1, jmi2, jmi3, jmi4;
public static void main(String[] args) {
new MySwing().go();
}
public void go() {
jf = new JFrame();
jf.setTitle("菜单");
jf.setSize(new Dimension(400, 200));
jf.setLocation(new Point(600, 250));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
jmb1 = new JMenuBar();
jm1 = new JMenu("文件");
jm2 = new JMenu("帮助");
jmb1.add(jm1);
jmb1.add(jm2);
jmi1 = new JMenuItem("打开");
jmi2 = new JMenuItem("另存为");
jm1.add(jmi1);
jm1.add(jmi2);
jmi3 = new JMenuItem("保存到桌面");
jmi4 = new JMenuItem("保存到网路");
jm3 = new JMenu("保存");
jm3.add(jmi3);
jm3.add(jmi4);
jm1.add(jm3);
jf.add(jmb1, BorderLayout.NORTH);
jf.setVisible(true);
}
}
效果图
package com.itlwc;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MySwing {
private JFrame jf;
private MyPanel mp;
public static void main(String[] args) {
new MySwing().go();
}
public void go() {
jf = new JFrame();
jf.setTitle("绘图");
jf.setSize(new Dimension(200, 200));
jf.setLocation(new Point(100, 100));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mp = new MyPanel();
jf.add(mp);
jf.addKeyListener(new MyKeyEvent());
jf.setVisible(true);
}
// 画个小球
private class MyPanel extends JPanel {
private int x = 5;
private int y = 5;
private static final long serialVersionUID = 1L;
// 重写父类绘制容器方法
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.GREEN);// 设置画笔颜色
// 填充一个圆形 x,y坐标,高度,宽度
g.fillOval(x, y, 50, 50);
}
}
// 控制小球的移动
private class MyKeyEvent extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_UP) {
mp.y--;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
mp.y++;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
mp.x--;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
mp.x++;
}
// 让面板刷新
mp.repaint();
}
}
}
效果图