Java学习笔记-GUI

GUI

tip: 不涉及面试,因为太古老!

GUI不是Java的强项,但是Java也能做!

01:GUI编程简介

tip:

  • 组件:窗口、弹窗、面板、文本框、列表框、按钮、图片、监听事件、鼠标事件、键盘事件、破解工具。
  • 外挂就喜欢用Java写,因为Java跨平台,JVM总不可能检测卸载。
  • 破解工具:反编译。
  • Gui的核心技术:Swing、AWT,只需要掌握这两个相关的类就好了。
  • AWT有画笔,Swing能画图。
  • 为什么不流行呢?
    • 因为界面不美观。
    • 需要有jre环境,100多M。
  • C++适合游戏开发,有很多关于图层的引擎!
  • 为什么我们要学习?
    • 因为这个是MVC的基础,了解MVC架构、了解监听。
    • 可以写出自己心中想要的小工具。
    • 工作时候也可能需要维护到我们的Swing界面,概率极小!
  • Component组件

02:AWT介绍

  • 是Swing 的一个前身吧
  • AWT:abstract window tools 单词意思
  1. 包含了很多的类和接口!用于GUI编程。GUI(Graphic User Interface):图形用户界面编程。

    Eclipse:使用Java写的。但是IDEA比较符合年轻人的思想。

  2. 元素:窗口,按钮,文本框

  3. Java.awt包,目录:C:\\Program Files\\Java\\jre1.8.0_181\\lib\\rt.jar,用解压软件打开,可以看到在java文件夹里面有awt包。

AWT包中各种部件的类继承层次图:

Untitled

Untitled

Untitled

03:第一个Frame窗口

代码如下:

package com.maynerd.lesson01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
//Frame
public static void main(String[] args) {
//Frame JDK 看源码!
Frame frame = new Frame("这是我的第一个Java图形界面窗口!");
//设置窗口大小
frame.setSize(1000,1000);
//设置可见性
frame.setVisible(true);
//设置背景颜色
frame.setBackground(new Color(155, 142, 142));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}

结果如下:

Untitled

发现窗口关不掉,停止java程序!

写一个类封装,尝试封装!代码如下:

package com.maynerd.lesson01;

import java.awt.*;

public class TestFrame02 {
public static void main(String[] args) {
//展示多个窗口
new MyFrame(100,100,200,200,Color.black);
new MyFrame(300,100,200,200,Color.green);
new MyFrame(100,300,200,200,Color.yellow);
new MyFrame(300,300,200,200,Color.red);
}
}
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);
}
}

结果如下:注意坐标是从屏幕左上角开始定位,屏幕为1920*1080像素。

Untitled

04:Panel面板讲解

写的东西尽量放在面板上,frame只有一个。

解决关闭事件!通过一个事件监听,再重写其中Closing方法,设定点击关闭即程序终止。

代码如下:

package com.maynerd.lesson01;

import com.sun.beans.editors.ColorEditor;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import static java.awt.SystemColor.window;

//可以看成是一个空间
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(114, 189, 86));

    //Panel设置坐标,相对于frame
    panel.setBounds(50,50,400,400);
    panel.setBackground(new Color(208, 26, 26));

    //frame.add(panel)
    frame.add(panel);

    frame.setVisible(true);

    //监听事件,监听窗口关闭事件 System.exit(0)
    frame.addWindowListener(new WindowAdapter() {
        //窗口点击关闭需要做的事情
        @Override
        public void windowClosing(WindowEvent e) {
            //结束程序
            System.exit(0);//1是非正常终止
        }
    });

}

}

结果如下:点击叉叉是可以关闭的;而且可见,Panel的起点即使为0,0也不跟Frame重合,y方向起始有一段距离大约40。

Untitled

05:3种布局管理器

流式布局

代码如下:

package com.maynerd.lesson01;

import java.awt.*;

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());//默认三个button居中
frame.setLayout(new FlowLayout(FlowLayout.LEFT));//添加参数设置左对齐
// frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//添加参数设置右对齐

    frame.setSize(200,200);

    //把按钮都添加上去
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);

    frame.setVisible(true);
}

}

运行结果:

Untitled

方位/边缘布局

代码如下:

package com.maynerd.lesson01;

import java.awt.*;

public class TestBorder {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");

