How to use Java+swing to implement the confession program on Douyin
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());
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界面对"好的"按钮添加鼠标点击事件
3.1.2 跳转到resource界面后,对鼠标点击事件加上具体操作
//鼠标点击后就会弹出提示 FrameUtil.msg("好的,老婆我就知道你会同意的"); //结束程序 System.exit(0);
3.2 对"滚"这个按钮加上鼠标进入事件
3.2.1 在design界面对"滚"按钮添加鼠标进入事件
3.2.2 跳转到resource界面后,对鼠标进入事件加上具体操作
//弹出信息框,不断的挽留,不允许它退出程序 FrameUtil.msg("老婆大人,原谅我好吗?"); FrameUtil.msg("我错了,再也不敢把钱不上交了");
3.3 对"滚"这个按钮加上鼠标点击事件(点中随机位置了)
3.3.1 在design界面对"滚"按钮添加鼠标点击事件
3.3.2 跳转到resource界面后,对鼠标点击事件加上具体操作
//当用户点击到滚按钮的随机位置时,也要进行一波挽留操作,不允许拒绝 //弹窗弹出挽留语句 FrameUtil.msg("老婆大人,原谅我好吗?"); FrameUtil.msg("我错了,再也不敢把钱不上交了");
4.设置滚按钮的层级为最上面
无论怎么移动,都是最上层
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
