• java swing实现抖音上的表白程序


    带你手把手,用 java swing实现抖音上的表白程序

    1.准备工作

    a.需要下载一个带着swing插件的eclipse

    b.需要配置好JDK

    c.创建一个JFrame的项目(如下图所示的步骤)

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    d.把资源文件放入与src所在的那个目录

    ​ 步骤如下:

    1.先复制资源文件

    2.粘贴文件

    3.把jar文件放入Referenced Libraries文件夹下

    ​ 这第3步的具体操作如何所示
    在这里插入图片描述

    那么如何判断添加是否成功呢?

    解答:看Referenced Libraries下面是否出现了刚刚build path的

    两个文件,若出现了,则代表添加成功(成功的视图如下所示:)
    在这里插入图片描述

    e.design界面和source界面主要是干嘛的?

    source界面用于写源代码,主要是用于写触发按键某一事件,需要进行简单的逻辑判断
    design界面是通过可视化界面来帮我们进行界面的基本设计,直接拖拽即可,不用书写那些定义、基本属性的赋值这类的java代码了

    2.界面窗体的设计与实现

    整体的按钮的布局应该如下图所示
    在这里插入图片描述

    实现过程如下:

    ​ a.对窗体进行操作

    //设置窗体关闭模式 exit-退出程序 do_nothing退出没有任何操作
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //设置窗体的大小和坐标 x y  宽度 高度
    setBounds(100, 100, 584, 439);
    //居中显示
    setLocationRelativeTo(null);
    //设置窗体不可拖拽
    setResizable(false);
    //设置窗体的图标
    setIconImage(new ImageIcon("love.png").getImage());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    b.在design界面.根据刚刚的布局分布图,把按键移动到合适位置

    c.把gif图片设置为相应为相应按钮的图标

    lblNewLabel.setIcon(newImageIcon("E:\\Ueclipseworkspace\\love\\gfriend.gif"));
    
    • 1

    d.对剩下的组件进行颜色的设置

    //以button按钮为例,进行颜色的设置
    //setforeground是设置控件里面的字体颜色
    btnNewButton.setForeground(Color.WHITE);
    //setbackground是设置控件里面的背景颜色
    btnNewButton.setBackground(Color.PINK);
    //setforeground是设置控件里面字体类型以及字体大小
    btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.对按钮加上监听事件

    3.1 对"好的"这个按钮加上鼠标点击事件

    3.1.1 在design界面对"好的"按钮添加鼠标点击事件

    在这里插入图片描述

    3.2.2 跳转到resource界面后,对鼠标点击事件加上具体操作

    //鼠标点击后就会弹出提示
    FrameUtil.msg("好的,老婆我就知道你会同意的");
    //结束程序
    System.exit(0);
    
    • 1
    • 2
    • 3
    • 4

    3.2 对"滚"这个按钮加上鼠标进入事件

    3.2.1 在design界面对"滚"按钮添加鼠标进入事件

    在这里插入图片描述

    3.2.2 跳转到resource界面后,对鼠标进入事件加上具体操作

    //弹出信息框,不断的挽留,不允许它退出程序
    FrameUtil.msg("老婆大人,原谅我好吗?");
    FrameUtil.msg("我错了,再也不敢把钱不上交了");
    
    • 1
    • 2
    • 3

    3.3 对"滚"这个按钮加上鼠标点击事件(点中随机位置了)

    3.3.1 在design界面对"滚"按钮添加鼠标点击事件

    在这里插入图片描述

    3.3.2 跳转到resource界面后,对鼠标点击事件加上具体操作

    //当用户点击到滚按钮的随机位置时,也要进行一波挽留操作,不允许拒绝
    //弹窗弹出挽留语句
    FrameUtil.msg("老婆大人,原谅我好吗?");
    FrameUtil.msg("我错了,再也不敢把钱不上交了");
    
    • 1
    • 2
    • 3
    • 4

    4.设置滚按钮的层级为最上面,无论怎么移动,都是最上层

    在这里插入图片描述

    5.为界面添加一首背景音乐

    //前提:需要把他人写好的资源包build path到自己的项目中
    //需要在窗体可见之前进行设置
    FrameUtil.playMusic("嫁给我.mp3");
    //当这首歌的路径和src文件夹同级别时,这样写就可以了
    //这个放的位置在方法体外面
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5.源代码

    package demo;
    
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    
    import com.frame.util.FrameUtil;
    
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.Random;
    
    public class Love extends JFrame {
    
    	private JPanel contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Love frame = new Love();
    					//设置窗体不可见
    					
    //					FrameUtil.playMusic("嫁给我.mp3");
    					frame.setVisible(true);
    					
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    		
    		FrameUtil.playMusic("嫁给我.mp3");
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public Love() {
    		//设置窗体的大小
    		setTitle("\u9ED1\u51E4\u68A8");
    		//设置窗体关闭模式 exit-退出程序 do_nothing退出没有任何操作
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		//设置窗体的大小和坐标 x y  宽度 高度
    		setBounds(100, 100, 584, 439);
    		//剧中显示
    		setLocationRelativeTo(null);
    		//设置窗体不可拖拽
    		setResizable(false);
    		//设置窗体的图标
    		setIconImage(new ImageIcon("love.png").getImage());
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JButton button = new JButton("\u6EDA");
    		
    			button.setForeground(Color.WHITE);
    			button.setFont(new Font("微软雅黑", Font.BOLD, 15));
    			button.setBackground(Color.PINK);
    			button.setBounds(396, 273, 113, 27);
    			button.addMouseListener(new MouseAdapter() {
    				@Override
    				public void mouseEntered(MouseEvent arg0) {
    					Random random=new Random();
    					int x=random.nextInt(480);
    					int y=random.nextInt(380);
    					button.setBounds(x, y, 113, 27);
    				}
    				@Override
    				public void mouseClicked(MouseEvent e) {
    					FrameUtil.msg("老婆大人,原谅我好吗?");
    					FrameUtil.msg("我错了,再也不敢把钱不上交了");
    				}
    			});
    			contentPane.add(button);
    		
    		JLabel lblNewLabel = new JLabel("New label");
    		lblNewLabel.setIcon(new ImageIcon("E:\\Ueclipse-workspace\\love\\gfriend.gif"));
    		lblNewLabel.setBounds(14, 40, 200, 200);
    		contentPane.add(lblNewLabel);
    		
    		JLabel lblNewLabel_1 = new JLabel("\u5C0F\u59D0\u59D0\u6211\u559C\u6B22\u4F60\u5F88\u4E45\u4E86");
    		lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 20));
    		lblNewLabel_1.setForeground(Color.PINK);
    		lblNewLabel_1.setBounds(269, 57, 219, 73);
    		contentPane.add(lblNewLabel_1);
    		
    		JLabel label = new JLabel("\u505A\u6211\u5973\u670B\u53CB\u597D\u5417?");
    		label.setForeground(Color.RED);
    		label.setFont(new Font("微软雅黑", Font.BOLD, 20));
    		label.setBounds(269, 167, 219, 73);
    		contentPane.add(label);
    		
    		JButton btnNewButton = new JButton("\u597D\u7684");
    		btnNewButton.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
    				//JOptionPane.showMessageDialog(null,"我的");
    				FrameUtil.msg("好的,老婆我就知道你会同意的");
    				System.exit(0);
    			}
    		});
    		btnNewButton.setForeground(Color.WHITE);
    		btnNewButton.setBackground(Color.PINK);
    		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
    		btnNewButton.setBounds(254, 272, 113, 27);
    		contentPane.add(btnNewButton);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
  • 相关阅读:
    手把手实现WebRTC音视频通话
    Tailwind CSS功能类优先
    SpringBoot注册web组件
    手机照片备份方案Immich
    字符串的简单介绍和字符串的大小比较
    【网络编程】socket、端口、进程的关系
    7 进制数字转换
    Hadoop架构、Hive相关知识点及Hive执行流程
    leetcode解题思路分析(一百二十四)1025 - 1031 题
    Vue路由懒加载
  • 原文地址:https://blog.csdn.net/SSS4362/article/details/125383772