    Button button1 = new Button("East");
    Button button2 = new Button("West");
    Button button3 = new Button("South");
    Button button4 = new Button("North");
    Button button5 = new Button("Center");

    frame.add(button1, BorderLayout.EAST);
    frame.add(button2, BorderLayout.WEST);
    frame.add(button3, BorderLayout.SOUTH);
    frame.add(button4, BorderLayout.NORTH);
    frame.add(button5, BorderLayout.CENTER);

    frame.setSize(200,200);
    frame.setVisible(true);
}

}

结果如下:

Untitled

表格布局

代码如下:

package com.maynerd.lesson01;

import java.awt.*;

public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");

    Button button1 = new Button("button1");
    Button button2 = new Button("button2");
    Button button3 = new Button("button2");
    Button button4 = new Button("button4");
    Button button5 = new Button("button5");
    Button button6 = new Button("button6");

    frame.setLayout(new GridLayout(3,2));

    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.add(button4);
    frame.add(button5);
    frame.add(button6);

    frame.pack();//根据布局大小来调整窗体的尺寸
    frame.setVisible(true);
}

}

结果如下:

Untitled

06:课堂练习讲解及总结

思考题:布局嵌套,具体如图:

Untitled

我的思路:

Untitled

代码如下:

package com.maynerd.lesson01;

import java.awt.*;

public class ThinkProblem {
public static void main(String[] args) {
Frame frame = new Frame("思考题布局!解答");
//三个Panel,panel3包含其他两个panel
Panel panel1 = new Panel(new GridLayout(2,1));
Panel panel2 = new Panel(new GridLayout(2,2));
Panel panel3 = new Panel(new GridLayout(2,3));

    //8个按钮
    Button button1 = new Button("button1");
    Button button2 = new Button("button2");
    Button button3 = new Button("button3");
    Button button4 = new Button("button4");
    Button button5 = new Button("button5");
    Button button6 = new Button("button6");
    Button button7 = new Button("button7");
    Button button8 = new Button("button8");
    Button button9 = new Button("button9");
    Button button10 = new Button("button10");

    //两行一列
    panel1.add(button1);
    panel1.add(button2);

    //两行两列
    panel2.add(button3);
    panel2.add(button4);
    panel2.add(button5);
    panel2.add(button6);

    //上面一半,流式
    panel3.add(button7);
    panel3.add(panel1);
    panel3.add(button8);

    //下面一半,流式
    panel3.add(button9);
    panel3.add(panel2);
    panel3.add(button10);

    //装一起
    frame.add(panel3);
    /*
    也可以最后不通过panel3装好,直接装进frame中也可以,可以省下一个panel
     */
    frame.pack();
    frame.setVisible(true);
}

}

总结:

  • Frame是一个顶级窗口
  • Panel无法单独显示,必须添加到某个容器中。
  • 大小、定位、背景颜色、可见性、是否自动调整窗体尺寸、是否可以改变窗体尺寸、 设置布局格式、监听。

07:事件监听

监听:当某个事件发生的时候,该干什么?

tip:一个事件源可以注册多个监听者,一个监听者也可以监听多个事件源。

代码如下:

package com.maynerd.lesson02;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

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 TestAction {
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);
    windowClose(frame);//关闭窗体

}
//关闭窗体事件的方法
private static void windowClose(Frame frame){
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

}
//按钮的事件监听
class MyActionListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("000");
}

}

结果如下:

Untitled

两个按钮实现同一个监听:

代码如下:

package com.maynerd.lesson02;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestActionEvent02 {
public static void main(String[] args) {
//两个按钮实现同一个监听
//开始按钮 停止按钮
Frame frame = new Frame("开始-停止");
Button button1 = new Button("up");
Button button2 = new Button("down");

    //可以显式地定义 触发 会返回的命令
    //如果不定义,会走默认的值
    //可以多个按钮,只写一个监听类
    button2.setActionCommand("command");

    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("我被点击了" + e.getActionCommand());

// if (e.getActionCommand().equals("up")){
// //根据按钮来完成达成相应的动作
// }
}
}

08:输入框事件监听

做一个计算器,需要输入框(也需要被监听),数字当按钮。

ActionEvent e; e.getSourece的作用:获得一些资源,返回一个对象,而且对象为Object类型,将它强制转换为TestField,可以get文本,也就是输入框输入的文本。

