GUI编程笔记

1、简介

GUI的核心技术:Swing AWT

  1. 因为界面不美观
  2. 需要jre环境!

为什么我们要学习

  1. 可以写出自己心中的一些小工具
  2. 工作时候,也可以需要维护到swing界面,概率极低
  3. 了解MVC架构,了解监听!

2、AWT

2.1、Awt介绍
  1. 包含了很多类和接口!GUI
  2. 元素:窗口,按钮,文本框
  3. java.awt
2.2、组件和容器
1.JFrame
import javax.swing.*;
import java.awt.*;

public class Demo01 {
    public static void main(String[] args) {

        JFrame jFrame = new JFrame("我的第一个Java界面");
        //设置大小
        jFrame.setSize(500, 500);
        //设置界面置顶
        jFrame.setAlwaysOnTop(true);
        //设置居中
        jFrame.setLocationRelativeTo(null);
        //设置关闭
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置背景颜色
        jFrame.setBackground(Color.BLUE);
        //固定窗口的大小
        jFrame.setResizable(false);
        //设置显示
        jFrame.setVisible(true);
    }
}
2.面板panel盒子
public class Demo02 {
    public static void main(String[] args) {
        //布局
        Panel panel = new Panel();
        panel.setBounds(20, 20, 100, 100);
        panel.setBackground(Color.blue);
        JFrame jFrame = new JFrame("我的第一个Java界面");
        //设置大小
        jFrame.setSize(500, 500);
        //设置界面置顶
        jFrame.setAlwaysOnTop(true);
        //设置居中
        jFrame.setLocationRelativeTo(null);
        //设置关闭
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置背景颜色
        jFrame.setBackground(Color.BLUE);
        //固定窗口的大小
        jFrame.setResizable(false);
        jFrame.add(panel);
        //设置显示
        jFrame.setLayout(null);
        jFrame.setVisible(true);
    }
}
3.布局管理器 
  • 流式布局 Flow
 jFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
        //jFrame.setLayout(new FlowLayout(FlowLayout.LEFT));
       // jFrame.setLayout(new FlowLayout(FlowLayout.RIGHT));
  • 东南西北中 Border
import javax.swing.*;
import java.awt.*;

public class Demo04 {
    public static void main(String[] args) {
        Button button = new Button("1");
        Button button2 = new Button("1");
        Button button3 = new Button("1");
        Button button4 = new Button("1");
        Button button5 = new Button("1");
        JFrame jFrame = new JFrame();
        jFrame.setSize(500, 500);
        //设置界面置顶
        jFrame.setAlwaysOnTop(true);
        //设置居中
        jFrame.setLocationRelativeTo(null);
        jFrame.add(button,BorderLayout.EAST);//东
        jFrame.add(button2,BorderLayout.WEST);//南
        jFrame.add(button3,BorderLayout.SOUTH);//西
        jFrame.add(button4,BorderLayout.NORTH);//北
        jFrame.add(button5,BorderLayout.CENTER);//中
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}
  • 表格布局Grid
import javax.swing.*;
import java.awt.*;

public class Demo05 {
    public static void main(String[] args) {
        Button button = new Button("1");
        Button button2 = new Button("1");
        Button button3 = new Button("1");
        Button button4 = new Button("1");
        Button button5 = new Button("1");
        JFrame jFrame = new JFrame();
        jFrame.setSize(500, 500);
        //设置界面置顶
        jFrame.setAlwaysOnTop(true);
        //设置居中
        jFrame.setLocationRelativeTo(null);
        //设置表格排序3行2列
        jFrame.setLayout(new GridLayout(3,2));
        jFrame.add(button);
        jFrame.add(button2);
        jFrame.add(button3);
        jFrame.add(button4);
        jFrame.add(button5);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}
4.事件监听

动作监听AbstractAction

jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source==jButton){
                    Random random = new Random();
                    jButton.setLocation(random.nextInt(500),random.nextInt(500));
                }
            }
        });

