恶搞好朋友 java

该博客介绍了一个使用Java Swing编写的程序,该程序展示一个恶搞性质的对话框,询问用户对自己帅不帅的评价。根据用户的选择,程序会在10到30分钟后执行关机操作,或者取消关机。用户可以通过点击特定按钮来延迟关机或取消关机。这个程序涉及Java图形界面编程和操作系统交互。
摘要由CSDN通过智能技术生成


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;




//shutdown : 关机
        //加上参数才能执行
        //-s : 默认一分钟后关机
        //-s -t  指定时间 : 指定关机时间
        //-s:  取消关机操作
        //-r    关机重启

public class MyJframe extends JFrame implements ActionListener {

    JButton yesBut = new JButton("帅爆了");
    JButton midBut = new JButton("一般般吧");
    JButton noBut = new JButton("不帅 有点磕碜");
    JButton dadBut = new JButton("绕了我吧");

    //决定上方的按钮是否展示
    //true: 展示
    //false  不展示
    boolean flag = false;


    public MyJframe() {
        initJFrame();

        initView();


        //显示
        this.setVisible(true);
    }


    private void initView() {


        this.getContentPane().removeAll();

        if (flag) {
            //展示按钮
            JButton dadBut = new JButton("饶了我吧");
            dadBut.setBounds(50, 20, 100, 30);
            dadBut.addActionListener(this);
            this.getContentPane().add(dadBut);

        }


        JLabel text = new JLabel("你觉得自己帅吗?");
        text.setFont(new Font("微软雅黑", 0, 30));
        text.setBounds(120, 150, 300, 50);


        yesBut.setBounds(200, 250, 100, 30);
        midBut.setBounds(200, 325, 100, 30);
        noBut.setBounds(160, 400, 180, 30);

        yesBut.addActionListener(this);
        midBut.addActionListener(this);
        noBut.addActionListener(this);

        this.getContentPane().add(text);
        this.getContentPane().add(yesBut);
        this.getContentPane().add(midBut);
        this.getContentPane().add(noBut);

        this.getContentPane().repaint();

    }

    private void initJFrame() {
        //设置宽高
        this.setSize(500, 600);
        //设置标题
        this.setTitle("恶搞好基友");
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //置顶
        this.setAlwaysOnTop(true);
        //居中
        this.setLocationRelativeTo(null);
        //取消内部默认布局
        this.setLayout(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == yesBut) {
            //给好基友一个弹框
            showJDialog("xxx,你太自信了,给你一点小惩罚");

            try {
                Runtime.getRuntime().exec("shutdown -s -t 10");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            flag = true;
            initView();

        } else if (obj == midBut) {
//            System.out.println("你的好基友点击了一般般吧");
            showJDialog("xxx,你还是太自信了,还要给你一点小惩罚");

            try {
                Runtime.getRuntime().exec("shutdown -s -t 20");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            flag = true;
            initView();

        } else if (obj == noBut) {
//            System.out.println("你的好基友点击了不帅");

            showJDialog("xxx,你还是有自知之明的,还要给你一点小惩罚");

            try {
                Runtime.getRuntime().exec("shutdown -s -t 30");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            flag = true;
            initView();

        } else if (obj == dadBut) {
            showJDialog("xxx,这次饶了你");

            try {
                Runtime.getRuntime().exec("shutdown -a");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建JLabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }

   


}


测试:

public class Test {
    public static void main(String[] args) {
        new MyJframe();
    }
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值