package com.maynerd.lesson02;

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);

    //监听这个文本框输入的文字
    MyActionListener02 actionListener02 = new MyActionListener02();
    //摁下enter 就会触发这个输入框的事件
    textField.addActionListener(actionListener02);

    //设置输入字母隐藏
    textField.setEchoChar('*');

    pack();
    setVisible(true);
}

}
class MyActionListener02 implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
    //获得一些资源,返回的一个对象,是Object类
    TextField source = (TextField)e.getSource();
    //获得输入框的文本
    System.out.println(source.getText());

    source.setText("");//实现输入完以后,清空
}

}

09:简易计算器、组合+内部类回顾复习

oop原则:组合,大于继承!

class A extends B{
			//继承
}
class A{
			//组合
			public B  b;
}
//写法一
package com.maynerd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TextCalculation {
public static void main(String[] args) {
new Calculation();
}
}

//计算器类
class Calculation extends Frame {
public Calculation(){
//三个文本框
TextField textField1 = new TextField(10);//字符数
TextField textField2 = new TextField(10);//字符数
TextField textField3 = new TextField(20);//字符数
// 一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculationListener(textField1,textField2,textField3));
// 一个标签
Label label = new Label("+");

    add(textField1);
    add(label);
    add(textField2);
    add(button);
    add(textField3);

    setVisible(true);
    setLayout(new FlowLayout());
    pack();
}

}
//监听器类
class MyCalculationListener implements ActionListener{
//获取三个变量
private TextField textField1,textField2,textField3;
public MyCalculationListener(TextField textField1,TextField textField2,TextField textField3){
this.textField1 = textField1;
this.textField2 = textField2;
this.textField3 = textField3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得两个加数
int n1 = Integer.parseInt(textField1.getText());
int n2 = Integer.parseInt(textField2.getText());
// 2.将这个值加法运算后放到第三个框
textField3.setText("" + (n1 + n2));
// 3.清除前两个方法
textField1.setText("");
textField2.setText("");
}
}

完全改造为面向对象写法:

//写法二
package com.maynerd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TextCalculation{
public static void main(String[] args) {
new Calculation().loadFrame();
}
}
class Calculation extends Frame{
TextField textField1,textField2,textField3;
public void loadFrame(){
//三个文本框
textField1 = new TextField(10);//字符数
textField2 = new TextField(10);//字符数
textField3 = new TextField(20);//字符数
// 一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculationListener(this));
// 一个标签
Label label = new Label("+");

    add(textField1);
    add(label);
    add(textField2);
    add(button);
    add(textField3);

    setVisible(true);
    setLayout(new FlowLayout());
    pack();
}

}
class MyCalculationListener implements ActionListener{
Calculation calculation = null;

public MyCalculationListener(Calculation calculation) {
    this.calculation = calculation;
}

@Override
public void actionPerformed(ActionEvent e) {
    String text1 = calculation.textField1.getText();
    int r1 = Integer.parseInt(text1);
    String text2 = calculation.textField2.getText();
    int r2 = Integer.parseInt(text2);
    calculation.textField3.setText("" + (r1 + r2));
    calculation.textField1.setText("");
    calculation.textField2.setText("");
}

}

再改造为内部类写法(更好的包装):

//写法三
package com.maynerd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TextCalculation{
public static void main(String[] args) {
new Calculation().loadFrame();
}
}
class Calculation extends Frame{
TextField textField1,textField2,textField3;
public void loadFrame(){
//三个文本框
textField1 = new TextField(10);//字符数
textField2 = new TextField(10);//字符数
textField3 = new TextField(20);//字符数
// 一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculationListener());
// 一个标签
Label label = new Label("+");

    add(textField1);
    add(label);
    add(textField2);
    add(button);
    add(textField3);

    setVisible(true);
    setLayout(new FlowLayout());
    pack();
}
private class MyCalculationListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        String text1 = textField1.getText();
        int r1 = Integer.parseInt(text1);
        String text2 = textField2.getText();
        int r2 = Integer.parseInt(text2);
        textField3.setText("" + (r1 + r2));
        textField1.setText("");
        textField2.setText("");
    }
}

}

运行结果:

Untitled

10:画笔paint

画圆,画矩形:

package com.maynerd.lesson03;

import java.awt.*;

public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame(){
setVisible(true);
setBounds(200,200,600,500);
}
@Override
public void paint(Graphics g) {
//画笔需要有颜色,画笔可以画画
// g.setColor(Color.red);
g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100);

