Java Can';当用户点击(X)时,无法关闭Jframe

Java Can';当用户点击(X)时,无法关闭Jframe,java,Java,当用户点击(X)按钮时,我无法关闭Jframe。我尝试了很多方法,但都不管用 我试过: JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 什么也没发生。我的主类扩展了jframe并实现了action listener。我做错了什么 我的代码: package com.xflare.Bot; import static java.lang.System.out; import java.awt.*; import java.lang.

当用户点击(X)按钮时,我无法关闭Jframe。我尝试了很多方法,但都不管用

我试过:

JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
什么也没发生。我的主类扩展了jframe并实现了action listener。我做错了什么

我的代码:

package com.xflare.Bot;

import static java.lang.System.out;
import java.awt.*;
import java.lang.String;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame implements ActionListener{

    private static Frame frame;

    private static boolean debug = true;

    private static boolean enabled = true;

    private static JButton exitbutton; // reference to the button object

    private static JButton webbutton; // reference to the button object

    private static JButton aboutbutton; // reference to the button object

    public static void main(String[] args) {
        new Main().start();
    }

    private void start(){
        //start up
        printSystem("starting");

        //create frame
        printSystem("Creating a frame...");
        createFrame();

        //create button(s)
        createQuitButton();
        createWebButton();
        createAboutButton();

        //Spawn init.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getFrame().setResizable(false);
        getFrame().setVisible(true);
        printSystem("Started up successfully!");
    }

    private void createFrame(){
        Main.frame = new Frame("app");
        frame.setSize(600, 300);
    }

    private void createQuitButton(){
        exitbutton = new JButton("Exit!");
        getFrame().setLayout(null);
        exitbutton.setBounds(225,45,150,75);//setBounds(x,y,width,height)
        exitbutton.setActionCommand("exit");
        exitbutton.addActionListener(this);
        getFrame().add(exitbutton);
    }

    private void createWebButton(){
        webbutton = new JButton("Open hacked browser");
        getFrame().setLayout(null);
        webbutton.setBounds(225,130,150,75);//setBounds(x,y,width,height)
        webbutton.setActionCommand("web");
        webbutton.addActionListener(this);
        getFrame().add(webbutton);
    }

    private void createAboutButton(){
        aboutbutton = new JButton("About");
        getFrame().setLayout(null);
        aboutbutton.setBounds(225,215,150,75);//setBounds(x,y,width,height)
        aboutbutton.setActionCommand("about");
        aboutbutton.addActionListener(this);
        getFrame().add(aboutbutton);
    }

    private Frame getFrame(){
        return Main.frame;
    }

    public void actionPerformed(ActionEvent e) {
        String actionCommand = ((JButton) e.getSource()).getActionCommand();
        printDebug("Button " + actionCommand + " was pressed.");
        if(actionCommand.equals("exit")){
            exitbutton.setVisible(false);
            shutdown();
        }
        else if(actionCommand.equals("about")){
            aboutbutton.setVisible(false);
            webbutton.setVisible(false);
            exitbutton.setVisible(false);
            showAbout();
        }
        else{
            printCritical("Unknown button pressed!");
        }
    }

    private void showAbout(){

    }

    private void shutdown(){
        printSystem("Attempting to shut down...");
        enabled = false;
        printSystem("Shut down successful!");
        System.exit(0);
    }

    private boolean debugEnabled(){
        return debug;
    }

    private String getVersion(){
        return "1.0.0";
    }

    private String getCodename(){
        return "[BeastReleased]";
    }

    private static void printSystem(String var){
        out.println("System> " + var);
    }

    private static void printError(String var){
        out.println("Error> " + var);
    }

    private static void printCritical(String var){
        out.println("Critical> " + var);
    }

    private void printDebug(String var){
        if(debugEnabled()) {
            out.println("Debug> " + var);
        }
    }

}

链接到同一代码:

此:
setDefaultCloseOperation(JFrame.EXIT\u ON\u CLOSE)

需要在实际显示的JFrame上调用Main.frame。你不会这么做的

getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getFrame().setVisible(true);
你的帧/帧太多了。您的类扩展了JFrame,但您没有显示它。您还拥有一个名为
Frame
Frame
变量(不是一个JFrame变量),您正在显示该变量,当然,因为这不是一个JFrame,所以您不能像上面那样调用JFrame方法

