Java---GUI编程详解

GUI编程

组件:

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具

1、简介

1.1.gui的核心技术:Swing AWT

1.2.不流行:

  • 界面不美观
  • 需要jre环境

2、AWT

2.1、Awt介绍

1.包含了很多类和接口。GUI:图形用户界面编程

2.元素:窗口,按钮,文本框

2.2、 组件和容器

1.Frame

/关于Frame的一些方法

package gui_study;

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(85,150,68));
        //设置弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);


    }

}

问题:弹窗关闭不了,只能结束程序

/打开多个窗口

package gui_study;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口 new
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.red);
        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);

    }

}

2、面板Panel

解决了主动关闭问题

package gui_study;

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();
        Panel panel1 = new Panel();

        //设置布局
        frame.setLayout(null);

        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(40,161,35));
        //panel设置坐标,相对于Frame
        panel.setBounds(50,50,100,100);
        panel.setBackground(new Color(193,15,60));
        panel1.setBounds(200,50,100,100);
        panel1.setBackground(new Color(193,15,60));
        //frame.add(panel)
        frame.add(panel);
        frame.add(panel1);
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件 System.exit()
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });


    }
}

3、布局管理器

  • 流式布局
package gui_study;

import java.awt.*;

public class TestFlowLayout {
    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");
        //设置为流式布局
        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);

    }
}

  • 东西南北中
package gui_study;

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        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 gui_study;

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("btn1");
        Button btn3 = new Button("btn1");
        Button btn4 = new Button("btn1");
        Button btn5 = new Button("btn1");
        Button btn6 = new Button("btn1");

        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);

    }

}

4、事件监听

事件监听:当某个事情发生的时候,干什么?

package gui_study;

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
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        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 {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");

    }
}
  • 多个按钮可以共享监听

5、输入框TextFile

package gui_study;

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();
        add(textField);

        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter,就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);
        //设置替换编码
        textField.setEchoChar('*');//在显示端用*代替


        setVisible(true);
        pack();
    }


}
class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
       TextField field = (TextField) e.getSource();//获得一些资源,返回的一个对象
        System.out.println(field.getText());//获得输入框的文本
        field.setText("");//null
    }

}

6、简易计算器,组合+内部类

package gui_study;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();

    }
}
//计算器类
class Calculator extends Frame{
    public Calculator(){
        //3个文本框
        TextField field1 = new TextField(10);
        TextField field2 = new TextField(20);
        TextField field3 = new TextField(30);

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(field1,field2,field3));
        //一个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
        setVisible(true);




    }
}

//监听器类
class MyCalculatorListener implements ActionListener{
    //获取三个变量
    private TextField field1,field2,field3;
    public MyCalculatorListener(TextField field1,TextField field2,TextField field3){
        this.field1=field1;
        this.field2=field2;
        this.field3=field3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1、获得加数和被加数
       int n1 =  Integer.parseInt(  field1.getText());
       int n2 =  Integer.parseInt(  field2.getText());


        //2、将这个值+运算后,放到第三个框
        field3.setText(""+(n1+n2));


        //3、清除前两个框
        field1.setText("");
        field2.setText("");



    }
}

7、画笔

package gui_study;

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,600,500);
        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g) {
        //画笔,需要有颜色,可以画画
        g.setColor(Color.BLUE);
        g.drawOval(100,100,100,100);
        g.setColor(Color.green);
        //画笔用完,将它还原到最初的颜色
        

    }
}

8、鼠标监听、

目标:想要实现鼠标画画

package gui_study;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

//鼠标监听事件
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,300);
        //存鼠标的点
       points= new ArrayList<>();

        setVisible(true);
        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());


    }

    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while(iterator.hasNext()){
           Point point = (Point) iterator.next();
           g.setColor(Color.blue);
           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 myFrame =(MyFrame) e.getSource();
            //点击的时候,在界面产生一个点
            //这个点就是鼠标的点
            myFrame.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都要重新画一遍
            myFrame.repaint();//刷新


        }
    }
}

9、窗口监听

package gui_study;

