簡體   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