// g.setColor(Color.green);
g.fillRect(100,200,100,100);
//假设让xy动起来,画笔不断重新画,就能动起来,每隔几秒画一次,用到timer
//养成习惯,画笔用完,还原到最初的颜色
}
}

运行结果:

Untitled

11:鼠标监听事件

目的:想要实现鼠标的画画。

代码如下:

package com.maynerd.lesson03;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyMouseFrame("画图");
}
}
class MyMouseFrame extends Frame {
//画画需要画笔,需要监听鼠标的当前位置,需要集合来存储这个点

ArrayList points;

public MyMouseFrame(String title) {
    super(title);
    setBounds(200,200,400,300);
    //存鼠标点击的点
    points = new ArrayList<>();

    //鼠标监听器,监听这个窗口
    this.addMouseListener(new MyMouserListener());
    setVisible(true);
}

@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);
    }
}
private class MyMouserListener extends MouseAdapter{
    //鼠标 按下、弹起、按住不放
    @Override
    public void mousePressed(MouseEvent e) {
        MyMouseFrame frame = (MyMouseFrame) e.getSource();
        //放我们点击的时候,就会在界面上产生一个点!画的
        points.add(new Point(e.getX(),e.getY()));
        //每次点击鼠标,都要重画一遍
        repaint();
    }
}

}

结果如下:

Untitled

12:窗口监听事件

目的:匿名内部类写法,实现窗口的激活与关闭监听。

代码如下:

package com.maynerd.lesson03;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Locale;

public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setVisible(true);
setBounds(100,100,200,200);
setBackground(Color.red);
// addWindowListener(new MyWindowListener());
//匿名内部类写法
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("WindowClosing");
System.exit(0);
}

        @Override
        public void windowActivated(WindowEvent e) {
            WindowFrame source = (WindowFrame) e.getSource();
            source.setTitle("被激活了!");
            System.out.println("WindowActived");
        }
    });
}

// class MyWindowListener extends WindowAdapter{
// @Override
// public void windowClosing(WindowEvent e) {
// //先隐藏再关闭
// setVisible(false);//隐藏窗口,通过按钮隐藏窗口
// System.exit(0);//exit正常退出
// }
// }
}

结果如下:

Untitled

13:键盘监听事件

监听键盘输入的字母:

代码如下:

package com.maynerd.lesson03;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

//键
public class TestKeyListener {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame{
public MyKeyFrame(){
setBounds(1,2,400,300);
setVisible(true);

    addKeyListener(new KeyAdapter() {
        //键盘按下
        @Override
        public void keyPressed(KeyEvent e) {
            //获得键盘按下的是哪一个键,当前键盘的码
            int keyCode = e.getKeyCode();
            System.out.println(keyCode);

// System.out.println(e.getKeyCode());
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键!");
}
}
});
}
}

结果如下:

Untitled

14:Swing之Jframe窗体

  • 有窗口和面板。
  • JFrame有升级的窗口关闭的方法:frame.setDefaultCloseOperation(WindowConstants.*EXIT_ON_CLOSE*);
  • 后面的常数:有四种,有隐藏关闭,有退出关闭... ...
  • 容器实例化,窗体是顶级容器,而给容器设置颜色才是真正的BackgroundColor

代码如下:

package com.maynerd.lesson04;

import javax.swing.;
import java.awt.
;

public class JFrameDemo {
//初始化
public void init(){
//顶级容器
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
frame.setBackground(Color.red);//没有生效

    //容器实例化,窗体是定义容器,而给容器设置颜色才是真正的backgroundcolor
    frame.getContentPane().setBackground(Color.red);

    //设置文字JLabel
    JLabel jLabel = new JLabel("欢迎来到狂神的节目@!");
    frame.add(jLabel);
    //设置居中对齐
    jLabel.setHorizontalAlignment(SwingConstants.CENTER);

    //关闭事件,类升级了
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
    //建立一个窗口
    new JFrameDemo().init();
}

}

结果如下:

Untitled

15:JDialog弹窗

代码如下:

