调用构造函数不会创建JFrame(Swing)

时间:2015-06-16 12:04:02

标签: java constructor jframe

我正在尝试优化我的图形MineSweeper;我想要做的是创建一个即时启动游戏的JFrame,但是当我调用构造函数来生成它时,它只显示窗口:(

我被迫创建一个JFrame,我必须在其中点击才能创建JFrame:这里有一些代码。

启动器(主要方法):

import javax.swing.*;

public class Launcher {
    public static void main(String[] args) {

    JFrame frame = new JFrame("MineSweeper");
    Menu menu = new Menu(frame, 11, 11, 10);
    frame.setJMenuBar(menu.operation());

    frame.setSize(300,100);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.add(new JLabel("MineSweeper 0.9 by ME ;)",SwingConstants.CENTER));

    // new MineSweeper(11,11,10);   <---- not working alone
    }
}

MineSweeper(代码的其余部分):

public class MineSweeper extends MouseAdapter implements ActionListener {
    /* useless instance variables */
private JFrame frame;
private JPanel gridPanel, optionsPanel;
private int[][] gridWithNumbers;
    private JToggleButton[][] gridWithGraphics;
/* useless instance variables */

public MineSweeper(int maxRows, int maxCols, int maxMines) {
    this.maxRows = maxRows;
    this.maxCols = maxCols;
    this.maxMines = maxMines;


    gridWithNumbers = new int[maxRows][maxCols];
    gridWithGraphics = new JToggleButton[maxRows][maxCols]; 


    gridPanel = new JPanel();
    //  gridPanel.setLayout(new GridLayout(maxRows-2,maxCols-2,1,1)); <--- nevermind this

    frame = new JFrame("MineSweeper");
    //frame.setSize( (int)(21 * maxCols) - 11 , (int)(21.5 * maxRows) + 83 );         <---  nice numbers, nevermind 
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

/* ...... */

0 个答案:

没有答案