简体   繁体   English

Swing GUI不显示,但没有错误?

[英]Swing gui not displaying, but no errors?

I am working on a java program that solves sudoku puzzles. 我正在研究解决数独难题的Java程序。 So far all I have coded is a class that draws the board with swing, and another class that calls the gui class. 到目前为止,我所编写的只是一个用秋千绘制木板的类,以及另一个称为gui类的类。

When I try and run the program nothing happens. 当我尝试运行该程序时,没有任何反应。 No error messages are shown, but the gui doesn't show either. 没有显示错误消息,但gui也未显示。 It immediately terminates. 立即终止。

Here is my code so far: 到目前为止,这是我的代码:

Gui class: 桂班:

package sudoku;

import java.awt.*;
import javax.swing.*;

public class Gui {

    Gui gui;
    JPanel board;
    JPanel subBoard[][];
    GridLayout layout;
    JLabel square[][];

    public void load() {

        gui = new Gui();
        gui.setUp();
        gui.buildBoard();

    }

    private void setUp() {

        layout = new GridLayout(3, 3);
        board = new JPanel(layout);
        subBoard = new JPanel[3][3];
        square = new JLabel[9][9];

    }

    private void buildBoard() {

        // set up board
        board.setSize(800, 600);
        board.setVisible(true);

        int mod = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {

                // add subBoards to board
                subBoard[i][j] = new JPanel();
                board.add(subBoard[i][j]);
                subBoard[i][j].setLayout(layout);
                subBoard[i][j].setVisible(true);

                // add textfields to each subBoard
                square[i + mod][j + mod] = new JLabel();
                subBoard[i][j].add(square[i + mod][j + mod]);
                square[i + mod][j + mod].setVisible(true);

            }
            mod += 3;

        }
    }

}

main class: 主类:

package sudoku;

public class SudokuSolver {

    public static void main(String[] args) {

        Gui gui = new Gui();
        gui.load();

    }

}

I tried running it in both eclipse and netbeans but got the same result both times. 我尝试在eclipse和netbeans中运行它,但是两次都得到了相同的结果。 Why does this not work? 为什么这不起作用?

There is no displayable window such as a JFrame being used in the application. 应用程序中没有可显示的窗口,例如JFrame

EventQueue.invokeLater(new Runnable() {
   @Override
   public void run() {
      JFrame frame = new JFrame("New GUI");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Gui gui = new Gui();
      gui.load();
      frame.add(gui.getBoard()); // add getBoard getter
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
});

Remove the class member variable gui within Gui . Gui删除类成员变量gui This is shadowing variables in the outer class Gui so use the latter instead. 这在外部类Gui隐藏了变量,因此请改用后者。 Also override getPreferredSize to give the board a size when frame.pack() is invoked. 也覆盖getPreferredSize ,得到board的尺寸时frame.pack()被调用。

Make GUI extend JFrame firstly. 首先使GUI扩展JFrame Then in your main method call gui.setVisible(true); 然后在您的主要方法中调用gui.setVisible(true); .

public class Gui extends JFrame { }

Then in main. 然后在主要。

Gui gui = new Gui();
gui.load();
gui.setVisible(true);

You can not show directly Jpanel. 您不能直接显示Jpanel。 for show gui you must use JFrame or any other Window class(JDialog, JWindow...) and after that set visible property true. 对于show gui,您必须使用JFrame或任何其他Window类(JDialog,JWindow ...),然后将visible属性设置为true。

setVisible(true); setVisible(true);

public class Gui extends JFrame {
       Gui(){
           ...
           setVisible(true);
           setSize(300,400);
        }
        ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM