GUI编程
告诉大家怎么学
- 这是什么?
- 它怎么玩?
- 该如何去运用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1.简介
Gui核心技术:Swing AWT
-
因为界面不美观。
-
需要jre环境!
为什么要学习?
- 可以写出自己心中想要的一些小工具。
- 为了以后MVC架构做准备,了解监听。
- 工作时候,也可能需要维护Swing界面,概率及小。
2.AWT
2.1、AWT介绍
-
包含了很多的类和接口!GUI:图形用户界面。
-
元素:窗口,按钮,文本框。
-
java.awt包
2.2、组件和容器
1. Frame窗口
package com.jyw;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame JDK 看源码 ctrl+alt+v 自动补全new的类
Frame frame = new Frame("我的第一个java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//需要设置窗口大小
frame.setSize(400, 400);
//设置背景颜色
frame.setBackground(Color.BLACK);
frame.setBackground(new Color(151, 26, 148));
//弹出的初始位置
frame.setLocation(100, 100);
//设置大小固定
frame.setResizable(false);
}
}
尝试封装
import java.awt.*;
//GUI的第一个界面
public class TestFrame2 {
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.red);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.yellow);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.magenta);
}
}
class MyFrame extends Frame {
static int id = 0;//可能存在多个窗口,需要一个计数器
public MyFrame(int x, int y, int w, int h, Color color) throws HeadlessException {
super("MyFrame" + (++id));
setBackground(color);
setBounds(x, y, w, h);
this.setVisible(true);
}
}
2 面板Panel
解决关闭事件
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//可以看成是一个空间,但是不能单独存在
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.blue);
//panel 设置坐标,相对于Frame坐标
panel.setBounds(50,50,400,400);
panel.setBackground(Color.red);
//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);
}
});
}
2.3 、布局管理器
-
流式布局
import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组件 alt+回车补全 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 //frame.setLayout(new FlowLayout()); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setSize(200,200); //添加按钮 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
-
东西南北中
import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { Frame frame=new Frame("TestBorderLayout"); Button east=new Button("East"); Button wast=new Button("Wast"); Button south=new Button("South"); Button north=new Button("North"); Button center=new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(wast,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); } }
-
表格布局 Grid
import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame=new Frame("TestGridLayout"); 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();//java 函数,会自动布局 frame.setVisible(true); } }
章节练习
import java.awt.*;
public class Test {
public static void main(String[] args) {
Frame faFrame = new Frame();
faFrame.setVisible(true);
faFrame.setSize(500, 500);
faFrame.setLocation(100, 100);
faFrame.setLayout(new GridLayout(2, 1));
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));
//上面
panel1.add(new Button("East-1"), BorderLayout.EAST);
panel1.add(new Button("West-1"), BorderLayout.WEST);
panel1.add(panel2, BorderLayout.CENTER);
panel2.add(new Button("p2-btn-1"));
panel2.add(new Button("p2-btn-2"));
//下面
panel3.add(new Button("East-2"), BorderLayout.EAST);
panel3.add(new Button("West-2"), BorderLayout.WEST);
panel3.add(panel4, BorderLayout.CENTER);
panel4.add(new Button("p4-btn-1"));
panel4.add(new Button("p4-btn-2"));
panel4.add(new Button("p4-btn-3"));
panel4.add(new Button("p4-btn-4"));
faFrame.add(panel1);
faFrame.add(panel3);
}
}
总结:
-
Frame是一个顶级窗口。
-
Panel无法单独显示,必须添加到某个容器中。
-
布局管理器
1.流式。
2.东西 南北中。
3.表格
-
大小,定位,背景颜色,可见性,监听。
2.4、事件监听
事件监听:当某个事情发生的时候,干什么?
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();
//因为 addActionListener()需要一个 ActionListener,所以我们需要构造一个ActionListener
//所以我们需要构造一个ActionListenter
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("aa");
}
}
多个按钮,共享一个事件
public class TestActionEvent02 {
public static void main(String[] args) {
//两个按钮,实现同一监听
Frame frame=new Frame("开始-终止");
Button button1=new Button("start");
Button button2=new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
//可以多个按钮按钮,只写一个监听类
button2.setActionCommand("button2-stop");
MyMonitor myMonitor=new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand() 获取按钮的信息
System.out.println("按钮点击=》"+e.getActionCommand());
}
}
2.5、输入框TextField监听
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();
this.add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//回车会触发输入框的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
this.setVisible(true);
this.pack();
}
}
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField=(TextField)e.getSource();//获得一些资源,返回一个对象
System.out.println(textField.getText());//获得输入框中的文本
textField.setText("");//清空
}
}
2.6、简易计算器,组合+内部类回顾
oop原则:组合,大于继承!
组合写法:
package com.jyw.lesson02;
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 {
//属性
//3个文本框
TextField textField1 = null;
TextField textField2 = null;
TextField textField3 = null;
Button button=null;
Label label=null;
//方法
public void loadFrame() {
textField1 = new TextField(10);//10个字符
textField2 = new TextField(10);//10个字符
textField3 = new TextField(20);//20个字符
//1个按钮
button = new Button("=");
//1个标签
label = new Label("+");
//监听
button.addActionListener(new MyCalculatorListener(this));
//布局
this.setLayout(new FlowLayout());
this.add(textField1);
this.add(label);
this.add(textField2);
this.add(button);
this.add(textField3);
this.pack();
this.setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener {
//构造器获取计算器对象
private Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n11 = Integer.parseInt(calculator.textField1.getText());
int n22 = Integer.parseInt(calculator.textField2.getText());
//2.加法运算后,放到第三个框
calculator.textField3.setText(String.valueOf(n11 + n22));
//3.清除前两个框
calculator.textField1.setText("");
calculator.textField2.setText("");
}
}
内部类:
-
更好得包装
-
内部类最大得好处,是可以直接使用访问我外部类
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 { //属性 //3个文本框 TextField textField1 = null; TextField textField2 = null; TextField textField3 = null; Button button=null; Label label=null; //方法 public void loadFrame() { textField1 = new TextField(10);//10个字符 textField2 = new TextField(10);//10个字符 textField3 = new TextField(20);//20个字符 //1个按钮 button = new Button("="); //1个标签 label = new Label("+"); //监听 button.addActionListener(new MyCalculatorListener()); //布局 this.setLayout(new FlowLayout()); this.add(textField1); this.add(label); this.add(textField2); this.add(button); this.add(textField3); this.pack(); this.setVisible(true); } //监听器类 //内部类最大得好处,是可以直接使用访问我外部类 private class MyCalculatorListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //1.获得加数和被加数 int n11 = Integer.parseInt(textField1.getText()); int n22 = Integer.parseInt(textField2.getText()); //2.加法运算后,放到第三个框 textField3.setText(String.valueOf(n11 + n22)); //3.清除前两个框 textField1.setText(""); textField2.setText(""); } } }
2.7、画笔
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame() {
setBounds(200, 200, 600, 500);
this.setVisible(true);
}
@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(150,200,200,200);
//养成习惯,画笔用完,将他还原最初的颜色
}
}
2.8、鼠标监听
目的:想要实现鼠标画画!
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame {
private ArrayList points;
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
public MyFrame(String title) {
super(title);
this.setBounds(200, 200, 400, 400);
this.points = new ArrayList<>();
this.setVisible(true);
//鼠标监听器,针对于窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//画画,需要监听鼠标事件
Iterator iterator = points.iterator();
while (iterator.hasNext()) {
Point ponint = (Point) iterator.next();
g.setColor(Color.green);
g.fillOval(ponint.x, ponint.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();
//这个我们点击的时候,就会在界面上产生一个点
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(), e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新界面
}
}
}
2.9、窗口监听
ublic class TestWindow {
public static void main(String[] args) {
new MyWindowFrame();
}
}
class MyWindowFrame extends Frame {
public MyWindowFrame() {
setVisible(true);
setBackground(Color.blue);
setBounds(100, 100, 400, 400);
//addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter() {
//匿名内部类
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你点击了关闭");
System.exit(0);//正常退出
}
});
}
}
2.8、键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new MyKeyListener();
}
}
class MyKeyListener extends Frame {
public MyKeyListener() {
setBounds(100, 100, 400, 400);
setBackground(Color.blue);
setVisible(true);
addKeyListener(new KeyAdapter() {
//键盘按下的键是哪一个
@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();//不需要记录数值,直接使用静态属性 VK_XXX
if (keycode == KeyEvent.VK_UP)
System.out.println(keycode);//获取键盘码
}
});
//根据按下不同事件,产生不同结果
}
}
3.Swing
3.1、窗口,面板
public class JFrameDemo {
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
//init 初始化
public void init() {
JFrame frame = new JFrame("这是一个JFrame");
frame.setVisible(true);
frame.setBounds(100, 100, 400, 400);
frame.setBackground(Color.BLUE);
//设置文字
JLabel jLabel = new JLabel();
jLabel.setText("jywjyw");
//让文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(jLabel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.2、弹窗
JDialog,用来被弹出,默认就有关闭事件。
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 MyDialoDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialoDemo extends JDialog {
public MyDialoDemo() {
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("jyw学java");
jLabel.setBounds(100,100,100,100);
contentPane.add(jLabel);
}
}
3.3、标签
new JLabel("xxx");
图标ICON
public class MyImageIcon extends JFrame {
public MyImageIcon() throws HeadlessException {
//获取图片的地址
URL url = MyImageIcon.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel("ImageIcon");
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
this.setVisible(true);
this.setBounds(100,100,800,800);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyImageIcon();
}
}
3.4、面板
public class JPanelDemo extends JFrame {
public JPanelDemo() throws HeadlessException {
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2, 1, 10, 10));//后面参数的意思是间距
JPanel jPanel = new JPanel(new GridLayout(1, 3));
jPanel.add(new Button("1"));
jPanel.add(new Button("2"));
jPanel.add(new Button("3"));
contentPane.add(jPanel);
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
public class JScrollPanelDemo extends JFrame {
public JScrollPanelDemo() throws HeadlessException {
Container container=this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 20);
jTextArea.setText("学习");
//Scroll面板
JScrollPane scrollPane=new JScrollPane(jTextArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanelDemo();
}
}
3.5、按钮
图片按钮
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() throws HeadlessException {
Container contentPane = this.getContentPane();
//将图片变成图标
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton button = new JButton(icon);
button.setToolTipText("这是一个图片按钮");
contentPane.add(button);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() throws HeadlessException {
Container contentPane = this.getContentPane();
//单选框
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);
contentPane.add(jRadioButton01,BorderLayout.CENTER);
contentPane.add(jRadioButton02,BorderLayout.NORTH);
contentPane.add(jRadioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
复选按钮
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() throws HeadlessException {
Container contentPane = this.getContentPane();
//复选框
JCheckBox jCheckBox01 = new JCheckBox("JCheckBox01");
JCheckBox jCheckBox02 = new JCheckBox("JCheckBox02");
JCheckBox jCheckBox03 = new JCheckBox("JCheckBox03");
contentPane.add(jCheckBox01,BorderLayout.NORTH);
contentPane.add(jCheckBox02,BorderLayout.CENTER);
contentPane.add(jCheckBox03,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
- 下拉框
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() throws HeadlessException {
this.setVisible(true);
this.setBounds(100,100,100,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("正在上映");
jComboBox.addItem("已下架");
jComboBox.addItem("即将上映");
contentPane.add(jComboBox);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
- 列表框
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() throws HeadlessException {
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
//生成列表内容
//String[] contents={"1","2","3"};
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("张三");
contents.add("李四");
contents.add("王五");
contentPane.add(jList);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
- 应用场景
- 选择地区,或者一些单个选项。
- 列表,展示信息,一般是动态扩容。
3.7、文本框
- 文本框
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() throws HeadlessException {
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
//文本框
JTextField jTextField1 = new JTextField("Hello");
JTextField jTextField2 = new JTextField("World",20);
contentPane.add(jTextField1,BorderLayout.NORTH);
contentPane.add(jTextField2,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
- 密码框
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() throws HeadlessException {
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
//密码框
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
contentPane.add(jPasswordField);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
- 文本域
public class JScrollPanelDemo extends JFrame {
public JScrollPanelDemo() throws HeadlessException {
Container container=this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 20);
jTextArea.setText("学习");
//Scroll面板
JScrollPane scrollPane=new JScrollPane(jTextArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanelDemo();
}
}