( ̄o ̄) . z Z683 2023-03-27 22:42 采纳率: 30%
浏览 43
已结题

Java程序运行后再次点击按钮会重复弹窗

该程序实现关机与重启操作。程序编译正常,但是Java程序运行后再次点击按钮会重复弹窗,每多一次按按钮弹窗越多。

import com.fjl.uc.MyJframe;

/**
 * @author FJL
 * @version 1.0
 */
public class app {
    public static void main(String[] args) {
        new MyJframe();

    }
}


package com.fjl.uc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class MyJframe extends JFrame implements ActionListener {

    JButton shotdown1= new JButton("关机");
    JButton restart1 = new JButton("重启");
    JButton shotdown2 = new JButton("定时关机1h");
    JButton restart2 = new JButton("定时重启1h");
    JButton cancel = new JButton("取消操作");

    boolean flag = false;


    public MyJframe(){
        initJFrame();

        initView();

        //显示
        this.setVisible(true);
    }

    private void initView() {

        this.getContentPane().removeAll();

        if(flag == true){
            cancel.setBounds(50,20,100,30);
            cancel.addActionListener(this);
            this.getContentPane().add(cancel);
        }


        JLabel text = new JLabel("关机程序");
        text.setFont(new Font("微软雅黑",0,30));
        text.setBounds(190,80,300,50);


        shotdown1.setBounds(200,150,100,30);
        restart1.setBounds(200,250,100,30);
        shotdown2.setBounds(200,350,100,30);
        restart2.setBounds(200,450,100,30);

        shotdown1.addActionListener(this);
        restart1.addActionListener(this);
        shotdown2.addActionListener(this);
        restart2.addActionListener(this);

        this.getContentPane().add(text);
        this.getContentPane().add(shotdown1);
        this.getContentPane().add(restart1);
        this.getContentPane().add(shotdown2);
        this.getContentPane().add(restart2);

        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 == shotdown1){
         //关机
            flag = true;
            showJDialog("计算机将立即关机");

            try {
                Runtime.getRuntime().exec("shutdown -s -t 0");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

            initView();
        }else if(obj == restart1){
         //重启
            flag = true;
            showJDialog("计算机将立即重启");
            try {
                Runtime.getRuntime().exec("shutdown -r -t 0");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            initView();
        }else if(obj == shotdown2){
         //定时关机
            flag = true;
            showJDialog("计算机将于1hour后关机");
            try {
                Runtime.getRuntime().exec("shutdown -s -t 3600");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            initView();
        }else if(obj == restart2){
         //定时重启
            flag = true;
            showJDialog("计算机将于1hour后重启");
            try {
                Runtime.getRuntime().exec("shutdown -r -t 3600");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            initView();
        }else if(obj == cancel){
            //取消关机操作
            flag = false;
            showJDialog("操作已取消");
            try {
                Runtime.getRuntime().exec("shutdown -a");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            initView();
        }


    }


    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);
        //关闭模式
        jDialog.setDefaultCloseOperation(2);

        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }
}


  • 写回答

3条回答 默认 最新

  • 关注

    img


    你把所有button的addActionListener都放构造方法里,initView方法里对应的addActionListener都删掉就好了。
    你每点击一次按钮都调用一次initView,我猜你是想控制那个取消按钮,但其实没必要,你应该把取消按钮那个部分直接放到弹窗那个代码后面就行了。或者你用我上面截图的代码也行。
    这个是因为你每调用一次initView,它都会执行一次addActionListener,但你那些button是全局的,对于frame来说,它从pane上移除了并不是这个button对象销毁了,button的对象引用还在frame里,你再次把它add到panel里,还是原来那个button,但是你又绑定了一个listener,所以点一次就绑定一次。你把它放在构造方法里,就只绑定一次了。
    你可以去了解一下java里对象和引用之间的关系,就明白了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月27日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改