表白窗口,基于Java实现

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

目录

文章目录

前言

一、目标

二、代码

1、引入库

2、创建实例型成员变量

3、创建主窗口

4、点击按钮弹出窗口

1、同意窗口

2、拒绝窗口

5、测试类

总结

总代码

1、封装类

2、测试类


前言

使用Java编写的一个表白窗口,大家有什么建议可以指出,多多关照。


一、目标

        创建一个窗口,窗口有两个按钮,同意和拒绝,只能通过同意关闭窗口。

二、代码

1、引入库

 代码如下:

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.Random;


2、创建实例型成员变量

代码如下:

// 定义实例成员变量
    String Pathname; // 文件地址
    String Pathname1;
    String Pathname2;
    String Title; // 窗口名称
    String Text1; // 按钮1名称
    String Text2; // 按钮2名称
    String Passage; // 窗口内容
    String Passage1;
    String Passage2;
    int x; // 窗口关闭类型0,1,2,3

    int Width1; // 窗口宽度
    int Height1; // 窗口高度

    int Width2; // 图片宽度
    int Height2; // 图片高度


3、创建主窗口

public void WindowOne(){
        javax.swing.JFrame jf = new javax.swing.JFrame();
        jf.setTitle(Title);
        jf.setSize(Width1, Height1);
        jf.setDefaultCloseOperation(x);

        jf.setLocationRelativeTo(null);

        Random q = new Random();
        int num1 = q.nextInt(200, 500);
        int num2 = q.nextInt(-50, 1000);
        jf.setLocation(num2, num1);
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int option = JOptionPane.showConfirmDialog(null, "这个窗口关不掉",
                        "温馨提示", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.CANCEL_OPTION)
                    System.exit(0);
            }
        });
        java.awt.FlowLayout flow = new java.awt.FlowLayout();
        jf.setLayout(flow);// 设置窗体布局

        JLabel jla = new JLabel(Passage);// 窗口内容,jla为窗口
        jf.add(jla);
        File zhu = new File(Pathname);//引用文件的地址
        javax.swing.ImageIcon icon = new ImageIcon(String.valueOf(zhu));
        javax.swing.JLabel jl = new JLabel(icon);
        java.awt.Dimension dm0 = new Dimension(Width2, Height2);// 设置图片大小
        jl.setPreferredSize(dm0);
        jf.add(jl);
        jf.setVisible(true);// 设置窗口可见
        javax.swing.JButton jbu1 = new JButton(Text1);
        jf.add(jbu1);// 添加按钮1

        javax.swing.JButton jbu2 = new JButton(Text2);
        jf.add(jbu2);// 添加按钮2

        jbu1.addActionListener(e -> ChildWindow1()); //按钮监听

        jbu2.addActionListener(e -> ChildWindow2()); //按钮监听

    }

4、点击按钮弹出窗口

1、同意窗口

    public void ChildWindow1(){
        JFrame fra = new JFrame();
        fra.setSize(Width1, Height1);
        fra.setDefaultCloseOperation(x);
        fra.setLocationRelativeTo(null);

        Random r = new Random();
        int num = r.nextInt(100, 400);
        fra.setLocation(num + 200, num + 100);

        File yes = new File(Pathname1);
        ImageIcon icon1 = new ImageIcon(String.valueOf(yes));
        JLabel jl2 = new JLabel(icon1);
        Dimension dm01 = new Dimension(Width2, Height2);
        jl2.setPreferredSize(dm01);
        fra.add(jl2);

        FlowLayout fl = new FlowLayout();
        fra.setLayout(fl);
        JLabel jl12 = new JLabel(Passage1);
        fra.add(jl12);
        fra.setVisible(true);
    }

2、拒绝窗口

 public void ChildWindow2(){
        JFrame fra = new JFrame();
        fra.setSize(Width1, Height1);
        fra.setDefaultCloseOperation(x);
        fra.setLocationRelativeTo(null);

        Random r = new Random();
        int num = r.nextInt(100, 600);
        fra.setLocation(num + 400, num + 100);

        File no = new File(Pathname2);
        ImageIcon icon2 = new ImageIcon(String.valueOf(no));
        JLabel jl1 = new JLabel(icon2);
        Dimension dm012 = new Dimension(Width2, Height2);
        jl1.setPreferredSize(dm012);
        fra.add(jl1);

        FlowLayout fl = new FlowLayout();
        fra.setLayout(fl);
        JLabel jl22 = new JLabel(Passage2);
        fra.add(jl22);
        fra.setVisible(true);
    }

5、测试类

public class TestGirlfriend {
    public static void main(String[] args) {
        WindowFirst window1 = new WindowFirst();
        window1.Title = "表白器";
        window1.Width1 = 250;
        window1.Height1 = 220;
        window1.x = 0;
        window1.y = 3;
        window1.z = 2;
        window1.Text1 = "我愿意!";
        window1.Text2 = "我不愿意!";
        window1.Passage = "我喜欢你,做我女朋友吧!";
        window1.Passage1 = "恭喜你成为了我的女朋友!";
        window1.Passage2 = "在考虑一下嘛~";
        window1.Width2 = 250;
        window1.Height2 = 100;
        window1.Pathname = "src/images/1.jpg";
        window1.Pathname1 = "src/images/3.jpg";
        window1.Pathname2 = "src/images/2.jpg";

        window1.WindowOne();
    }
}

