发布一个Java关机源代码

这个Java程序创建了一个简单的GUI,包含两个按钮:'关机'和'取消'。点击'关机'按钮会执行操作系统关机命令,而'取消'则会停止关机进程。程序使用了FlowLayout布局管理器,并设置了自定义的按钮尺寸和位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package org.shutdown.demo;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ShutDownComputer implements ActionListener{
    Component panel2,panel3,panel4;
    JButton shutdown,reshutdown;
    public JFrame frame;
    JScrollPane scroll;
    public ShutDownComputer() {
            frame=new JFrame();
            ImageIcon img = new ImageIcon(ShutDownComputer.class.getResource("sun.png"));//这是背景图片
         
            JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
         
            frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意这里是关键,将背景标签
            imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());//设置背景标签的位置
            Container cp=frame.getContentPane();
            ((JPanel)cp).setOpaque(false); //注意这里,将内容面板设为透明。这样LayeredPane面板中的背景才能显示

            //label控件
            JLabel label=new JLabel("关机");
            //设置颜色
            label.setForeground(Color.blue);
            label.setFont(new Font("宋体",Font.PLAIN,12));
            label.setBackground(Color.red);
            //启用关闭按钮
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //设置布局
            frame.setLayout(new FlowLayout());
            //frame的显示坐标
            //frame.setBounds(0,0, 400, 300);
            frame.setSize(300,100);
            int w = (Toolkit.getDefaultToolkit().getScreenSize().width) / 2-500;
            int h = (Toolkit.getDefaultToolkit().getScreenSize().height) / 2-350;
            frame.setLocation(w, h);
            frame.add(label);
            frame.add(Box.createHorizontalStrut(30000));

            Dimension preferredSize = new Dimension(40,20);      
            shutdown = new JButton("关机");
            shutdown.addActionListener(this);
            shutdown.setMargin(new java.awt.Insets(0,0,0,0));
            shutdown.setPreferredSize(preferredSize );
            
            reshutdown = new JButton("取消");
            reshutdown.addActionListener(this);
            reshutdown.setMargin(new java.awt.Insets(0,0,0,0));
            reshutdown.setPreferredSize(preferredSize );
            
            frame.add(Box.createHorizontalStrut(30000));
            frame.add(shutdown);
            frame.add(reshutdown);

            frame.setVisible(true);   
        }  
        

        public static void main(String[] args) {
             new ShutDownComputer();
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==shutdown){
                  String Command = "shutdown -s -t 10";   
                    try {
                       Runtime.getRuntime().exec(Command);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }  
            }else if(e.getSource()==reshutdown){
                String Command = "shutdown.exe -a";   
                try {
                   Runtime.getRuntime().exec(Command);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }  
            }
        }   
}
目录结构

打成jar包后运行效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值