赞
踩
package test;
import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class Close implements ItemListener{ JFrame f=null; JRadioButton r1=null; JRadioButton r2=null; JRadioButton r3=null; JButton bt1=null; public Close() { f = new JFrame("关机小程序:Black-love"); Container contentPane = f.getContentPane(); contentPane.setLayout(new GridLayout(2,1)); JPanel p1 = new JPanel(); JPanel p2=new JPanel(); p1.setLayout(new GridLayout(1,3)); p2.setLayout(new FlowLayout() ); p1.setBorder(BorderFactory.createTitledBorder("请选择")); r1 = new JRadioButton("关机"); r2 = new JRadioButton("重启"); r3 = new JRadioButton("注销"); r1.addItemListener(this); r2.addItemListener(this); r3.addItemListener(this); p1.add(r1); p1.add(r2); p1.add(r3); bt1=new JButton("确定"); p2.add(bt1); contentPane.add(p1); contentPane.add(p2); f.setSize(200,150); f.show(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub if(e.getSource() == r1)//关闭 { bt1.addActionListener(new ActionListener()//监听事件 { public void actionPerformed(ActionEvent event) { try { Runtime.getRuntime().exec("shutdown -s"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ); } else if(e.getSource()==r2)//重启 { bt1.addActionListener(new ActionListener()//监听事件 { public void actionPerformed(ActionEvent event) { try { Runtime.getRuntime().exec("shutdown -r"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ); } else if(e.getSource()==r3)//注销 { bt1.addActionListener(new ActionListener()//监听事件 { public void actionPerformed(ActionEvent event) { try { Runtime.getRuntime().exec("shutdown -l"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ); }} public static void main(String[] args) { new Close(); }
}
小编推荐:欲学习电脑技术、系统维护、网络管理、编程开发和安全攻防等高端IT技术,请 点击这里注册账号,公开课频道价值万元IT培训教程免费学,让您少走弯路、事半功倍,好工作升职加薪!
免责声明:本站系公益性非盈利IT技术普及网,本文由投稿者转载自互联网的公开文章,文末均已注明出处,其内容和图片版权归原网站或作者所有,文中所述不代表本站观点,若有无意侵权或转载不当之处请从网站右下角联系我们处理,谢谢合作!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。