简化:创建ONEJFrame非框架,并对其调用此方法并将其设置为可见。因此,要么去掉框架变量并使用类本身,即
this
,作为JFrame并显示它,要么不让类扩展JFrame并使用框架变量,而是使其成为JFrame对象而不是框架对象,因为frame没有
setDefaultCloseOperation(…)
方法

另外,在不应该使用静态修改器的地方,您也过度使用了它们。所有字段都应该是实例(非静态)字段

此外,使用空布局和收进框最终也会让你吃亏。例如,当我运行您的程序时,中间按钮的部分文本丢失,因为它的大小被人为地以一种不好的方式限制。更好的是我们的布局经理对您的优势。例如:

请看一下这个程序结构:

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class MyMain extends JPanel {
    public static final String MENU_PANEL = "MENU";
    public static final String ABOUT_PANEL = "About";
    private CardLayout cardLayout = new CardLayout();

    public MyMain() {
        JPanel aboutPanel = new JPanel(new GridBagLayout());
        JLabel aboutLabel = new JLabel("About");
        aboutLabel.setFont(aboutLabel.getFont().deriveFont(Font.BOLD, 32));
        aboutPanel.add(aboutLabel);

        JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 10, 10));
        buttonPanel.add(createButton(new ExitAction("Exit!", KeyEvent.VK_X)));
        buttonPanel.add(createButton(new OpenBrowserAction("Open Hacked Browser", KeyEvent.VK_O)));
        buttonPanel.add(createButton(new AboutAction("About", KeyEvent.VK_A, this)));        

        JPanel menuPanel = new JPanel(new GridBagLayout());
        int ebGap = 40;
        menuPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
        menuPanel.add(buttonPanel);

        setLayout(cardLayout);
        add(menuPanel, MENU_PANEL);
        add(aboutPanel, ABOUT_PANEL);
    }

    private JButton createButton(Action action) {
        JButton button = new JButton(action);
        Font btnFont = button.getFont().deriveFont(Font.BOLD, 20);
        button.setFont(btnFont);
        return button;
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("My Main Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyMain());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    public void showPanel(String cardLayoutKey) {
        cardLayout.show(this, cardLayoutKey);
    }
}


您在错误的位置调用setDefaultCloseOperation方法。以下是您应该做的:

private void start(){
    //start up
    printSystem("starting");

    //create frame
    printSystem("Creating a frame...");
    createFrame();

    //Spawn init.
    getFrame().setResizable(false);
    getFrame().setVisible(true);
    printSystem("Started up successfully!");
}

private void createFrame(){
    Main.frame = new Frame("app");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 300);
}

你有其他的关键代码,你没有告诉我们,这很可能是一个极客的方式。您需要创建并发布一个有效的、短的程序,该程序足够短,可以作为代码格式的文本发布在此处,用于编译并演示您的问题。在调用setVisible()之前,请尝试设置默认的关闭操作。什么是X按钮?你的JButton代码?框架应用程序上的关闭图标?请发布一个MCVE!我尝试了,未解析的符号“setDefaultCloseOperation”。我尝试了,未解析的符号“setDefaultCloseOperation”。@javanumero:请再次创建并发布您的。你让我们猜,这一点都不好玩。@javanumero:你的代码乱七八糟。请参见编辑以回答问题。您只需要一个顶层窗口,而不是两个。要么不让类扩展JFrame并使用frame变量(但作为JFrame变量而不是frame变量),要么去掉frame变量并使用类本身。
class OpenBrowserAction extends AbstractAction {
    public OpenBrowserAction(String name, int mnemonic) {
        super(name);
        putValue(MNEMONIC_KEY, mnemonic);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Open Browswer");
    }
}
class AboutAction extends AbstractAction {
    private MyMain myMain;

    public AboutAction(String name, int mnemonic, MyMain myMain) {
        super(name);
        putValue(MNEMONIC_KEY, mnemonic);
        this.myMain = myMain;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (myMain != null) {
            myMain.showPanel(MyMain.ABOUT_PANEL);
        }
    }
}
private void start(){
    //start up
    printSystem("starting");

    //create frame
    printSystem("Creating a frame...");
    createFrame();

    //Spawn init.
    getFrame().setResizable(false);
    getFrame().setVisible(true);
    printSystem("Started up successfully!");
}

private void createFrame(){
    Main.frame = new Frame("app");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 300);
}