GUI编程
1、简介
Gui的核心技术:Swing AWT
- 因为界面不美观。
- 需要jre环境
为什么要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到swing界面
- 了解MVC架构,了解监听
2、AWT
2.1、AWT介绍
- 包含了很多类和接口、GUI、
- 元素:窗口、面板
2.2、组件和容器
2.2.1.Frame
//gui的第一个界面
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个java图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//需要设置窗口大小
frame.setSize(400,500);
//设置背景颜色
frame.setBackground(new Color(255,156,185));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定,默认true可以改变大小,现在false不能改变
frame.setResizable(false);
}
}
多个弹窗
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.gray);
MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.yellow);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.pink);
}
}
class MyFrame extends Frame {
static int id = 0;//可能存在多个窗口,我们需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
2.2.2.panel
面板,不能单独存在,可以看做是一个空间,需要在frame上
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(193,15,60));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(93,15,45));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
解决了窗口无法关闭的问题,方法是添加监听事件
2.3 布局管理
2.3.1流式布局
public class TestFlowlayout {
public static void main(String[] args) {
Frame frame = new Frame();
//组件--按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.3.2 东西南北中
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
frame.setBounds(200,200,400,300);
Button east = new Button("East");
Button west = new Button("west");
Button south = new Button("south");
Button north = new Button("north");
Button center = new Button("center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.3.3 表格布局 Grid
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
frame.setBounds(200,200,400,300);
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//java函数
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
- 布局练习题
实现
效果,思路:
一个大的窗口分为上下两个也就是表格布局2行1列,然后上下两个窗口中添加面板(p1、p3),上面的窗口中可分为东中西三个部分(p1-1、p2、p1-2),中间部分用一个面板(p2),中间部分的面板可以设置成两行一列的格局(p2-1、p2-2)。下面的frame也分为东中西三个部分,中间的部分使用面板(p4),中间部分的面板设置成2行2列的格局就可以了(p4-1、p4-2、p4-3、p4-4)。最后将p2与东西两个按钮添加到frame1中,将p4与东西两个按钮(p3-1、p3-2)添加到frame2中。
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestButton {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(200,200,400,500);
frame.setLayout(new GridLayout(2,1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("p1-1"),BorderLayout.WEST);
p1.add(new Button("p1-2"),BorderLayout.EAST);
p2.add(new Button("p2-1"));
p2.add(new Button("p2-2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("p3-1"),BorderLayout.WEST);
p3.add(new Button("p3-2"),BorderLayout.EAST);
p4.add(new Button("p4-1"));
p4.add(new Button("p4-2"));
p4.add(new Button("p4-3"));
p4.add(new Button("p4-4"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器–流式、东西南北中、表格
2.4 事件监听
给一个按钮添加事件监听,点击后控制台会打印出aaa
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionevent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
//因为,addActionListener()需要一个ActionListener,所以我们需要构造一个需要一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
给两个按钮添加同一个监听事件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
public static void main(String[] args) {
Frame frame = new Frame("开始--停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
button2.setActionCommand("button2-stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:msg"+e.getActionCommand());
}
}
2.5 输入框TextField 监听
主函数里只写启动的,不再写其他内容,其他方法另外写。
创建一个文本框放入窗口中,为文本框设置监听事件,写事件需要继承接口中的才能添加自己想要的
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
//只写启动
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下enter会触发输入框事件
textField.addActionListener(myActionListener2);
textField.setEchoChar('*');//输入的字符用*代替了
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field =(TextField) e.getSource();//获得一些资源
System.out.println(field.getText());//获取输入框的内容
field.setText("");//回车后清空输入框里的内容
}
}
2.6 计算器
oop原则:组合,大于继承
//继承
class A extends B{
}
//组合,在A里也可以使用B
class A{
public B b;
}
简易计算器:(面向对象写法)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
//属性
TextField field1,field2,field3;
public Calculator(){
field1 = new TextField(10);
field2 = new TextField(10);
field3 = new TextField(10);
Label label = new Label("+");
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
setLayout(new FlowLayout());
add(field1);
add(label);
add(field2);
add(button);
add(field3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类中组合另外一个类;
Calculator calculator = null;
//有参构造函数
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取第一个和第二个文本框中的数
int n1 = Integer.parseInt( calculator.field1.getText());
int n2 = Integer.parseInt( calculator.field2.getText());
calculator.field3.setText(""+(n1+n2));
calculator.field1.setText("");
calculator.field2.setText("");
}
}
点击=后前两个会清空
使用内部类,将监听器类放在Calculator类中作为内部类,因为内部类可以直接使用外部的属性和方法,可以直接获取输入框中的内容,并且减少了代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
//属性
TextField field1,field2,field3;
public Calculator(){
field1 = new TextField(10);
field2 = new TextField(10);
field3 = new TextField(10);
Label label = new Label("+");
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
setLayout(new FlowLayout());
add(field1);
add(label);
add(field2);
add(button);
add(field3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//监听器类
//内部类的最大的好处是可以直接访问外部的属性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获取第一个和第二个文本框中的数
int n1 = Integer.parseInt(field1.getText());
int n2 = Integer.parseInt(field2.getText());
field3.setText(""+(n1+n2));
field1.setText("");
field2.setText("");
}
}
}
2.7 画笔
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(100,300,500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
//画笔首先要有颜色
g.setColor(Color.red);
g.fillOval(100,100,100,100);//圆
g.setColor(Color.BLUE);
g.fillRect(150,200,200,200);//矩形
//养成习惯,画笔用完,将他还原到最初的颜色,黑色
}
}
2.8 鼠标监听
目的:想要实现鼠标画画
思路:先创建一个窗口设置大小,因为要点击鼠标画画,所以给窗口设置一个鼠标监听器,于是下面写一个自己的MyMouseListener,适配器模式选择继承重写自己需要的事件(鼠标按压事件mousePress),getSource获取的我们点击的坐标,所以需要一个数组ArrayList存放点,写个addPaint方法,画笔读取存点的位置,添加点到界面上,paint方法中使用迭代器,将所有点绘画到窗口中,因为画笔只会画一次,所以使用repaint,每次点击就重画一次。(画板和画笔是一直存在的,画笔通过读取监听鼠标所读取存放在集合里的点的位置,在画板上画出点)
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;//迭代器
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200,200,400,300);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//存鼠标点击的点的位置
points = new ArrayList<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener() {
});
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){//如果存放点获取到了下一个点,就让他可以执行下面的
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面
public void addPaint(Point point){
points.add(point);
}
//适配器模式,不用接口重写全部方法,用继承重写自己需要的
private class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
//鼠标按下,弹起,按住不放
MyFrame frame = (MyFrame) e.getSource();
//我们点击鼠标后,就会在界面上产生一个点
//这个点就是鼠标的点
frame.addPaint( new Point(e.getX(),e.getY()));
//每次点击鼠标重新画一遍
frame.repaint();
}
}
}
lterator迭代器:简单理解为遍历,是一个标准化遍历各类容器里面的多有对象的方法类,是一个经典的设计模式。
我们一般遍历数组Array是使用下标遍历得出,我们都是事先知道集合的内部结构,才可以循环遍历。而lterator总是使用同一种逻辑来遍历集合,客户端自身不需要来维护集合的内部结构。
lterator作为java中的一个接口,其定义:
public interface Iterator{ boolean hasNext();//判断容器内是否还有可供访问的元素 Object next();//返回迭代器刚越过的元素的引用 void remove();//删除迭代器刚越过的元素 }
一般而言,我们只需要使用Next().hasNext()两个方法就可以完成迭代
for(Iterator it = c.iterator();it.hasNext();){ Object o = it.next(); //Do something... }
2.9 窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(){
setBounds(100,100,200,400);
setBackground(Color.pink);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
窗口监听有:
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
2.10 键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new keyFrame();
}
}
class keyFrame extends Frame{
public keyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP){
System.out.println("你按了上键");
}
}
});
}
}
3. Swing
3.1 窗口(JFrame)
import javax.swing.*;
public class JFrameDemo01 {
//init();初始化
public void init(){
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,300,300);
//设置文字 Label
JLabel label = new JLabel("欢迎来到贪吃蛇游戏");
jf.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo01().init();
}
}
3.2 弹窗 JDialog
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
setVisible(true);
setSize(700,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个对话框
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,700,400);
//JFrame 放东西 容器
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("这是弹窗"));
}
}
}
//弹窗的窗口
3.3 标签(Label)
new JLabel("xxx");
图标 Icon
3.4 面板(JPanel)、滚动面板(JScroll)
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("hello");
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5 按钮
- 单选按钮(JradioButton )
JradioButton radioButton1 = new JradioButton;
JradioButton radioButton2 = new JradioButton;
JradioButton radioButton3 = new JradioButton;
//单选框只能选择一个,分组,组内只能选择一个就可以了
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
- 复选按钮(JCheckBox)
JCheckBox checkBox01 = new JCheckBox();
JCheckBox checkBox02 = new JCheckBox();
JCheckBox checkBox03 = new JCheckBox();
3.6 列表
- 下拉框(JcomboBox)
JcomboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
- 列表框(JList)
Container cintainer = this.getContentPane();
Vector contents = new Vector();
JList jList = new JList(contents);
contents.add("1");
contents.add("2");
contents.add("3");
container.add(jList);
- 应用场景
- 选择地图,或者一些单个选项
- 列表,展示信息,一般是动态扩容
3.7 文本框
-
文本框(JTextField)
-
密码框(JPasswordField)
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField)
- 文本域(JTextArea)