package com.maynerd.lesson04;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    //JFrame放东西,需要一个容器,需要get到
    Container container = this.getContentPane();
    //绝对布局
    container.setLayout(null);
    //按钮
    JButton button = new JButton("点击弹出对话框!");
    //设置了绝对布局,那么这些坐标都是相对于当前这个布局来的,加进去会相对容器自动定位
    button.setBounds(30,30,200,200);

    //点击按钮的时候弹出另外一个弹窗
    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(30,50,500,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("欢迎来到对抗路!"));
}
}

结果如下:

Untitled

总结:JDialog用来弹出,默认自带关闭的方法,不必另外添加关闭的方法,设置弹窗中的Label需要setBounds指定坐标,否则在绝对布局下不显示。

16:Icon、ImageIcon标签

  • 标签可以进化一点变成图片标签。
  • 除了标签还有面板,滚动框就是属于面板这里边的。
  • 标签基础语法:Label jLabel = new JLabel();

图标Icon类:包含三个方法,获得图标宽、高以及画出图标。

Untitled

代码如下:

package com.maynerd.lesson04;

import javax.swing.;
import java.awt.
;

//图标,需要实现Icon类,Frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;

//无参构造
public IconDemo() {

}
//有参构造
public IconDemo(int width, int height) {
    this.width = width;
    this.height = height;
}

public void init(){
    IconDemo iconDemo = new IconDemo(15, 15);
    //图标可以放在标签上,也可以放在按钮上,这里是标签
    JLabel jLabel = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
    Container container = getContentPane();
    container.add(jLabel);

    this.setVisible(true);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setBounds(0,0,200,200);

}
public static void main(String[] args) {
    new IconDemo().init();
}

@Override
public int getIconWidth() {
    return this.width;
}

@Override
public int getIconHeight() {
    return this.height;
}

//放在哪个组件  要画笔画东西  图标的宽 和  高
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    g.fillOval(x,y,width,height);
}

}

结果如下:为画了一个黑心圆加一个icon字符串。

Untitled

图片ImageIcon类:传参传入一张图片,正常的logo一般比例都会较小。此处将image放在类的同级目录下,调用URL url = ImageIconDemo.class.getResource("QQ图片20200902175012.png");命令即可添加图片进去。

Untitled

package com.maynerd.lesson04;

import javax.swing.;
import java.awt.
;
import java.net.URL;

public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
//获取图片的地址
JLabel jLabel = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("QQ图片20200902175012.png");

    ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突
    jLabel.setIcon(imageIcon);
    jLabel.setHorizontalAlignment(SwingConstants.CENTER);

    Container container = getContentPane();
    container.add(jLabel);

    setVisible(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setBounds(100,100,1000,480);
}

public static void main(String[] args) {
    new ImageIconDemo();
}

}

结果如下:

Untitled

17:文本域、JScroll面板

tip:滚动框属于面板,JS写网站的时候也会学。

JScroll就是上下左右可滚动的面板。

通过container.setLayout(new GridLayout(2,1,10,10));设置间距。

代码如下:

package com.maynerd.lesson05;

import javax.swing.;
import java.awt.
;

public class JPanelDemo extends JFrame {
public JPanelDemo(){
super("MyWindows");
Container container = this.getContentPane();
//后面参数的意思是间距
container.setLayout(new GridLayout(2,1,10,10));
JPanel jPanel1 = new JPanel(new GridLayout(1,3));
JPanel jPanel2 = new JPanel(new GridLayout(1,2));
JPanel jPanel3 = new JPanel(new GridLayout(2,1));
JPanel jPanel4 = new JPanel(new GridLayout(3,2));

    jPanel1.add(new JButton("1"));
    jPanel1.add(new JButton("2"));
    jPanel1.add(new JButton("3"));

    jPanel2.add(new JButton("4"));
    jPanel2.add(new JButton("5"));

    jPanel3.add(new JButton("6"));
    jPanel3.add(new JButton("7"));

    jPanel4.add(new JButton("8"));
    jPanel4.add(new JButton("9"));
    jPanel4.add(new JButton("10"));
    jPanel4.add(new JButton("11"));
    jPanel4.add(new JButton("12"));
    jPanel4.add(new JButton("13"));

    container.add(jPanel1);
    container.add(jPanel2);
    container.add(jPanel3);
    container.add(jPanel4);
    this.setVisible(true);
    this.setSize(500,500);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new JPanelDemo();
}

}

结果如下:

Untitled

文本域+JScroll面板演示:

代码如下:

package com.maynerd.lesson05;

