JAVA GUI 基本使用

package com.lu.gui;

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

public class MyJFrame extends JFrame {
    public MyJFrame() {
        this.setBackground(Color.BLACK);
        this.setResizable(false);
        this.setSize(500,500);
        this.setTitle("登录页面");
    }
}
package com.lu.gui;

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

public class MyJpanel extends JPanel {
    public MyJpanel(){
        this.setBackground(Color.black);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.cyan);
        g.setFont(new Font("微软雅黑",Font.BOLD,20));
        g.drawString("I like you",250,100);
        BufferedImage bufferedImage = ImgUtil.readImage("img/l.jpg");
        g.drawImage(bufferedImage,0,0,500,500,null);
        //取消居中布局->自定义组件位置
        this.setLayout(null);

        //标签组件
        JLabel jLabel = new JLabel("用户登录:");
        jLabel.setBounds(100,100,100,30);
        this.add(jLabel);

        //单行文本框
        JTextField jTextField = new JTextField();
        jTextField.setText("请输入用户名");
        jTextField.setBounds(160,100,150,30);
        this.add(jTextField);

        //标签组件
        JLabel jLabel1 = new JLabel("密    码:");
        jLabel1.setBounds(100,150,100,30);
        this.add(jLabel1);

        //密码框
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setBounds(160,150,150,30);
        this.add(jPasswordField);

        JButton jButton = new JButton("登录");
        jButton.setBounds(180,200,80,20);
        this.add(jButton);
    }
}
package com.lu.gui;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

