GUI编程(纯属个人喜好)
窗口、弹窗、面板、文本框、列表框、按钮、图片、监听事件、鼠标、键盘事件、破解工具
1.简介
GUI核心技术:Swing 、AWT、界面不美观
GUI编程的学习主要是一些思想:MVC架构,了解监听
2.AWT
2.1简单介绍
awt是抽象的窗口工具,包含了很多的类和接口。
组件Component:基本组件button、textArea、Label等;容器有Container,Window(Frame、Dialog)和面板(Applet)
2.1组件
1.Frame
public static void main(String[] args) {
Frame frame = new Frame("我的第一个java图像界面窗口");
// 设置可见性
frame.setVisible(true);
// 设置窗口大小
frame.setSize(400, 400);
// 设置背景颜色
// Color color = new Color();
frame.setBackground(new Color(85, 105, 68));
// 弹出初始位置
frame.setLocation(200, 200);
// 设置大小固定
frame.setResizable(false);
}
多个Frame
public class TestFrame2 {
public static void main(String[] args) {
MyFrame frame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame frame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame frame4 = new MyFrame(100, 300, 200, 200, Color.darkGray);
MyFrame frame3 = new MyFrame(300, 300, 200, 200, Color.gray);
}
}
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.Pannel
//Pannel 可以看做是一个空间,不能单独存在,需要嵌入frame中
public class TestPannel {
public static void main(String[] args) {
Frame frame = new Frame();
// 布局
Panel panel = new Panel();
// 设置布局,布局出设置需要设置frame的布局,否则会被覆盖
frame.setLayout(null);
// frame设置坐标
frame.setBounds(300, 300, 500, 500);
frame.setBackground(new Color(40, 161, 35));
// panel设置坐标
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(193, 15, 60));
// frame中嵌入面板
frame.add(panel);
// 设置可见性
frame.setVisible(true);
// 监听事件,监听窗口关闭时间,采用的是适配器模式,只需要实现自己需要的方法计科
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3.布局管理器
流式布局
public static void main(String[] args) {
Frame frame = new Frame();
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn1");
Button btn3 = new Button("btn1");
// 设置流式布局
frame.setLayout(new FlowLayout());
// 添加按钮
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.setSize(200, 200);
frame.setVisible(true);
}
东西南北中布局:
public static void main(String[] args) {
Frame frame = new Frame("布局管理");
Button east = new Button("East");
Button west = new Button("West");
Button north = new Button("North");
Button south = new Button("south");
Button center = new Button("Center");
// 将按钮放入窗体中
frame.add(east, BorderLayout.EAST);
frame.add(west, BorderLayout.WEST);
frame.add(north, BorderLayout.NORTH);
frame.add(south, BorderLayout.SOUTH);
frame.add(center, BorderLayout.CENTER);
//设置窗口的大小
frame.setSize(400, 400);
//设置窗口的位置
frame.setLocation(200, 200);
frame.setVisible(true);
// 添加关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
GridLayout
public static void main(String[] args) {
Frame frame = new Frame("布局管理");
Button east = new Button("按钮1");
Button west = new Button("按钮2");
Button north = new Button("按钮3");
Button south = new Button("按钮4");
Button center = new Button("按钮5");
// 设置按钮填入三行两列的形式
frame.setLayout(new GridLayout(3, 2));
// 将按钮放入窗体中
frame.add(east);
frame.add(west);
frame.add(north);
frame.add(south);
frame.add(center);
// java的函数
frame.pack();
frame.setVisible(true);
// 设置位置
frame.setLocation(200, 200);
// 设置大小
frame.setSize(400, 400);
// 设置窗口关闭事件监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
练习题:
自己先编写,代码:
public static void main(String[] args) {
Frame frame = new Frame("练习小窗口");
// 设置大小
frame.setSize(400, 400);
// 设置位置
frame.setLocation(200, 200);
// 创建按钮
Button btn1 = new Button("button1");
Button btn2 = new Button("button2");
Button btn3 = new Button("button3");
Button btn4 = new Button("button4");
Button btn5 = new Button("button5");
Button btn6 = new Button("button6");
Button btn7 = new Button("button7");
Button btn8 = new Button("button8");
Button btn9 = new Button("button9");
Button btn10 = new Button("button10");
// 创建两个pannel用来存放按钮
Panel panel1 = new Panel(new GridLayout(2, 1));
Panel panel2 = new Panel(new GridLayout(2, 2));
// 设置背景颜色
panel1.setBackground(Color.YELLOW);
panel2.setBackground(Color.DARK_GRAY);
// 一次向frame嵌入组件
frame.add(btn1);
// 嵌入panel组件
frame.add(panel1);
frame.add(btn2);
frame.add(btn3);
// 嵌入panel组件
frame.add(panel2);
panel1.add(btn5);
panel1.add(btn6);
panel2.add(btn7);
panel2.add(btn8);
panel2.add(btn9);
panel2.add(btn10);
frame.add(btn4);
// 设置窗大小
frame.setVisible(true);
// 设置窗体两行三列
frame.setLayout(new GridLayout(2, 3));
// 设置窗口关闭监听事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
4.事件监听
public class TestActiontwo {
public static void main(String[] args) {
Frame frame = new Frame("事件监听");
Button btn1 = new Button("start");
Button btn2 = new Button("stop");
frame.setSize(400, 400);
frame.setLocation(200, 200);
btn1.setActionCommand("task.start");
btn2.setActionCommand("task.stop");
// 两个按钮共享一个事件
MyMoniter monitor = new MyMoniter();
btn1.addActionListener(monitor);
btn2.addActionListener(monitor);
frame.add(btn1, BorderLayout.NORTH);
frame.add(btn2, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
public static void windowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMoniter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击:msg = " + e.getActionCommand());
}
}
文本框练习:
public class TestText01 {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame() {
TextField textField = new TextField();
add(textField);
// 监听文字输入文本框
MyActionListener2 action = new MyActionListener2();
// 回车键按下时,会触发该事件发生
textField.addActionListener(action);
// 替换编码
textField.setEchoChar('*');
setSize(400, 400);
setLocation(200, 200);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 获取一些资源
TextField text = (TextField) e.getSource();
System.out.println(text.getText());
text.setText("");
}
}
小练习:一个label、三个text、一个按钮制作一个简单的+法操作
package com.itxing.lession02;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @Class_Name CaculateDemo
* @Class_Desc (简单的小实验)
* @author it星
* @date 2020年5月27日下午3:52:59
* @Version
*/
public class CaculateDemo {
public static void main(String[] args) {
new Caclutate().loadFrame();
}
}
//设计计算器类
class Caclutate extends Frame {
TextField num1 = null;
TextField num2 = null;
TextField num3 = null;
public void loadFrame() {
// 三个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
// 一个按钮
Button btn = new Button("=");
// 一个便签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
// 按钮添加监听事件
btn.addActionListener(new MyCaculAction(this));
add(num1);
add(label);
add(num2);
add(btn);
add(num3);
setSize(500, 300);
setLocation(200, 200);
setVisible(true);
// 窗口关闭时间
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//使用组合模式
class MyCaculAction implements ActionListener {
// 获取三个变量
// private TextField num1, num2, num3;
Caclutate caclu = null;
MyCaculAction(Caclutate caclu) {
this.caclu = caclu;
}
@Override
public void actionPerformed(ActionEvent e) {
// 获取加数和被加数
int n1 = Integer.parseInt(caclu.num1.getText());
int n2 = Integer.parseInt(caclu.num2.getText());
// 获取+运算后放入第三个框
caclu.num3.setText("" + (n1 + n2));
// 删除前两个框的内容
caclu.num1.setText("");
caclu.num2.setText("");
}
}
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame() {
setBounds(200, 200, 600, 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.drawOval(100, 100, 100, 100);
g.fillOval(100, 100, 100, 100);
g.setColor(Color.green);
g.fillRect(200, 200, 100, 100);
// 画笔用完将其还原
}
}
鼠标监听事件:
package com.itxing.lession03;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
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;
/**
* @Class_Name TestMouseListener
* @Class_Desc (鼠标监听,画笔每次只能画一次,因此每次点击完需要重新repaint)
* @author it星
* @date 2020年5月27日下午4:09:12
* @Version
*/
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("鼠标点击事件");
}
}
//自己的窗体类
class MyFrame extends Frame {
ArrayList<Point> points = null;
public MyFrame(String name) {
super(name);
setBounds(200, 200, 400, 300);
// 存储鼠标点击的位置
points = new ArrayList<>();
setVisible(true);
// 鼠标监听事件
this.addMouseListener(new MyMouseListener());
// 窗口关闭事件
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
// 画笔,监听事件
Iterator<Point> iterator = points.iterator();
while (iterator.hasNext()) {
Point point = iterator.next();
// g.setColor(Color.RED);
g.setColor(Color.RED);
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();
// 点击时,界面画笔上产生一个点
addPaint(new Point(e.getX(), e.getY()));
// 鼠标点击后重新绘制
frame.repaint();
}
}
}
窗口监听:
package com.itxing.lession03;
import java.awt.Color;
import java.awt.Frame;
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() {
setBackground(Color.blue);
setBounds(100, 100, 200, 200);
setVisible(true);
// addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("您点击关闭");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活");
System.out.println("windowsActivited");
}
});
}
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);// 隐藏窗口
System.exit(0);// 正常退出
}
}
}
键盘监听:
package com.itxing.lession03;
import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TextKeyListener {
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
Swing对awt进行了封装
package com.itxing.swinglession;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class JframeDemo02 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame {
public void init() {
this.setBounds(10, 10, 200, 300);
this.setVisible(true);
// 设置文字
JLabel jLabel = new JLabel("欢迎来到java的学习");
this.add(jLabel);
// 让文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 获取一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.YELLOW);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
弹窗:
package com.itxing.swinglession;
import java.awt.Container;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
* @Class_Name DialogDemo
* @Class_Desc (弹窗设置)
* @author 冯雁星
* @date 2020年5月27日下午5:43:18
* @Version
*/
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setBounds(100, 100, 200, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// JFrame
Container container = this.getContentPane();
// 按钮
JButton btn = new JButton("点击弹出一个对话框");
btn.setBounds(30, 30, 200, 50);
// 点击按钮创建一个弹窗
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 弹窗
new MyDialogDemo();
}
});
container.add(btn);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialogDemo extends JDialog {
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("好好学习天天向上"));
}
}
标签:JLabel
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
JLabel jLabel = new JLabel("ImageIconDemo");
URL url = ImageIconDemo.class.getResource("dog1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setBounds(200, 200, 400, 400);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板:JPanel
package com.itxing.swingpanel;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
// 获取容器
Container contentPane = this.getContentPane();
// 添加组件
// 创建按钮组件放入Panel中
JButton jButton1 = new JButton("1");
JButton jButton2 = new JButton("2");
JButton jButton3 = new JButton("3");
jPanel1.add(jButton1);
jPanel1.add(jButton2);
jPanel1.add(new JButton("4"));
jPanel1.add(new JButton("5"));
jPanel1.add(new JButton("6"));
jPanel1.add(jButton3);
jPanel1.add(new JButton("7"));
jPanel1.add(new JButton("8"));
jPanel1.add(new JButton("9"));
contentPane.add(jPanel1);
setVisible(true);
setBounds(200, 200, 300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPane侧边栏滚动条
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container contentPane = this.getContentPane();
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("你好,java工程师必须要走的路");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100, 100, 300, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮(单选按钮、多选按钮)
图片按钮:
public class JButtonDemo01 extends JFrame {
JButtonDemo01() {
Container contain = this.getContentPane();
URL resource = JButtonDemo01.class.getResource("3.jpg");
ImageIcon icon = new ImageIcon(resource);
// 把这个图标放在按钮上
JButton jbutton = new JButton();
jbutton.setIcon(icon);
jbutton.setToolTipText("图片按钮");
contain.add(jbutton);
this.setVisible(true);
this.setBounds(300, 300, 300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选框:
public class JButtonDemo02 extends JFrame {
JButtonDemo02() {
Container contain = this.getContentPane();
URL resource = JButtonDemo01.class.getResource("3.jpg");
ImageIcon icon = new ImageIcon(resource);
// 把这个图标放在按钮上
JRadioButton jbtn1 = new JRadioButton("btn1");
JRadioButton jbtn2 = new JRadioButton("btn2");
JRadioButton jbtn3 = new JRadioButton("btn3");
ButtonGroup group = new ButtonGroup();
group.add(jbtn1);
group.add(jbtn2);
group.add(jbtn3);
contain.add(jbtn1, BorderLayout.NORTH);
contain.add(jbtn2, BorderLayout.CENTER);
contain.add(jbtn3, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
多选框:
public class MulButtonDemo extends JFrame {
MulButtonDemo() {
Container contain = this.getContentPane();
URL resource = JButtonDemo01.class.getResource("3.jpg");
Icon icon = new ImageIcon(resource);
// 把这个图标放在按钮上
JCheckBox jCheckBox1 = new JCheckBox("checkBox1");
JCheckBox jCheckBox2 = new JCheckBox("checkBox2");
JCheckBox jCheckBox3 = new JCheckBox("checkBox3");
contain.add(jCheckBox1, BorderLayout.NORTH);
contain.add(jCheckBox2, BorderLayout.CENTER);
contain.add(jCheckBox3, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MulButtonDemo();
}
}
列表:
//下拉框
public class TestComboxDemo01 extends JFrame {
TestComboxDemo01() {
Container container = this.getContentPane();
JComboBox jComBox = new JComboBox();
jComBox.addItem(null);
jComBox.addItem("唱");
jComBox.addItem("跳");
jComBox.addItem("rap");
jComBox.addItem("篮球");
container.add(jComBox);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestComboxDemo01();
}
}
//列表框
public class TestListDemo02 extends JFrame {
TestListDemo02() {
Container container = this.getContentPane();
// 生成列表元素,并将其加入到列表控件
Vector<Object> content = new Vector<>();
content.add("课程1");
content.add("课程2");
content.add("课程3");
JList jList = new JList(content);
container.add(jList);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestListDemo02();
}
}
文本框以及密码框:
public class JTextDemo extends JFrame {
JTextDemo() {
Container container = this.getContentPane();
// 文本框
JTextField jtext = new JTextField("Hello");
JTextField jtext2 = new JTextField("world", 20);
// 密码框
JPasswordField password = new JPasswordField();
password.setEchoChar('*');
container.add(jtext, BorderLayout.NORTH);
container.add(password, BorderLayout.CENTER);
container.add(jtext2, BorderLayout.SOUTH);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JTextDemo();
}
}
小游戏贪吃蛇小游戏:狂神说java
推荐看秦老师的视频学习,通俗易懂,还是要多看书,多看底层知识。