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