ActionListener后退出按钮不起作用

时间:2014-06-15 09:05:46

标签: java swing actionlistener

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 个答案:

答案 0 :(得分:2)

您的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);

            }
        }
    });

答案 1 :(得分:2)

检查此示例代码

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