GUI编程
告诉大家怎么学?
- 这是什么?
- 他怎么玩?
- 该如何去在我们平时运用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1、简介
GUI的核心技术:Swing AWT
- 界面不美观
- 需要jre环境!
为甚恶魔我们要学习呢?
- 可以写出自己心中想要的一些工具
- 工作的时候,也可能需要维护到swing界面,概率小!
- 了解MVC架构,了解监听!
2、 AWT(抽象的窗口工具)
2.1、AWT介绍
- 包含了很多类和接口,GUI:图形用户界面编程
- 元素:窗口,按钮,文本框
- java.awt
Eeclipse就是用java写的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-21WvWBHk-1651110242988)(C:\Users\57322\AppData\Roaming\Typora\typora-user-images\image-20220324092214707.png)]
2.2、组键和容器
1、Frame
package com.kang.lesson01;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame 对象
Frame frame = new Frame("我的第一个Java图形界面窗口!");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(201, 213, 130));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5KrvYPtE-1651110242989)(C:\Users\57322\AppData\Roaming\Typora\typora-user-images\image-20220324094035183.png)]
package com.kang.lesson01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
new MyFrame(100,100,200,200,Color.BLUE);
new MyFrame(300,100,200,200,Color.red);
new MyFrame(100,300,200,200,Color.green);
new MyFrame(300,300,200,200,Color.black);
}
}
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-L6jiWYrM-1651110242991)(C:\Users\57322\AppData\Roaming\Typora\typora-user-images\image-20220324095400362.png)]
2、面板Panel
package com.kang.lesson01;
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,500,500,500);
frame.setBackground(new Color(107, 241, 110));
//panel设置坐标 相对于frame 在frame内部
panel.setBounds(200,200,200,200);
panel.setBackground(new Color(234, 15, 15));
//把panel放进去
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D2mzMDmk-1651110242991)(C:\Users\57322\AppData\Roaming\Typora\typora-user-images\image-20220401153256609.png)]
2.3、布局管理器
(1)BorderLayout东西南北中
package com.kang.lesson01;
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("我的窗口");
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.setBounds(500,500,500,500);
frame.setBackground(new Color(59, 66, 156));
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(2);
}
});
}
}
- 东西南北中
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XiBjWtwE-1651110242992)(C:\Users\57322\AppData\Roaming\Typora\typora-user-images\image-20220401153631339.png)]
(2)GridLayout表格的形式
package com.kang.lesson01;
import java.awt.*;
public class TestGridLayout {
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");
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);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oPdRkB6O-1651110242993)(C:\Users\57322\AppData\Roaming\Typora\typora-user-images\image-20220401153842857.png)]
(3)练习
- 把下面的打印出来
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-puGBgl7x-1651110242994)(C:\Users\57322\AppData\Roaming\Typora\typora-user-images\image-20220401154000107.png)]
代码
package com.kang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test01 {
public static void main(String[] args) {
//框框
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setSize(500,400);
frame.setVisible(true);
//把面板都new出来
//两个是BorderLayout 两个是GridLayout
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2,2));
//进行嵌套就ok
panel1.add(new Button("East"),BorderLayout.EAST);
panel1.add(new Button("West"),BorderLayout.WEST);
panel2.add(new Button("shang"));
panel2.add(new Button("xia"));
panel3.add(new Button("East1"),BorderLayout.EAST);
panel3.add(new Button("West1"),BorderLayout.WEST);
panel4.add(new Button("butn1"));
panel4.add(new Button("butn2"));
panel4.add(new Button("butn3"));
panel4.add(new Button("butn4"));
panel1.add(panel2,BorderLayout.CENTER);
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
//关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}
2.4、事件监听
package com.kang.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,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("丁耀胜大傻逼");
}
}
一个事件被两个按钮监听
package com.kang.lesson02;
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("button-Stop");
MyMoniter myMoniter = new MyMoniter();
button1.addActionListener(myMoniter);
button2.addActionListener(myMoniter);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.setVisible(true);
}
}
class MyMoniter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//我们可以 通过e得到一些信息
System.out.println("这个按钮被点了=》"+e.getActionCommand());
}
}
2.5、输入框TextField监听
2.6简易计算器,组合+内部类回顾复习
面向对象版编程
package com.kang.lesson02;
import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame{
//创建一个文本框
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
//一个按钮
Button button = new Button("=");
//一个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
MyActionListener3 myActionListener3 = new MyActionListener3(this);
button.addActionListener(myActionListener3);
pack();
setVisible(true);
}
}
class MyActionListener3 implements ActionListener{
Calculator calculator=null;
public MyActionListener3(Calculator calculator){
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取到三个值
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//三个值相加
calculator.num3.setText(""+(n1+n2));
//把空白清空
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类
package com.kang.lesson02;
import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame{
//创建一个文本框
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
//一个按钮
Button button = new Button("=");
//一个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
button.addActionListener(new MyActionListener3());
pack();
setVisible(true);
}
private class MyActionListener3 implements ActionListener{
@Override
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、画笔
package com.kang.lesson02;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new Mypaint().loadFrame();
}
}
class Mypaint extends Frame {
public void loadFrame(){
setBounds(200,200,400,500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.drawOval(100,100,100,100);
g.setColor(Color.red);
g.fillOval(100,200,100,100);
}
}
2.8、键盘监听事件
package com.kang.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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("你按下了上键");
}
}
});
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}
3、Swing
3.1、窗口、面板
package com.kang.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init(); 初始化
public static void init(){
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setBounds(100,100,200,200);
//设置文字Jlabel
JLabel jLabel = new JLabel("李文康爱❤段凯钰");
//让文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//设置一个容器
Container container = jf.getContentPane();
container.setBackground(Color.cyan);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.add(jLabel);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
3.2、弹窗
package com.kang.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 需要一个容器来放东西
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 MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialog extends JDialog{
public MyDialog() {
JDialog jDialog = new JDialog();
jDialog.setVisible(true);
jDialog.setBounds(100,100,500,500);
Container container = new Container();
container.add(new Label("康康想拉屎"));
container.setLayout(null);
}
}
3.3、标签
package com.kang.lesson04;
import javax.swing.*;
import java.awt.*;
public class iconDemo extends JFrame implements Icon {
private int width;
private int height;
public iconDemo(){}
public iconDemo(int width,int height){
this.height=height;
this.width=width;
}
public void init(){
iconDemo iconDemo = new iconDemo(15,15);
JLabel label = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
//new一个容器 可以存放
Container contentPane = getContentPane();
contentPane.add(label);
this.setVisible(true);
this.setBounds(100,100,500,500);
//设置可以关闭
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new iconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
3.4、面板
package com.kang.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = getContentPane();
//容器的布局
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(2,1));
JPanel panel3 = new JPanel(new GridLayout(3,1));
JPanel panel4 = new JPanel(new GridLayout(1,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,200,300,400);
setVisible(true);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
package com.kang.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//设置文本域
JTextArea textArea = new JTextArea();
textArea.setText("李文康❤段凯钰!");
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
setVisible(true);
setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按钮
图片按钮
package com.kang.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("dsg.jpg");
Icon icon = new ImageIcon(url);
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
container.add(jButton);
setVisible(true);
setBounds(100,100,500,500);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选框
package com.kang.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container container = getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("dsg.jpg");
Icon icon = new ImageIcon(url);
//单选框
JRadioButton radioButton1 = new JRadioButton("JRadioRutton01");
JRadioButton radioButton2 = new JRadioButton("JRadioRutton02");
JRadioButton radioButton3 = new JRadioButton("JRadioRutton03");
//由于单选框中只能选一个,分组中一个组中只能选一个
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);
setVisible(true);
setBounds(100,100,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
复选框
package com.kang.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container container = getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("dsg.jpg");
Icon icon = new ImageIcon(url);
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
container.add(checkBox01,BorderLayout.CENTER);
container.add(checkBox02,BorderLayout.NORTH);
setVisible(true);
setBounds(100,100,500,500);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
- 下拉框
package com.kang.lesson06;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = getContentPane();
JComboBox jcomboBox = new JComboBox();
jcomboBox.addItem(null);
jcomboBox.addItem("喜羊羊与灰太狼");
jcomboBox.addItem("熊出没");
jcomboBox.addItem("开心超人");
container.add(jcomboBox);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
- 列表框
package com.kang.lesson06;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = getContentPane();
//静态存放
//String[] arr={"张三","李四","王五"};
//动态存放
Vector objects = new Vector();
//列表中需要存放内容
JList jList = new JList(objects);
objects.add("张三");
objects.add("李四");
objects.add("王五");
container.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
3.7、文本框
文本框
package com.kang.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField1 = new JTextField("world",20);
container.add(textField,BorderLayout.NORTH);
container.add(textField1,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
密码框
package com.kang.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
文本域
package com.kang.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//设置文本域
JTextArea textArea = new JTextArea();
textArea.setText("李文康❤段凯钰!");
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
setVisible(true);
setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}