最终效果图:
<img src="https://img-blog.csdn.net/20140920140741474?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3lzdHFs/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
源代码
package com.tim.timer;
import java.awt.*;
import java.awt.event.*; //事件类
import javax.swing.*; //包含JFrame
public class CloseComputer extends JFrame implements ActionListener{
//创建成员变量
//创建实现BorderLayout布局的面板对象panel_main
private JPanel panel_main=new JPanel(new BorderLayout(5,10));
//创建实现FlowLayout布局的面板对象panel_subnorth
private JPanel panel_subnorth=new JPanel(new FlowLayout(3));
//创建实现FlowLayout布局的面板对象panel_subcenter
private JPanel panel_subcenter=new JPanel(new FlowLayout(1,5,3));
//创建了3个按钮对象countdown、time和cancel
private JButton countdown = new JButton("倒计时关机");
private JButton time = new JButton("定时关机");
private JButton cancel = new JButton("取消关机");
//创建标签对象tag
private JLabel tag;
//创建字符串对象key
String key;
//构造函数
public CloseComputer()
{
//添加对象panel_main到主窗口里
this.getContentPane().add(panel_main);
//添加对象panel_subnorth到对象pane_main窗口里
panel_main.add(panel_subnorth,BorderLayout.NORTH);
//添加对象panel_subnorth到对象pane_main窗口里
panel_main.add(panel_subcenter,BorderLayout.CENTER);
//添加标签对象tag到对象panel_subnorth里
panel_subnorth.add(tag=new JLabel("请选择关机方式:"));
//添加3个按钮到对象panel_subcenter里
panel_subcenter.add(countdown);
panel_subcenter.add(time);
panel_subcenter.add(cancel);
//为3个按钮注册事件监听器
countdown.addActionListener(this);;
time.addActionListener(this);
cancel.addActionListener(this);
}
public static void main(String[] args) throws Exception
{
CloseComputer frame=new CloseComputer();//创建CloseComputer对象
//设置窗口关闭功能
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(320,120);
frame.setTitle("关机工具");
frame.setLocation(350,350);
//设置显示
frame.setVisible(true);
frame.setResizable(false);
}
public void actionPerformed(ActionEvent eAction)
{
}
}
java关机工具之页面布局参考代码
最新推荐文章于 2023-05-07 12:23:26 发布