2016-11-19 69 views
2

當用戶點擊(X)按鈕時,無法讓我的Jframe關閉。我嘗試了很多方法來做到這一點,但都沒有成功。用戶點擊時無法關閉Jframe(X)

我想:

JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

什麼也沒有發生。我的主類擴展了jframe並實現了動作監聽器。我究竟做錯了什麼?

我的代碼:

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

} 

鏈接到同一代碼:http://pastebin.com/1fDbjm74

+1

你有其他的關鍵代碼,你沒有向我們展示,這可能是geetting的方式。您需要創建併發佈一個有效的[mcve],一個簡短的程序,足夠短,可以在此發佈爲代碼格式的文本,編譯並顯示您的問題。 –

+0

編輯帖子。 – javanumero

+0

嘗試在調用setVisible()之前設置默認關閉操作() –

回答

1

此:需求setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

上,你實際上顯示,Main.frame JFrame中被調用。你沒有這樣做。

getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
getFrame().setVisible(true); 

你有太多的幀/ JFrames。你的類擴展了JFrame,但你沒有顯示它。你也有Frame變量frame不是一個JFrame變量),你顯示,當然,因爲這不是一個JFrame,你不能使JFrame方法調用,就像上面的一個它。

簡化:創建ONE JFrame不是框架,並調用此方法並將其設置爲可見。因此,要麼擺脫框架變量,並使用類本身,this作爲您的JFrame,並顯示它,或者沒有您的類擴展JFrame並使用您的框架變量,但使其成爲JFrame對象而不是 a框架對象,因爲框架沒有setDefaultCloseOperation(...)方法。

此外,你是過度使用靜態修飾符,他們不應該被使用。所有的字段都應該是實例(非靜態)字段。

此外,使用null佈局和setBounds會最終咬你。例如,當我運行你的程序時,中間按鈕的文本部分缺失,因爲它的大小被人爲地限制在一個不好的方面。更好的是我們佈局經理對你有利。例如:....

請看看這個程序結構:

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

class ExitAction extends AbstractAction { 
    public ExitAction(String name, int mnemonic) { 
     super(name); 
     putValue(MNEMONIC_KEY, mnemonic); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Component comp = (Component) e.getSource(); 
     if (comp != null) { 
      Window win = SwingUtilities.getWindowAncestor(comp); 
      if (win != null) { 
       win.dispose(); 
      } 
     } 
    } 
} 

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

我試了一下,未解決的符號「setDefaultCloseOperation」。 – javanumero

+1

@javanumero:**再次**請創建併發布您的[mcve]。你讓我們猜測,這沒有趣。 –

+0

完成。我編輯了這篇文章。 – javanumero

1

您正在調用錯誤位置的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); 
} 
+0

我試了一下,未解決的符號「setDefaultCloseOperation」。 – javanumero