总结

代码分为两个类,先封装一个类,然后从测试类调用封装类,从而实现功能。

总代码

1、封装类

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Random;


public class WindowFirst {
    // 定义实例成员变量
    String Pathname; // 文件地址
    String Pathname1;
    String Pathname2;
    String Title; // 窗口名称
    String Text1; // 按钮1名称
    String Text2; // 按钮2名称
    String Passage; // 窗口内容
    String Passage1;
    String Passage2;
    int x,y,z; // 窗口关闭类型0,1,2,3

    int Width1; // 窗口宽度
    int Height1; // 窗口高度

    int Width2; // 图片宽度
    int Height2; // 图片高度


    public void WindowOne(){
        javax.swing.JFrame jf = new javax.swing.JFrame();
        jf.setTitle(Title);
        jf.setSize(Width1, Height1);
        jf.setDefaultCloseOperation(x);

        jf.setLocationRelativeTo(null);

        Random q = new Random();
        int num1 = q.nextInt(200, 500);
        int num2 = q.nextInt(-50, 1000);
        jf.setLocation(num2, num1);
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int option = JOptionPane.showConfirmDialog(null, "这个窗口关不掉",
                        "温馨提示", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.CANCEL_OPTION)
                    System.exit(0);
            }
        });
        java.awt.FlowLayout flow = new java.awt.FlowLayout();
        jf.setLayout(flow);// 设置窗体布局

        JLabel jla = new JLabel(Passage);// 窗口内容,jla为窗口
        jf.add(jla);
        File zhu = new File(Pathname);//引用文件的地址
        javax.swing.ImageIcon icon = new ImageIcon(String.valueOf(zhu));
        javax.swing.JLabel jl = new JLabel(icon);
        java.awt.Dimension dm0 = new Dimension(Width2, Height2);// 设置图片大小
        jl.setPreferredSize(dm0);
        jf.add(jl);
        jf.setVisible(true);// 设置窗口可见
        javax.swing.JButton jbu1 = new JButton(Text1);
        jf.add(jbu1);// 添加按钮1

        javax.swing.JButton jbu2 = new JButton(Text2);
        jf.add(jbu2);// 添加按钮2

        jbu1.addActionListener(e -> ChildWindow1());

        jbu2.addActionListener(e -> ChildWindow2());

    }


    public void ChildWindow1(){
        JFrame fra = new JFrame();
        fra.setSize(Width1, Height1);
        fra.setDefaultCloseOperation(y);
        fra.setLocationRelativeTo(null);

        Random r = new Random();
        int num = r.nextInt(100, 400);
        fra.setLocation(num + 200, num + 100);

        File yes = new File(Pathname1);
        ImageIcon icon1 = new ImageIcon(String.valueOf(yes));
        JLabel jl2 = new JLabel(icon1);
        Dimension dm01 = new Dimension(Width2, Height2);
        jl2.setPreferredSize(dm01);
        fra.add(jl2);

        FlowLayout fl = new FlowLayout();
        fra.setLayout(fl);
        JLabel jl12 = new JLabel(Passage1);
        fra.add(jl12);
        fra.setVisible(true);
    }


    public void ChildWindow2(){
        JFrame fra = new JFrame();
        fra.setSize(Width1, Height1);
        fra.setDefaultCloseOperation(z);
        fra.setLocationRelativeTo(null);

        Random r = new Random();
        int num = r.nextInt(100, 600);
        fra.setLocation(num + 400, num + 100);

        File no = new File(Pathname2);
        ImageIcon icon2 = new ImageIcon(String.valueOf(no));
        JLabel jl1 = new JLabel(icon2);
        Dimension dm012 = new Dimension(Width2, Height2);
        jl1.setPreferredSize(dm012);
        fra.add(jl1);

        FlowLayout fl = new FlowLayout();
        fra.setLayout(fl);
        JLabel jl22 = new JLabel(Passage2);
        fra.add(jl22);
        fra.setVisible(true);
    }
}

2、测试类

public class TestGirlfriend {
    public static void main(String[] args) {
        WindowFirst window1 = new WindowFirst();
        window1.Title = "表白器";
        window1.Width1 = 250;
        window1.Height1 = 220;
        window1.x = 0;
        window1.y = 3;
        window1.z = 2;
        window1.Text1 = "我愿意!";
        window1.Text2 = "我不愿意!";
        window1.Passage = "我喜欢你,做我女朋友吧!";
        window1.Passage1 = "恭喜你成为了我的女朋友!";
        window1.Passage2 = "在考虑一下嘛~";
        window1.Width2 = 250;
        window1.Height2 = 100;
        window1.Pathname = "src/images/1.jpg";
        window1.Pathname1 = "src/images/3.jpg";
        window1.Pathname2 = "src/images/2.jpg";

        window1.WindowOne();
    }
}

我自己打包成了一个软件,大家可以尝试运行

表白器exe文件,弹窗版,使用Java语言编写-桌面系统文档类资源-CSDN文库icon-default.png?t=M85Bhttps://download.csdn.net/download/qq_62899128/86762487

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值