GUI编程
前言:告诉大家应该怎么学?
- 这是什么?
- 它怎么玩?
- 该如何在我们平时运用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
一、是什么
- GUI是图形界面编程
- GUI的核心技术:Swing AWT
- GUI缺点:界面不美观;需要jar环境
二、为什么
为什么我们要学习
- 可以写出自己心中想要的一些小工具
- 工作的时候,也可能需要维护到swing界面,(概率极小!)
- 了解MVC架构,了解监听!
三、怎么做
1、AWT
1.1 AWT介绍
- 包含了很多类和接口!
- 元素:窗口、按钮、文本框
1.2 组件和容器
1.2.1. Frame(容器)
package com.gui;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame =new Frame("我的第一个JAVA图像界面窗");
frame.setVisible(true);
frame.setSize(200,200);
frame.setBackground(Color.BLUE);
frame.setLocation(200,200);
frame.setResizable(false);
}
}
展示多个窗口
package com.gui;
import java.awt.*;
public class TestFrame02 {
//展示多个窗口
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.BLUE);
MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.GREEN);
MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.MAGENTA);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.YELLOW);
}
}
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);
}
}
1.2.2. Panel(面板)
package com.gui;
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(Color.GREEN);
//相对于frame的面板坐标
panel.setBounds(50,50,400,400);
panel.setBackground(Color.BLUE);
//将面板添加到窗口
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//结束程序
}
});
}
}
1.2.3. 布局管理器
流式布局
package com.gui; import com.sun.media.jfxmedia.events.NewFrameEvent; import java.awt.*; public class TestLayout { 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"); //设置为流式布局 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setVisible(true); frame.setSize(200,200); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); } }
东西南北中
package com.gui; import java.awt.*; public class TestLayoutBorder { public static void main(String[] args) { Frame frame = new Frame(); 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.setSize(200,200); frame.setVisible(true); } }
表格布局
package com.gui; import java.awt.*; public class TestLayoutGrid { public static void main(String[] args) { Frame frame = new Frame(); 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"); //设置表格布局 3*2 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(); frame.setVisible(true); } }
2、Swing
2.1、窗口,画板
顶级窗口Jframe
//实例化Jframe
JFrame jf = new JFrame("这是JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.BLUE);
JLabel label = new JLabel("大家好");
jf.add(label);
//让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = jf.getContentPane();
container.setBackground(Color.YELLOW);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5RgXboVG-1617029018116)(D:\0.0 study\红芯书院\5、GUI编程\yellow.png)]
2.2、弹窗
JDialog,用来被弹出。默认就有关闭事件!
//主窗口
public class DialogDemo {
public DialogDemo() {
//实例化Jframe
javax.swing.JFrame jf = new javax.swing.JFrame("这是JFrame窗口");
jf.setVisible(true);
jf.setSize(700, 500);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container container = jf.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) {
DialogDemo dialogDemo = new DialogDemo();
}
}
//弹窗的窗口
public 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("欢迎弹出"));
}
}
2.3、标签 label
new JLabel("xxx",放置谁,,SwingConstante.CENTER);
图标(ICON)放在标签上,也可以放在按钮上。
2.4、按钮
2.4.1 单选框
public class JButtonDemo extends JFrame {
public JButtonDemo() {
Container container = this.getContenPance();
/*
// 放图片
* Icon icon = new ImageIcon(resource);
* */
//单选框
JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
JRadioButton radioButton3 = new JRadioButton("JRadioButton3");
//由于单选框只能选择一个,用分组,一个组中只能选择一个
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.setSize(500, 300);
this.setDeafaultCloseOpeartion(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
2.4.2 多选框
public class JButtonDemo extends JFrame {
public JButtonDemo() {
Container container = this.getContenPance();
/*
// 放图片
* Icon icon = new ImageIcon(resource);
* */
//多选框
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();
}
}
2.5、下拉框
JComboBox status = new JComboBox();
status.addItem("01");
status.addItem("02");
status.addItem("03");
container.add(status);
2.6、列表框
String[] contents={"1","2".,"3"};
JList jList = new JList(contents);
contents.add(jList);
2.7、文本框
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("word",20);
Container.add(textField1,BorderLayout.NORTH);
Container.add(textField2,BorderLayout.SOUTH);
2.8、密码框
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);