import java.awt.*;
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());

    }
    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(true);//隐藏窗口
            System.exit(0);//正常退出
        }
    }
}

10、键盘监听

package gui_study;

import javafx.scene.input.KeyCode;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//键
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();//不需要记录这个数值,直接使用静态属性,
                if (keyCode == KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }

            }
        });

    }

}

3、Swing

1、窗口、面板

    • //init():初始化
    • swing是顶级窗口,需要容器实例化
package gui_study.Swing;
import javax.swing.*;
import java.awt.*;

public class JFrameDemo {
    //init():初始化
    public void init(){
        //顶级窗口
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setVisible(true);
        frame.setBounds(100,100,200,200);
        frame.setBackground(Color.BLUE);
        //设置文字 JLabel
        JLabel jLabel = new JLabel("欢迎学习java");
        frame.add(jLabel);
        //让文本居中
        jLabel.setHorizontalAlignment(JTextField.CENTER);
        //需要容器实例化
        Container contentPane = frame.getContentPane();
        contentPane.setBackground(Color.BLUE);


        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

     public static void main(String[] args) {
        //建立一个窗口
         new JFrameDemo().init();

    }
}

2、JDialog弹窗

  • JDialog,用来被弹出,默认就有关闭事件!
package gui_study.Swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

//主窗口
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 AbstractAction() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });
        container.add(button);
    }


    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("欢迎学习java"));


    }


}

3、标签

label

new JLabel("xxx")
  

图标Icon

package gui_study.Swing;

import javax.swing.*;
import java.awt.*;

//图标,需要实现类,Frame继承
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(){
        IconDemo iconDemo = new IconDemo(15,15);
        //图标放在标签,也可以放在按钮上
        JLabel jLabel = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
        Container container =getContentPane();
        container.add(jLabel);
        this.setVisible(true);
        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;
    }
}

4、面板

JPanel

package gui_study.Swing;

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public JPanelDemo(){
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        container.add(jPanel);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);



    }

    public static void main(String[] args) {
        new JPanelDemo();

    }
}

JScrollPanel滚动面板

package gui_study.Swing;

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎学习Java");
        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.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();
    }

}

5、按钮

普通按钮

package gui_study.Swing;

import javax.swing.*;
import java.awt.*;

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container container = this.getContentPane();
        JButton button = new JButton();
        button.setToolTipText("图片变成按钮");
        container.add(button);
        this.setVisible(true);
        this.setBounds(300,300,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        new JButtonDemo01();

    }
}

  • 单选按钮
package gui_study.Swing;

import javax.swing.*;
import java.awt.*;

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        Container container = 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);
        container.add(jRadioButton01,BorderLayout.CENTER);
        container.add(jRadioButton02,BorderLayout.NORTH);
        container.add(jRadioButton03,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(300,300,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        new JButtonDemo02();

    }
}

  • 复选按钮
 //多选框
        JCheckBox box01 = new JCheckBox("box01");
        JCheckBox box02 = new JCheckBox("box02");
        JCheckBox box03 = new JCheckBox("box03");





        container.add(box01,BorderLayout.CENTER);
        container.add(box02,BorderLayout.NORTH);
        container.add(box03,BorderLayout.SOUTH);

6、列表

  • 下拉框
package gui_study.Swing;

import javax.swing.*;
import java.awt.*;

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01(){
        Container container = this.getContentPane();


        JComboBox box = new JComboBox();
        box.addItem(null);
        box.addItem("正在上映");
        box.addItem("已下架");
        box.addItem("即将上映");
        container.add(box);




        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}

  • 列表框
String contents ={"1","2","3"};
JList jlist = new JList(contents);
container.add(jlist)
  • 应用场景
    • 选择地区,或者一些单个选项
    • 列表,展示信息,一般是动态扩容

7、文本框

  • 文本框
package gui_study.Swing;

import javax.swing.*;
import java.awt.*;

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.SOUTH);
        container.add(textField2,BorderLayout.NORTH);



        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

  • 密码框
new JPasswordField passwordField = new JPasswordField();

passwordField.setEchoChar('*');

container.add(passwordField);

你可能感兴趣的:(javase,java,开发语言)