public class ImgUtil {
    /**
     * 读取图片
     */
    public static BufferedImage readImage(String path) {
        //获取类对象
        Class<ImgUtil> imgUtilClass = ImgUtil.class;
        //获取类加载器(系统类加载器)
        ClassLoader classLoader = imgUtilClass.getClassLoader();
        //通过类加载器获取内存静态资源
        InputStream resourceAsStream = classLoader.getResourceAsStream(path);
        Objects.requireNonNull(resourceAsStream,"请检查文件路径"+path);
        try {
            BufferedImage read = ImageIO.read(resourceAsStream);
            return read;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
package com.lu.gui;

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

        MyJFrame myJFrame = new MyJFrame();
        myJFrame.setVisible(true);
        MyJpanel myJpanel = new MyJpanel();
        myJFrame.add(myJpanel);


    }
}

gui登录页面

package com.lu.gui.ui;

import com.lu.gui.util.PoinUtil;

public class App {
    public static void main(String[] args) {
        StuFrame stuFrame = new StuFrame("登录页面");

        LoginJPanel loginJPanel = new LoginJPanel();
        loginJPanel.addMouseListener(PoinUtil.printPoint());
        stuFrame.add(loginJPanel);
        stuFrame.setVisible(true);
    }
}
package com.lu.gui.ui;

import com.lu.gui.constants.StuWindowConstants;
import com.lu.gui.util.ImgUtil;
import com.lu.gui.util.RadioUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Objects;

/**
 * 登录面板
 */
public class LoginJPanel  extends JPanel {
    public LoginJPanel() {
        //取消布局
        this.setLayout(null);
        //文本框
        JTextField jTextField = new JTextField();
        jTextField.setText("用户名");
        jTextField.setBounds(424,200,180,30);
        this.add(jTextField);
        //创建密码显示和隐藏的图标
        ImageIcon imageIcon1 = new ImageIcon(Objects.requireNonNull(this.getClass().getClassLoader().getResource("img/眼睛_隐藏.png")));
        ImageIcon imageIcon2 = new ImageIcon(Objects.requireNonNull(this.getClass().getClassLoader().getResource("img/眼睛_显示.png")));

        //密码框
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setBounds(424,250,180,30);
        this.add(jPasswordField);
        //标签组件
        JLabel jLabel = new JLabel("用户名:");
        jLabel.setBounds(365,200,100,30);
        jLabel.setFont(new Font("宋体",Font.BOLD,15));
        this.add(jLabel);

        JLabel jLabel1 = new JLabel("密  码:");
        jLabel1.setBounds(365,250,100,30);
        jLabel1.setFont(new Font("宋体",Font.BOLD,15));
        this.add(jLabel1);

        //登录按钮
        JButton jButton = new JButton("登录");
        jButton.setBounds(525,298,80,20);
        this.add(jButton);

        //注册按钮
        JButton enroll = new JButton("注册");
        enroll.setBounds(424,298,80,20);
        this.add(enroll);
        //设置动图
        ImageIcon imageIcon =  new ImageIcon(Objects.requireNonNull(this.getClass().getClassLoader().getResource("img/pdx.gif")));
        JLabel jLabel2 = new JLabel(imageIcon);
        jLabel2.setBounds(877,576,90,90);
        this.add(jLabel2);
        //控制密码显示隐藏框
        JCheckBox jCheckBox = new JCheckBox(imageIcon1);
        //设置密码框透明
        jCheckBox.setOpaque(false);
        jCheckBox.setBounds(613,257,20,20);
        //添加选中或未选中事件监听
        jCheckBox.addItemListener(event->{
            int stateChange = event.getStateChange();
            if (stateChange == ItemEvent.SELECTED){
                jCheckBox.setIcon(imageIcon2);
                jPasswordField.setEchoChar((char)0);
            }else if(stateChange == ItemEvent.DESELECTED){
                jCheckBox.setIcon(imageIcon1);
                //不显示密码
                jPasswordField.setEchoChar('•');
            }
        });
        this.add(jCheckBox);

    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        BufferedImage bufferedImage = ImgUtil.readImage("img/l.jpg");
        g.drawImage(bufferedImage,0,0, StuWindowConstants.WIDTH,StuWindowConstants.HEIGHT,null);
    }
}
package com.lu.gui.ui;

import com.lu.gui.constants.StuWindowConstants;

import javax.swing.*;

public class StuFrame  extends JFrame {
    public StuFrame(String title){
        this.setTitle(title);
        this.setSize(StuWindowConstants.WIDTH,StuWindowConstants.HEIGHT);
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }
}
package com.lu.gui.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

public class ImgUtil {
    /**
     * 读取图片
     */
    public static BufferedImage readImage(String path) {
        //获取类对象
        Class<ImgUtil> imgUtilClass = ImgUtil.class;
        //获取类加载器(系统类加载器)
        ClassLoader classLoader = imgUtilClass.getClassLoader();
        //通过类加载器获取内存静态资源
        InputStream resourceAsStream = classLoader.getResourceAsStream(path);
        Objects.requireNonNull(resourceAsStream,"请检查文件路径"+path);
        try {
            BufferedImage read = ImageIO.read(resourceAsStream);
            return read;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
package com.lu.gui.util;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.StringJoiner;

public class PoinUtil {
    private PoinUtil(){}
    public static MouseAdapter printPoint(){
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
                stringJoiner.add(String.valueOf(e.getX()));
                stringJoiner.add(String.valueOf(e.getY()));
                System.out.println("坐标为"+stringJoiner);
            }
        };
    }
}
package com.lu.gui.util;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;
import java.util.concurrent.*;


/**
 * 播放音频工具类
 */
public class RadioUtil {
    private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 8, 1
            , TimeUnit.MINUTES, new ArrayBlockingQueue<>(30));
    public static final RadioUtil radio = new RadioUtil();
    private RadioUtil() {
    }





    /**
     * 播放wav音频
     *
     * @param radioPath 音频地址
     */
    public static void playWav(String radioPath) {
        URL resource = radio.getClass().getClassLoader().getResource(radioPath);
        Objects.requireNonNull(resource,"请检查音频路径是否正确");
        Thread thread = new Thread(() -> {
            try {
                AudioInputStream stream = AudioSystem.getAudioInputStream(resource);
                Clip clip = AudioSystem.getClip();
                clip.open(stream);
                clip.start();
                Thread.sleep(clip.getMicrosecondLength() / 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        thread.setDaemon(true);
        threadPoolExecutor.submit(thread);

    }

    /**
     * 播放MP3
     * @param radioPath 路径
     */
    public static void playMp3(String radioPath){
        InputStream resourceAsStream = radio.getClass().getClassLoader().getResourceAsStream(radioPath);
        Objects.requireNonNull(resourceAsStream,"请检查音频路径是否正确");
        Thread thread = new Thread(() -> {
            AdvancedPlayer player = null;
            try {
                player = new AdvancedPlayer(resourceAsStream);
                player.play();
            } catch (JavaLayerException e) {
                e.printStackTrace();
            }
        });
        thread.setDaemon(true);
        threadPoolExecutor.submit(thread);

    }
}

package com.lu.gui.constants;

public interface StuWindowConstants {
    int WIDTH = 1000;
    int HEIGHT = 700;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值