import javax.swing.;
import java.awt.
;

public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();

    //文本域
    JTextArea textArea = new JTextArea();
    textArea.setText("欢迎来到对抗路!");

    //Scroll面板,把textarea丢到JScroll面板里面,把JScroll面板丢到容器里面
    JScrollPane jScrollPane = new JScrollPane(textArea);
    container.add(jScrollPane);

    this.setVisible(true);
    this.setBounds(100,300,300,350);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new JScrollDemo();
}

}

结果如下:

Untitled

18:图片按钮、单选框、多选框

tip:JFrame需要先得到一个容器,用Swing就不需要,用AWT也不需要。

图片按钮

代码如下:

package com.maynerd.lesson05;

import javax.swing.;
import java.awt.
;
import java.net.URL;

public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container container = this.getContentPane();
//将一个图片变成一个图标
URL url = JButtonDemo01.class.getResource("666.png");
ImageIcon imageIcon = new ImageIcon(url);
//把这个图标放在一个按钮上
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
//提示文本
jButton.setToolTipText("图片按钮");

    //add
    container.add(jButton);
    this.setVisible(true);
    this.setSize(500,300);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new JButtonDemo01();
}

}

结果如下:

Untitled

单选框如何实现?

利用buttonGroupJbutton分组,同一组的就只能单选,不分组默认三组,都可以选。

package com.maynerd.lesson05;

import javax.swing.;
import java.awt.
;
import java.net.URL;

public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container container = this.getContentPane();
//将一个图片变成一个图标
URL url = JButtonDemo02.class.getResource("666.png");
ImageIcon imageIcon = new ImageIcon(url);

   //单选框
    JRadioButton jRadioButton01 = new JRadioButton("jRadioButton01");
    JRadioButton jRadioButton02 = new JRadioButton("jRadioButton02");
    JRadioButton jRadioButton03 = new JRadioButton("jRadioButton03");
    //由于单选框只能选择一个,所以我们一般会把它们分在一个组,来实现单选,正常情况下。三个组都能选
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(jRadioButton01);
    buttonGroup.add(jRadioButton02);
    buttonGroup.add(jRadioButton03);

    //add
    container.add(jRadioButton01,BorderLayout.NORTH);
    container.add(jRadioButton02,SwingConstants.CENTER);
    container.add(jRadioButton03,BorderLayout.SOUTH);

    this.setVisible(true);
    this.setSize(500,300);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new JButtonDemo02();
}

}

多选框如何实现?

利用JCheckBox 多选框方法即可。

注意如果用方位边缘布局,需要在参数框中指定方位,北、中、南将在左边出现三个选项;如果用流式布局,需要将当前的JFrame设置为流式布局,再指定左对齐还是右对齐还是垂直居中对齐。

package com.maynerd.lesson05;

import javax.swing.;
import java.awt.
;
import java.net.URL;

public class JButtonDemo03 extends JFrame {
public JButtonDemo03(){
this.setLayout(new FlowLayout(FlowLayout.LEFT));
Container container = this.getContentPane();
//将一个图片变成一个图标
URL url = JButtonDemo03.class.getResource("666.png");
ImageIcon imageIcon = new ImageIcon(url);

    //多选框
    JCheckBox jCheckBox01 = new JCheckBox("jCheckBox01");
    JCheckBox jCheckBox02 = new JCheckBox("jCheckBox02");
    JCheckBox jCheckBox03 = new JCheckBox("jCheckBox03");

    //add
    container.add(jCheckBox01);
    container.add(jCheckBox02);
    container.add(jCheckBox03);
    //方位布局,北、中、南将在左边三个选项

// container.add(jCheckBox01,BorderLayout.NORTH);
// container.add(jCheckBox02,BorderLayout.CENTER);
// container.add(jCheckBox03,BorderLayout.SOUTH);

    this.setVisible(true);
    this.setSize(500,300);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new JButtonDemo03();
}

}

结果如下:

Untitled

19:下拉框、列表框

下拉框:用JComboBox实现。

代码如下:

package com.maynerd.lesson06;

import javax.swing.;
import java.awt.
;

public class TestComboBoxDemo01 extends JFrame {
public TestComboBoxDemo01(){
Container container = this.getContentPane();
JComboBox jComboBox = new JComboBox();

    jComboBox.addItem(null);
    jComboBox.addItem("正在上映");
    jComboBox.addItem("已下架");
    jComboBox.addItem("即将上映");
    container.add(jComboBox);

    this.setVisible(true);
    this.setSize(500,350);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TestComboBoxDemo01();
}

}

