GUI 编程
告诉大家该怎么学?
- 这是什么?
- 它怎么玩
- 该如何去在我们平时运用?
- class-可阅读的
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听时间
- 鼠标
- 键盘事件
- 外挂
- 破解工具
1、简介
Gui的核心技术: Swing AWT
- 因为界面不美观
- 需要jre环境
为什么我们要学习?
- 可以写出自己心中想要的小工具
- 工作时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
2、AWT
2.1 AWT介绍
- 包含了很多类和接口! GUI!
- 元素:窗口,按钮,文本框
- java.awt
2.2 组件和容器
1.2.1 Frame
package com.tree.lesson01;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的java程序");
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setBackground(Color.gray);
frame.setVisible(true);
frame.setResizable(false);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GhtqF6b2-1633706763750)(GUI编程.assets/2325334-20210317161930105-314930371.png)]
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.red);
MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);
MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.blue);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.green);
}
}
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);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rAg1aiIO-1633706763754)(GUI编程.assets/2325334-20210317161947465-1768301913.png)]
1.2.2. Panel(面板)
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
frame.setLayout(null);
frame.setVisible(true);
frame.setBounds(100,100,400,400);
frame.setBackground(Color.red);
panel.setBounds(50,50,300,300);
panel.setBackground(Color.yellow);
frame.add(panel);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wq5jgLHK-1633706763764)(GUI编程.assets/2325334-20210317162003068-2097307029.png)]
2.3 布局管理器
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(300,200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XwQRgazw-1633706763769)(GUI编程.assets/2325334-20210317162013759-127782931.png)]
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
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.setSize(300,200);
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);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U3A4UHWT-1633706763773)(GUI编程.assets/2325334-20210317162025494-1657290476.png)]
public class TestGridLayout {
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");
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.setVisible(true);
frame.pack();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L8ZoyI4B-1633706763777)(GUI编程.assets/2325334-20210317162037528-294596330.png)]
课堂练习
public class TestHomeWork {
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");
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");
button1.setBounds(0,0,100,200);
frame.add(button1);
button2.setBounds(100,0,400,100);
frame.add(button2);
button3.setBounds(100,100,400,100);
frame.add(button3);
button4.setBounds(500,0,100,200);
frame.add(button4);
button5.setBounds(0,200,100,200);
frame.add(button5);
button6.setBounds(100,200,200,100);
frame.add(button6);
button7.setBounds(300,200,200,100);
frame.add(button7);
button8.setBounds(100,300,200,100);
frame.add(button8);
button9.setBounds(300,300,200,100);
frame.add(button9);
button10.setBounds(500,200,100,200);
frame.add(button10);
frame.setSize(600,400);
frame.setVisible(true);
}
}
/**
* 面板布局法
*/
public class TestHomeWork02 {
public static void main(String[] args) {
Frame frame = new Frame();
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("east-1"),BorderLayout.EAST);
p1.add(new Button("west-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("east-2"),BorderLayout.EAST);
p3.add(new Button("west-2"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
p4.add(new Button("p4-btn-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.setLocation(300,400);
frame.setBackground(Color.black);
frame.setSize(600,400);
frame.setVisible(true);
}
}
2.4 事件监听
事件监听:当某个事情发送的时候,干什么?
package com.tree.lesson02;
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 TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button);
frame.pack();
frame.setVisible(true);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
2.5 输入框TextField
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();
textField.addActionListener(myActionListener2);
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();
System.out.println(field.getText());
field.setText("");
}
}
2.6 易计算器,组合+内部类回顾复习!
oop原则:组合,大于继承
class A extends B{
}
class A{
public B b
}
目前代码
public class TestCalc {
public static void main(String[] args) {
new Calcultor();
}
}
class Calcultor extends Frame{
public Calcultor(){
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalcultorListener(num1,num2,num3));
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setSize(600,100);
setVisible(true);
}
}
class MyCalcultorListener implements ActionListener{
private TextField num1,num2,num3;
public MyCalcultorListener(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象写法
public class TestCalc {
public static void main(String[] args) {
new Calcultor().loadFrame();
}
}
class Calcultor extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalcultorListener(this));
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setSize(600,100);
setVisible(true);
}
}
class MyCalcultorListener implements ActionListener{
Calcultor calcultor = null;
public MyCalcultorListener(Calcultor calcultor){
this.calcultor = calcultor;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(calcultor.num1.getText());
int n2 = Integer.parseInt(calcultor.num2.getText());
calcultor.num3.setText(""+(n1+n2));
calcultor.num1.setText("");
calcultor.num2.setText("");
}
}
内部类
public class TestCalc {
public static void main(String[] args) {
new Calcultor().loadFrame();
}
}
class Calcultor extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalcultorListener());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setSize(600,100);
setVisible(true);
}
private class MyCalcultorListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
2.7 画笔
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadPaint();
}
}
class MyPaint extends Frame{
public void loadPaint(){
setBounds(200,200,600,500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawOval(100,100,100,100);
g.setColor(Color.green);
g.fillRect(150,200,200,200);
}
}
2.8 鼠标监听
目的:想要实现鼠标画画!
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, 400);
points=new ArrayList<>();
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
public void paint(Graphics g) {
Iterator iterator=points.iterator();
while (iterator.hasNext()) {
Point point=(Point)iterator.next();
g.setColor(Color.cyan);
g.fillOval(point.x, point.y, 10, 10);
}
}
public void addPaint(Point point) {
points.add(point);
}
private class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
MyFrame myFrame=(MyFrame)e.getSource();
myFrame.addPaint(new Point(e.getX(),e.getY()));
myFrame.repaint();
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-63HKpiWv-1633706763781)(GUI编程.assets/image-20210912160651707.png)]
2.9 窗口监听
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame() {
setBackground(Color.red);
setBounds(100,100,200,200);
setVisible(true);
addWindowListener(new MyWindowListener());
}
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);
System.out.println("窗口关闭");
}
}
}
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame() {
setBackground(Color.red);
setBounds(100,100,200,200);
setVisible(true);
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭");
}
}
);
}
}
2.10 键盘监听
public class TestKeyboard {
public static void main(String[] args) {
new KeyboardFrame();
}
}
class KeyboardFrame extends Frame{
public KeyboardFrame(){
setBackground(Color.red);
setBounds(100,100,200,200);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode==KeyEvent.VK_7);
System.out.println("按了‘7’");
}
});
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小,定位,背景颜色,可见性,监听!
3、Swing
3.1 窗口,画板
public class JFrameDemo {
public void init(){
JFrame jf = new JFrame("这是一个JFrame 窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.cyan);
JLabel label = new JLabel("阿巴阿巴阿巴",SwingConstants.CENTER);
jf.add(label);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
标签居中
public class JframeDemo02 {
public static void main(String[] args) {
new MyJframe2().inti();
}
}
class MyJframe2 extends JFrame{
public void inti(){
this.setVisible(true);
this.setBounds(10,10,200,200);
JLabel label = new JLabel("阿巴阿巴阿巴",SwingConstants.CENTER);
this.add(label);
Container container = this.getContentPane();
container.setBackground(Color.BLUE);
}
}
3.2 弹窗
JDialog,用来被弹出,默认就有关闭事件
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setBounds(100,100,200,200);
this.setBackground(Color.gray);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
JButton jButton = new JButton("弹出窗口");
jButton.setBounds(30,30,100,100);
contentPane.add(jButton);
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new MyDialogFrame();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialogFrame extends JDialog{
public MyDialogFrame() {
this.setBounds(100,100,300,300);
this.setVisible(true);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("欢迎cvbnm,");
jLabel.setBounds(50,50,100,100);
contentPane.add(jLabel);
}
}
3.3 标签
图标 ICON
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(){
this.setBounds(100,100,700,500);
IconDemo iconDemo = new IconDemo(15, 15);
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
public void paintIcon(Component c, Graphics g, int x, int y){
g.fillOval(x,y,width,height);
}
public int getIconWidth(){
return this.width;
}
public int getIconHeight(){
return this.height;
}
}
图片ICON
public class ImageIconDemo extends JFrame{
public ImageIconDemo(){
JLabel jLabel = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("security.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,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.4 面板
JPanel
public class JPanelDemo extends JFrame{
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));
JPanel p1 = new JPanel(new GridLayout(1, 3));
p1.add(new JButton("1"));
p1.add(new JButton("1"));
p1.add(new JButton("1"));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
public class JPanelScroll extends JFrame {
public JPanelScroll() {
Container container = this.getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("这就是王者");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new JPanelScroll();
}
}
3.5 按钮
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
URL url = ImageIconDemo.class.getResource("timg.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton button = new JButton();
button.setIcon(imageIcon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,500,300);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container container = this.getContentPane();
URL url = ImageIconDemo.class.getResource("timg.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
public class JButtonDemo extends JFrame {
public JButtonDemo() {
Container container = this.getContenPance();
JCheckBox c1 = new JCheckBox("01");
JCheckBox c2 = new JCheckBox("02");
JCheckBox c3 = new JCheckBox("03");
container.add(c1, BorderLayout.CENTER);
container.add(c2, BorderLayout.NORTH);
container.add(c3, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDeafaultCloseOpeartion(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
3.6 列表
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01(){
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02(){
Container container = this.getContentPane();
Vector<Object> contents = new Vector();
JList jList = new JList(contents);
contents.add("张三");
contents.add("list");
contents.add("wangwu");
container.add(jList);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息;一般是动态扩容
3.7 文本框
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
container.add(textField,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
public class TestTextDemo02 extends JFrame {
public TestTextDemo02(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
public class TestTextDemo03 extends JFrame {
public TestTextDemo03(){
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(20,50);
textArea.setText("阿巴阿巴阿巴");
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(400,350);
}
public static void main(String[] args) {
new TestTextDemo03();
}
}
4、贪吃蛇
帧,如果时间片足够小,就是动画,一秒30帧 60帧。连起来是动画,拆开就是静态的图片!
监听监听
定时器 Timer
-
定义数据 -
画上去 -
监听事件 键盘 事件
package snake;
import javax.swing.*;
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,720);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
|