鼠标监听MouseListener

 public class Demo03 extends JFrame implements MouseListener {
private void initActionListener(){

        jButton.setBounds(0,0,100,200);
        this.getContentPane().add(jButton);
        //给按钮添加事件
        jButton.addMouseListener(this);

    }

    /**
     * Invoked when the mouse button has been clicked (pressed
     * and released) on a component.
     *
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        //点击
        System.out.println("点击");
    }

    /**
     * Invoked when a mouse button has been pressed on a component.
     *
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {
        //按下
        System.out.println("按下不松");
    }

    /**
     * Invoked when a mouse button has been released on a component.
     *
     * @param e
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        //按下松开
        System.out.println("松开");
    }

    /**
     * Invoked when the mouse enters a component.
     *
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        //划入
        System.out.println("划入");
    }

    /**
     * Invoked when the mouse exits a component.
     *
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {
        //划出
        System.out.println("划出");
    }
}

键盘监听KeyListener 

public class Demo04 extends JFrame implements KeyListener {
    public Demo04(){
        initJFrame();
        this.setVisible(true);
    }

    private  void initJFrame(){
        this.setSize(500,500);
        this.setTitle("键盘监听");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.addKeyListener(this);
    }

    /**
     * Invoked when a key has been typed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key typed event.
     *
     * @param e
     */
    @Override
    public void keyTyped(KeyEvent e) {

    }

    /**
     * Invoked when a key has been pressed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key pressed event.
     *
     * @param e
     */
    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("按下不松");
    }

    /**
     * Invoked when a key has been released.
     * See the class description for {@link KeyEvent} for a definition of
     * a key released event.
     *
     * @param e
     */
    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("松开");
    }
}

获取键盘上的对应数e.getkeycode

 public void keyReleased(KeyEvent e) {
        System.out.println("松开");
        int keyCode = e.getKeyCode();
        System.out.println((char) keyCode);
    }

3、Swing

1.窗体
import javax.swing.*;

public class Demo06 extends JFrame {

    public Demo06(){
        initJFrame();
        initJFram1();
        this.setVisible(true);
    }
    private  void initJFrame(){
        //设置界面大小
        this.setSize(500,400);
        //设置界面文本
        this.setTitle("监听");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //取消默认居中
        //this.setLayout(null);
        //关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    private  void initJFram1(){
        JLabel jLabel = new JLabel("555555555");
        //jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        this.getContentPane().add(jLabel);
    }
}
2.弹窗
import javax.swing.*;
import java.awt.event.ActionEvent;

public class Demo06 extends JFrame {

    public Demo06(){
        initJFrame();
        initJFram1();
        this.setVisible(true);
    }
    private  void initJFrame(){
        //设置界面大小
        this.setSize(500,400);
        //设置界面文本
        this.setTitle("监听");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //取消默认居中
        this.setLayout(null);
        //关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    private  void initJFram1(){
        JButton jButton = new JButton("点击");
        jButton.setBounds(20, 20, 100, 100);
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (jButton==source){
                    JDialog();
                }
            }
        });
        this.getContentPane().add(jButton);
    }
    private void JDialog(){
        JDialog jDialog = new JDialog();
        JLabel jLabel = new JLabel("你好");
        //位置
        jLabel.setBounds(80, 50, 200, 50);
        jDialog.add(jLabel);
        //高宽
        jDialog.setSize(40, 80);
        //置顶
       jDialog.setAlwaysOnTop(true);
       //居中
       jDialog.setLocationRelativeTo(null);
       //不关闭不执行
       jDialog.setModal(true);
       jDialog.setVisible(true);
    }
}
3.标签

画图标

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

//图标 需要实现类 Frame继承
public class Demo02 extends JFrame implements Icon {
    private int width;
    private int height;

    public Demo02() {
    }
    public Demo02(int width,int height) {
        this.width=width;
        this.height=height;
    }
    private void intiIOcn(){
        //文本 图标 位置
        Demo02 demo02 = new Demo02(100,100);
        JLabel jLabel = new JLabel("demo02", demo02, SwingConstants.CENTER);
        this.getContentPane().add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Demo02().intiIOcn();
    }
    @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;
    }
}

图片

import javax.swing.*;
import java.net.URL;

public class Demo03 extends JFrame {
    public Demo03(){
        //图片地址
        JLabel jLabel = new JLabel("Guts");
        URL resource = Demo03.class.getResource("111.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);
        //图标
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        this.setSize(200,500);
        this.getContentPane().add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Demo03();
    }
}
4.面板

JPaneL盒子

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

public class Demo04  extends JFrame {
    public Demo04() {
        //几行几列 上下间距
        this.getContentPane().setLayout(new GridLayout(2,1,10,10));
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(1,2));
        JPanel jPanel3 = new JPanel(new GridLayout(2,3));
        JPanel jPanel4 = new JPanel(new GridLayout(2,1));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("2"));
        jPanel.add(new JButton("3"));

        jPanel2.add(new JButton("1"));
        jPanel2.add(new JButton("2"));

        jPanel3.add(new JButton("1"));
        jPanel3.add(new JButton("2"));
        jPanel3.add(new JButton("3"));

        jPanel4.add(new JButton("1"));