结果如下:

Untitled

列表框:用jList 实现,可以使用String数组静态初始化内容,也可以用向量动态添加,传入容器container的只是引用,即可以先传入数据结构到容器,再动态添加。

代码如下:

package com.maynerd.lesson06;

import javax.swing.;
import java.awt.
;

public class TestComboBoxDemo02 extends JFrame {
public TestComboBoxDemo02(){
Container container = this.getContentPane();

    //生成列表的内容,静态添加
    String[] contents = {"1","2","3","4"};
    //列表中需要放入内容
    JList jList = new JList(contents);

    container.add(jList);

    this.setVisible(true);
    this.setSize(500,350);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TestComboBoxDemo02();
}

}

结果如下:

Untitled

总结:下拉框一般引用在选择地区或者一些单个选项,如果只有2个建议用单选框去做,多于2个建议用下拉框去做;列表一般用来展示信息,一般是动态扩容的。

20:文本框、密码框、文本域

文本框

代码如下:

package com.maynerd.lesson06;

import javax.swing.;
import java.awt.
;

public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();

    JTextField jTextField01 = new JTextField("hello");//可以指定默认文本
    JTextField jTextField02 = new JTextField("world",10);//设置文本框宽度

    container.setLayout(new FlowLayout());//流式布局
    container.add(jTextField01);
    container.add(jTextField02);

    this.setVisible(true);
    this.setSize(500,350);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TestTextDemo01();
}

}

结果如下:

Untitled

密码框:除了手动设置密码框,可以直接生成密码框。

代码如下:

package com.maynerd.lesson06;

import javax.swing.;
import java.awt.
;

public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = this.getContentPane();

    JPasswordField jPasswordField = new JPasswordField();

// jPasswordField.setEchoChar('*');

    container.add(jPasswordField);

    this.setVisible(true);
    this.setSize(500,350);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TestTextDemo02();
}

}

结果如下:

Untitled

Untitled

文本域:在[JScroll面板](https://www.notion.so/GUI-56368e4a4e3c47d9b2a6e95c5d22588b)(点击跳转超链接)中已经介绍过。

21-26:贪吃蛇之界面控制

前提知识:

  • 帧(动画):如果时间片足够小,就是动画,一秒60帧。
  • 键盘监听
  • 定时器Timer
package com.maynerd.SnakePractice;

import javax.swing.*;

public class SnakePractice extends JFrame{
public SnakePractice() {
this.setBounds(10,10,900,720);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    //正常游戏界面应该在面板上
    this.add(new GamePanel());
    this.setVisible(true);
}

public static void main(String[] args) {
    new SnakePractice();
}

}

package com.maynerd.SnakePractice;

import com.maynerd.SnakePractice.Data;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//游戏面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//    定义蛇的数据结构
    int length;//蛇的长度
    int[] snakeX = new int[600];//蛇的X坐标
    int[] snakeY = new int[500];//蛇的Y坐标
    String dir;//初始化方向向右
    //游戏当前状态:开始 停止
    boolean isStart;//默认是不开始
    boolean isFail;//默认不失败状态

    //食物的坐标
    int foodx;
    int foody;

    int score;
    Random random = new Random();

    //定时器:只需要实现一个接口ActionListener,重写一个方法
    Timer timer = new Timer(100,this);//100ms刷新一次,监听this这个对象

    public GamePanel() {
        init();
        //获得焦点和键盘事件:打游戏肯定要专注在窗口上
        this.setFocusable(true);
        this.addKeyListener(this);//获得键盘监听事件
        timer.start();//游戏一开始就启动
    }

    //初始化方法
    public void init(){
        isStart = false;
        isFail = false;
        score = 0;
        length = 3;
        snakeX[0] = 100;snakeY[0] = 100;//脑袋
        snakeX[1] = 75;snakeY[1] = 100;//身体第一段
        snakeX[2] = 50;snakeY[2] = 100;//身体第二段
        dir = "R";//初始化默认方向

        //食物随机分布在界面上
        foodx = 25 + 25 * random.nextInt(34);
        foody = 75 + 25 * random.nextInt(24);
    }

    //绘制面板,我们游戏中的所有东西,都是用这个画笔来画
    @Override
    public void paint(Graphics g) {
        super.paintComponent(g);//清屏,否则会闪烁

        //绘制静态的面板
        Data.header.paintIcon(this,g,25,11);//头部广告栏画上去
        g.fillRect(20,75,850,600);//默认的游戏界面
        this.setBackground(Color.white);

        //把小蛇画上去
        if (dir.equals("R")) {
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        } else if(dir.equals("L")) {
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        } else if(dir.equals("U")) {
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        } else if(dir.equals("D")) {
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i] );
        }
