| 关键词: nbsp new JRadioButton public catch contentPane generated 监听 addActionListene addItemListener |
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(); } } |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|