        this.getContentPane().add(jPanel);
        this.getContentPane().add(jPanel2);
        this.getContentPane().add(jPanel3);
        this.getContentPane().add(jPanel4);
        this.setSize(500,500);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

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

滑动JScorll

import javax.swing.*;

public class Demo05 extends JFrame{
    public Demo05() {
        //文本域
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("请输入");
        //滑动JScroll
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        jScrollPane.setBounds(20,50,100,100);
        this.getContentPane().add(jScrollPane);
        this.setSize(500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //关闭默认居中
        this.setLayout(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new Demo05();
    }
}
5.按钮

图片按钮imageIcon 

import javax.swing.*;
import java.net.URL;

public class Demo01  extends JFrame {
    public Demo01(){
        //路径
        URL resource = Demo01.class.getResource("111.jpg");
        //图片
        ImageIcon imageIcon = new ImageIcon(resource);
        //把图片放在按钮
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("回眸");
        jButton.setBounds(50,50,200,200);
        this.getContentPane().add(jButton);
        this.setSize(500,500);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }

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

单选按钮jRadioButton

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

public class Demo02  extends JFrame  {
    public Demo02(){
        //单选按钮
        JRadioButton jRadioButton = new JRadioButton("1");
        JRadioButton jRadioButton2 = new JRadioButton("2");
        JRadioButton jRadioButton3 = new JRadioButton("3");

        //放在一个组中
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);


        this.getContentPane().add(jRadioButton, BorderLayout.NORTH);
        this.getContentPane().add(jRadioButton2,BorderLayout.CENTER);
        this.getContentPane().add(jRadioButton3,BorderLayout.SOUTH);

        this.setSize(500,500);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //this.setLayout(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo02();
    }
}

多选按钮JcheckBox

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

public class Demo03 extends JFrame{
    public Demo03(){
        JCheckBox jCheckBox = new JCheckBox("1");
        JCheckBox jCheckBox2 = new JCheckBox("2");

        jCheckBox.setBounds(20,20,50,50);
        jCheckBox2.setBounds(80,20,50,50);

        this.getContentPane().add(jCheckBox, BorderLayout.NORTH);
        this.getContentPane().add(jCheckBox2, BorderLayout.CENTER);

        this.setSize(500,500);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo03();
    }
}
6.列表
  • 下拉框

JComboBox

import javax.swing.*;

public class Demo0 extends JFrame {
    public Demo0(){
        //下拉边框
        JComboBox JComboBox = new JComboBox();
        JComboBox.addItem("序号");
        JComboBox.addItem("站着看");
        JComboBox.addItem("饥荒");
        JComboBox.addItem("在一起");
        JComboBox.setBounds(50, 50, 200, 100);
        this.getContentPane().add(JComboBox);
        this.setSize(500,400);
        //设置界面文本
        this.setTitle("监听");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //取消默认居中
        this.setLayout(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo0();
    }
}
  • 列表框
import javax.swing.*;
import java.util.Vector;

public class Demo02 extends JFrame{
    public Demo02(){

        //生成列表内容
        //String [] strings={"1","2","3"};
        Vector vector = new Vector();
        //放入容器
        JList jList = new JList(vector);
        vector.add("1");
        vector.add("2");
        vector.add("3");
        jList.setBounds(50,100,100,100);
        this.getContentPane().add(jList);
        this.setAlwaysOnTop(true);
        this.setSize(500,500);
        this.setTitle("边框");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo02();
    }
}
  • 应用场景:1.单选框一般是地区 2.列表一般是展示信息,动态扩容
7.文本框
  • 文本框
import javax.swing.*;

public class Demo03 extends JFrame{
    public Demo03(){

        JTextField jTextField = new JTextField("hello");
        JTextField jTextField2 = new JTextField("Word" ,20);
        jTextField.setBounds(50,100,100,100);
        jTextField2.setBounds(50,200,100,100);

        this.getContentPane().add(jTextField);
        this.getContentPane().add(jTextField2);

        this.setAlwaysOnTop(true);
        this.setSize(500,500);
        this.setTitle("边框");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo03();
    }
}
  • 密码框
import javax.swing.*;

public class Demo03 extends JFrame{
    public Demo03(){

        JPasswordField jPasswordField = new JPasswordField();
        //不改隐藏样式,默认是黑点
        jPasswordField.setEchoChar('*');
        jPasswordField.setBounds(50,100,100,100);

        this.getContentPane().add(jPasswordField);

        this.setAlwaysOnTop(true);
        this.setSize(500,500);
        this.setTitle("边框");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo03();
    }
}
  • 文本域
import javax.swing.*;

public class Demo03 extends JFrame{
    public Demo03(){

        JTextArea jTextArea = new JTextArea();
        //不改隐藏样式,默认是黑点
        jTextArea.setBounds(50,100,100,100);

        this.getContentPane().add(jTextArea);

        this.setAlwaysOnTop(true);
        this.setSize(500,500);
        this.setTitle("边框");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo03();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值