search
HomeJavajavaTutorialHow to use Java+swing to implement the confession program on Douyin

1.准备工作

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

b.需要配置好JDK

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

How to use Java+swing to implement the confession program on Douyin

How to use Java+swing to implement the confession program on Douyin

How to use Java+swing to implement the confession program on Douyin

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

步骤如下:

1.先复制资源文件

2.粘贴文件

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

这第3步的具体操作如何所示

How to use Java+swing to implement the confession program on Douyin

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

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

两个文件,若出现了,则代表添加成功(成功的视图如下所示:)

How to use Java+swing to implement the confession program on Douyin

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

source界面用于写源代码,主要是用于写触发按键某一事件,需要进行简单的逻辑判断

design界面是通过可视化界面来帮我们进行界面的基本设计,直接拖拽即可,不用书写那些定义、基本属性的赋值这类的java代码了

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

整体的按钮的布局应该如下图所示

How to use Java+swing to implement the confession program on Douyin

实现过程如下:

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

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

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

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

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

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

3.对按钮加上监听事件

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

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

How to use Java+swing to implement the confession program on Douyin

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

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

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

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

How to use Java+swing to implement the confession program on Douyin

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

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

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

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

How to use Java+swing to implement the confession program on Douyin

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

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

4.设置滚按钮的层级为最上面

无论怎么移动,都是最上层

How to use Java+swing to implement the confession program on Douyin

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

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

6.源代码

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

The above is the detailed content of How to use Java+swing to implement the confession program on Douyin. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.