美文网首页
用java写一个抖音同款撩(ai)妹(da)程序

用java写一个抖音同款撩(ai)妹(da)程序

作者: GALAace | 来源:发表于2018-06-27 02:11 被阅读0次

首先创建项目,new一个JFrame类,先修改一下logo,把默认小咖啡换掉顺便让窗口运行时自动居中


默认logo

更换logo后,导出的jar包和状态栏的图标都会随之改变

//更换logo
Image icon = Toolkit.getDefaultToolkit().getImage("logo.png");
frame.setIconImage(icon);
//居中
frame.setLocationRelativeTo(null);
更换后效果

先加两个按钮,为了美观隐藏焦点框

        JButton btnYes = new JButton("好");
        btnYes.setFont(new Font("幼圆", Font.PLAIN, 15));
        btnYes.setBackground(Color.PINK);
        //隐藏焦点框
        btnYes.setFocusPainted(false);
        btnYes.setBounds(74, 240, 95, 30);
        contentPane.add(btnYes);
        
        JButton btnNo = new JButton("不好");
        btnNo.setFont(new Font("幼圆", Font.PLAIN, 15));
        btnNo.setFocusPainted(false);
        btnNo.setBackground(Color.PINK);
        btnNo.setBounds(244, 240, 95, 30);
        contentPane.add(btnNo);
加两个按钮

在给按钮设置监听器前,我们先要获取一下当前窗口的大小,然后写一个生成随机按钮位置的方法,这里需要给这个方法传两个参数就是上面获取的窗口长和宽,防止按钮跑到窗口外面去

    //按钮随机位置
    public static void randomButton() {
        //获取窗口大小
        Dimension size = frame.getSize();
        int ww = (int) size.getWidth();
        int wh = (int) size.getHeight();
        System.out.println(ww+","+wh);
        //设置按钮位置
        int bw = new Random().nextInt(ww-100);
        int bh = new Random().nextInt(wh-100);
        btnNo.setBounds(bw, bh, 95, 30);
    }

现在为"不好"按钮添加鼠标事件然后在mouseEntered方法下调用randomButton()就可实现按钮乱跑的效果了

        btnNo.addMouseListener(new MouseListener() {
            
            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("释放");
            }
            
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("按下");
            }
            
            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("离开");
            }
            
            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("进入");
                //按钮随机位置
                randomButton();
            }
            
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("单击");
            }
        });
效果

我们加上文字和图片

        JLabel lblNewLabel = new JLabel("小姐姐!");
        lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 22));
        lblNewLabel.setForeground(new Color(255, 20, 147));
        lblNewLabel.setBounds(60, 45, 95, 37);
        contentPane.add(lblNewLabel);

        JLabel label = new JLabel("做我女朋友好不好?!");
        label.setForeground(new Color(255, 20, 147));
        label.setFont(new Font("幼圆", Font.PLAIN, 20));
        label.setBounds(32, 110, 206, 30);
        contentPane.add(label);

        JLabel limg1 = new JLabel(new ImageIcon("a.png"));
        limg1.setBounds(217, 86, 165, 154);
        contentPane.add(limg1);
        JLabel limg2 = new JLabel(new ImageIcon("mua.png"));
        limg2.setBounds(266, 202, 147, 134);
        contentPane.add(limg2);
        limg2.setVisible(false);
        JLabel limg3 = new JLabel(new ImageIcon("ye.png"));
        limg3.setBounds(-13, -17, 123, 126);
        contentPane.add(limg3);
        limg3.setVisible(false);

接下来把标题栏的关闭按钮改一下,不然直接退出了就不好玩了emmm

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                // 弹出提示
                JOptionPane.showMessageDialog(null, "你还没选呢!", "不行!", JOptionPane.PLAIN_MESSAGE);
            }
        });
点击关闭

最后,给"好"按钮加上监听事件

        btnYes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 换图片
                limg1.setVisible(false);
                limg2.setVisible(true);
                limg3.setVisible(true);
                lblNewLabel.setVisible(false);
                label.setVisible(false);
                // 弹出提示
                JOptionPane.showMessageDialog(null, "获得男朋友一个!", "恭喜!", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
        });
点击"好"

到这里差不多就完成了,也就一百多行代码,做的比较粗糙有空再优化,最后完整演示一下


最终效果

相关文章

网友评论

      本文标题:用java写一个抖音同款撩(ai)妹(da)程序

      本文链接:https://www.haomeiwen.com/subject/mytvyftx.html