有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java退出按钮在操作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);
    }
}

共 (2) 个答案

  1. # 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);
        }
    }
    
  2. # 2 楼答案

    你的ActionListener基本上什么都不做,试着在actionPerform方法中移动JOptionPane对话框

    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);
    
                }
            }
        });