//        Data.body.paintIcon(this,g,snakeX[1],snakeY[1]);
//        Data.body.paintIcon(this,g,snakeX[2],snakeY[2]);
        //画积分
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度:" + length,750,35);
        g.drawString("分数:" + score,750,55);
        //画食物
        Data.food.paintIcon(this,g,foodx,foody);

        //游戏状态
        if (isStart == false){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏!",300,300);
        }
        if (isFail == true){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("您已失败,按下空格重新开始!",205,300);
        }

    }

    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_SPACE){//如果按下的是空格键
            if (isFail){
                //重新开始
                isFail = false;
                init();//重新初始化,数据恢复到最初。
            }else{
                isStart = !isStart;//取反
                repaint();
            }
        }
        //小蛇移动
        if (keyCode == KeyEvent.VK_UP){
            dir = "U";
        }else if (keyCode == KeyEvent.VK_DOWN){
            dir = "D";
        }else if (keyCode == KeyEvent.VK_LEFT){
            dir = "L";
        }else if (keyCode == KeyEvent.VK_RIGHT){
            dir = "R";
        }
    }

    //事件监听:需要通过固定的时间来刷新,1s=10次
    @Override
    public void actionPerformed(ActionEvent e) {
        if(isStart == true && isFail == false){//如果游戏是开始状态,让它动起来
            //吃食物
            if (snakeX[0] == foodx && snakeY[0] == foody){
                length++;//长度+1
                score += 10;//分数+10
                //再次随机食物
                foodx = 25 + 25 * random.nextInt(34);
                foody = 75 + 25 * random.nextInt(24);
            }
            //默认右移
            for (int i = length - 1;i > 0;i--) {//后一节移动到前一节的位置
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }
            //移动时,只有脑袋需要改变,后面身体都是后一节跟着前一节节奏
            if (dir.equals("R")){
                snakeX[0] += 25;
                if (snakeX[0] > 850) snakeX[0] = 25;
            }else if (dir.equals("L")){
                snakeX[0] -= 25;
//                            System.out.println("lll");

                if (snakeX[0] < 25) snakeX[0] = 850;
            } else if (dir.equals("U")) {
                snakeY[0] -= 25;
                if (snakeY[0] < 75) snakeY[0] = 650;
            } else if (dir.equals("D")) {
                snakeY[0] += 25;
                if (snakeY[0] > 650) snakeY[0] = 75;
            }

            //失败判定,撞到自己算失败
            for (int i = 1; i < length; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
                    isFail = true;
                }
            }

            repaint();//重画页面
        }
        timer.start();//定时器开启
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
}
package com.maynerd.SnakePractice;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

//数据中心
public class Data {
    public static URL headerUrl = Data.class.getResource("Image/header.png");
    public static URL upUrl = Data.class.getResource("Image/up.png");
    public static URL downUrl = Data.class.getResource("Image/down.png");
    public static URL leftUrl = Data.class.getResource("Image/left.png");
    public static URL rightUrl = Data.class.getResource("Image/right.png");
    public static URL bodyUrl = Data.class.getResource("Image/body.png");
    public static URL foodUrl = Data.class.getResource("Image/food.png");

    public static ImageIcon up = new ImageIcon(upUrl);
    public static ImageIcon down = new ImageIcon(downUrl);
    public static ImageIcon right = new ImageIcon(rightUrl);
    public static ImageIcon left = new ImageIcon(leftUrl);
    public static ImageIcon header = new ImageIcon(headerUrl);
    public static ImageIcon body = new ImageIcon(bodyUrl);
    public static ImageIcon food = new ImageIcon(foodUrl);
}

结果如下:

Untitled

Untitled

posted @ 2022-03-05 17:30  Maynerd  阅读(60)  评论(0编辑  收藏  举报