繁体   English   中英

退出按钮不起作用后动作列表器

[英]Exit Button is not working After Action Listerner

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class StudentGUI extends JFrame implements ActionListener {

    public StudentGUI() {
        super("StudentGUI Frame");

        //TopPanel
        TopPanel tp;
        tp=new TopPanel();
        Dimension d = new Dimension(800,600);
        tp.setPreferredSize(d);
        this.add (tp, BorderLayout.NORTH);
        tp.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        setBackground(Color.PINK);
        tp.setVisible(true);
        //TopPanel End

        //BottomPanel
        BottomPanel bp;
        bp=new BottomPanel();
        this.add (bp, BorderLayout.SOUTH);
        tp.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        //BottomPanel End

        //MiddlePanel 
        MiddlePanel mp;
        mp=new MiddlePanel();
        this.add (mp, BorderLayout.CENTER);
        mp.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        //MiddlePanel End

        this.setVisible(true);
    }

    exitBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e){
            int selectedOption = JOptionPane.showConfirmDialog(null, 
                                    "Do you want to close the window?", 
                                    "Choose", 
                                    JOptionPane.YES_NO_OPTION); 
            if (selectedOption == JOptionPane.YES_OPTION) {
                System.exit(1);
            }
        }
    });

    public static void main(String[] args) {
        new StudentGUI();
    }
}

这是代码,我不确定出什么问题了,我错过了什么吗? 代码将执行,但是什么也没有执行。.如何使动作监听器和动作执行? 它根本没有实现该按钮。

做到这一点,以便当用户单击“退出”按钮时,将弹出一个MessageBox窗口,并询问用户是否确定要退出。 如果他们说“是”,则退出应用程序。

底部面板:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;

public class BottomPanel extends JPanel {

    public String findbtn="";
    public String insertBtn="";
    public String updateBtn="";
    public String deleteBtn="";
    public String exitBtn="";

    public BottomPanel() {
        JButton findbtn;
        findbtn=new JButton("Find");
        add(findbtn);

        JButton insertBtn;
        insertBtn=new JButton("Insert");
        add(insertBtn);

        JButton updateBtn;
        updateBtn=new JButton("Update");
        add(updateBtn);

        JButton deleteBtn;
        deleteBtn=new JButton("Delete");
        add(deleteBtn);

        JButton exitBtn;
        exitBtn=new JButton("Exit");
        add(exitBtn);
        exitBtn.addActionListener(this);
    }
}

您的ActionListener基本上不执行任何操作,请尝试将JOptionPane对话框移至actionPerform方法内。

exitBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            int selectedOption = JOptionPane.showConfirmDialog(null, 
                              "Do you want to close the window?", 
                              "Choose", 
                              JOptionPane.YES_NO_OPTION); 
            if (selectedOption == JOptionPane.YES_OPTION) {
                    System.exit(1);

            }
        }
    });

检查此示例代码

public class StudentGUI extends JFrame implements ActionListener {

    JButton b;
    public StudentGUI() {
        setSize(400, 400);
        b = new JButton("Exit");

        this.setLayout(new FlowLayout());
        add(b);
        b.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b) {
            int qq=JOptionPane.showConfirmDialog(this, "Are you sure??");
            if(qq == 0)
                System.exit(0);
        }
    }

    public static void main(String[] args) {
        new StudentGUI().setVisible(true);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM