Java GridLayout在应显示时未显示在框架上

Java GridLayout在应显示时未显示在框架上,java,swing,function,button,layout,Java,Swing,Function,Button,Layout,现在它只显示带有“solve”按钮、文本字段和JLabel的框架。当我在文本字段中按带有数字的“solve”时,它应该遍历NQueens模型,计算出有多少个解决方案,并使用网格布局在屏幕上显示一个解决方案。但当我单击“求解”时,实际上什么都没有发生。我已经尝试调用视图的repaint()和revalidate()方法,但这些方法似乎不起作用 “解决”按钮使用反射转到控制器以查看要执行的操作。有人知道发生了什么事吗?这是MVC风格 控制器类: package model; public clas

现在它只显示带有“solve”按钮、文本字段和JLabel的框架。当我在文本字段中按带有数字的“solve”时,它应该遍历NQueens模型,计算出有多少个解决方案,并使用网格布局在屏幕上显示一个解决方案。但当我单击“求解”时,实际上什么都没有发生。我已经尝试调用视图的repaint()和revalidate()方法,但这些方法似乎不起作用

“解决”按钮使用反射转到控制器以查看要执行的操作。有人知道发生了什么事吗?这是MVC风格

控制器类:

package model;

public class Controller {
    private View myView;
    private NQueensModel myModel;
    private int int1, possibilities;
    public Controller()
    {
        myView = new View(this);

    }
    public void solve()
    {
        int1 = myView.getEntryInt();
        myView.doViewGrid();
        myModel = new NQueensModel(int1);
        myModel.solvePuzzle();
        possibilities = myModel.getPossibilities();
        myView.addButtons();
        myView.setPossibilitiesLabel(possibilities);
        myView.revalidate();
        myView.repaint();




    }

    public boolean[][] getMyBoard()
    {
        return myModel.getBoard();
    }


    /*public static void main(String[] args)
    {
        Controller controller = new Controller();
    }*/

}
视图类

package model;
import java.awt.*;

import javax.swing.*;

import model.ButtonListener;
import model.Controller;



import java.lang.reflect.Method;

@SuppressWarnings("serial")
public class View extends JFrame {
    private static final int FRAME_WIDTH = 600;
    private static final int FRAME_HEIGHT = 600;
    private static final int FRAME_X_ORIGIN = 250;
    private static final int FRAME_Y_ORIGIN = 250;

    private JButton solveButton;
    JPanel gridPanel;
    private JTextField text;
    private String entryNum;
    private int entryInt;
    private ButtonListener mySolveListener;
    private boolean[][] myBoard;
    private JButton buttons, emptyButtons;
    private JLabel possibilitiesLabel;
    public static void main(String[] args){

    }
    private static Controller myController;
    public View(Controller controller)
    {
        JPanel gridPanel;
        gridPanel = new JPanel();
        gridPanel.setBounds(100,100,400,400);
        gridPanel.setLayout(new GridLayout(4,4));
        this.add(gridPanel);
        this.setTitle("NQueens");
        myController = controller;

        this.setLayout(null);
        this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
//      this.associateListeners();
        Container contentPane;
//      this.add(contentPane);
        this.setTitle("NQueens");

//      this.addWindowListener(new AWindowListener());
        this.setResizable(false);
        this.setBackground(Color.WHITE);


        solveButton = new JButton("Solve");
        solveButton.setBounds(500,500,80,80);
        this.add(solveButton);

        text = new JTextField();
        text.setBounds(400,500,80,80);
        this.add(text);

        possibilitiesLabel = new JLabel("Possibilities: ");
        possibilitiesLabel.setBounds(240,500,80,80);
        this.add(possibilitiesLabel);


        this.setVisible(true);

    }

    public void setPossibilitiesLabel(int x)
    {
        possibilitiesLabel.setText("Possiblities: " + x);
    }
    public void doInt(){
        entryNum = text.getText();
        entryInt = Integer.parseInt(entryNum);
    }
    public int getEntryInt(){
        return entryInt;
    }
    public void addButtons()
    {
        myBoard = myController.getMyBoard();
        for (int i = 0; i < this.getEntryInt(); i++)
        {
            for(int j = 0 ; j < this.getEntryInt(); j++)
            {


                if(myBoard[i][j]==true)
                {
                    buttons = new JButton("Q");
                    gridPanel.add(buttons);
                    this.setVisible(true);
                }
                else
                {
                    emptyButtons = new JButton(" ");
                    gridPanel.add(emptyButtons);
                    this.setVisible(true);
                }
            }
        }
    }
    public void doViewGrid(){
        gridPanel.setLayout(new GridLayout(this.getEntryInt(),this.getEntryInt()));
    }
      /**
     * Associates each component's listener with the controller
     * and the correct method to invoke when triggered.
     *
     * <pre>
     * pre:  the controller class has be instantiated
     * post: all listeners have been associated to the controller
     *       and the method it must invoke
     * </pre>
     */
    public void associateListeners()
    {
        String error;
        Class<? extends Controller> controllerClass;
        Method solveMethod,
               decrementMethod;
        Class<?>[] classArgs;
        Integer[] args;

        controllerClass = myController.getClass();

        error = null;
        solveMethod = null;
        decrementMethod = null;

        classArgs = new Class[1];
        args = new Integer[1];

        // Set argument types for method invokations
        try
        {
           classArgs[0] = Class.forName("java.lang.Integer");
        }
        catch(ClassNotFoundException exception)
        {
           error = exception.toString();
           System.out.println(error);
        }

        // Associate method names with actual methods to be invoked
        try
        {
           solveMethod = controllerClass.getMethod("solve",classArgs);
           decrementMethod = controllerClass.getMethod("decrement",classArgs);        
        }

        catch(NoSuchMethodException exception)
        {
           error = exception.toString();
           System.out.println(error);
        }
        catch(SecurityException exception)
        {
           error = exception.toString();
           System.out.println(error);
        }

        // Set up listeners with actual argument values passed in
        // to methods

        args[0] = new Integer(200);
        mySolveListener 
               = new ButtonListener(myController, solveMethod, args);
//        myDecrementListener 
//               = new ButtonListener(myController, decrementMethod, args);
    }

}
包模型;
导入java.awt.*;
导入javax.swing.*;
导入model.ButtonListener;
导入模型控制器;
导入java.lang.reflect.Method;
@抑制警告(“串行”)
公共类视图扩展了JFrame{
专用静态最终整数帧_宽度=600;
专用静态最终整型框架高度=600;
私有静态最终整数帧×原点=250;
私有静态最终整数帧_Y_原点=250;
私有JButton按钮;
JPanel网格面板;
私有JTextField文本;
私有字符串entryNum;
私人入境;
私有按钮列表器mysolvelister;
私有布尔[][]myBoard;
专用按钮、清空按钮;
私人JLabel可能性标签;
公共静态void main(字符串[]args){
}
专用静态控制器;
公共视图(控制器)
{
JPanel网格面板;
gridPanel=newJPanel();
网格面板.立根(100400400);
setLayout(新的GridLayout(4,4));
添加(gridPanel);
本条。设定标题(“NQueens”);
myController=控制器;
此.setLayout(null);
此.setSize(框宽、框高);
//this.associateListeners();
容器内容窗格;
//添加(contentPane);
本条。设定标题(“NQueens”);
//this.addWindowListener(新的AWindowListener());
此参数为.setresizeable(false);
这个.背景(颜色.白色);
solveButton=新的JButton(“解决”);
立根(500500,80,80);
这个.添加(按钮);
text=新的JTextField();
文本.立根(400500,80,80);
增加(文本);
可能性标签=新的JLabel(“可能性:”);
可能性标签.setBounds(240500,80,80);
添加(可能性标签);
此.setVisible(true);
}
公共无效集合可能性标签(int x)
{
可能性标签.setText(“可能性:+x”);
}
公屋{
entryNum=text.getText();
entryInt=Integer.parseInt(entryNum);
}
public int getEntryInt(){
返回入口;
}
公共无效添加按钮()
{
myBoard=myController.getMyBoard();
for(int i=0;i
模型

包模型;
公共类模型
{
密努姆斯奎因私人酒店;
公共int=0;
私有布尔[][]myBoard;
//私有静态NQueensModel myModel=新的NQueensModel(5);
公共静态void main(字符串[]args){
//System.out.println(myModel.solvePuzzle());
//System.out.println(myModel.myprobabilities);
//System.out.println(myModel.doIt(myModel.myprobabilities));//您想要这个吗
//System.out.println(我的可能性);
}
公共NQueensModel(intnqueens)
{
mynumsquen=nq
package model;

public class NQueensModel
{
    private int myNumsQueen;
    public int myPossibilities=0;
    private boolean[][] myBoard;
  //  private static NQueensModel myModel = new NQueensModel(5);

    public static void main (String[] args) {

//        System.out.println(myModel.solvePuzzle());
//        System.out.println(myModel.myPossibilities);
//        System.out.println(myModel.doIt(myModel.myPossibilities)); //you want this




       // System.out.println(myPossibilities);


    }
    public NQueensModel(int nQueens)
    {
        myNumsQueen = nQueens;
        myPossibilities=0;
        myBoard = new boolean[myNumsQueen][myNumsQueen];
    }
    public boolean solvePuzzle()
    {
//      return this.doIt(myModel.myPossibilities);
        return solvePuzzle(0);

    }
    private boolean solvePuzzle(int ncolumn)
    {
        if(ncolumn>myNumsQueen-1)
        {
            myPossibilities++;
//            return true;          
        }
        int i;       
        for( i =0; i<myNumsQueen;i++)
        {           
            if(this.isSafeMove(i, ncolumn)==true)
            {                
                this.placeQueen(i,ncolumn);
                if(this.solvePuzzle(ncolumn+1)==true)
                {   
                    return true;                 
                }
                this.removeQueen(i, ncolumn);
            }     

        }      
//        else if(myPossibilities>0)
//        {
//          return true;
//        }

        return false;

    }

    private boolean doIt(int county)
    {
        if(county>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
//    private int solveCount(int count, int col)
//    {
//      for(int j = 0 ; j<col; j++){
//          if(myModel.solvePuzzle(j)==true){
//               count++;
//          }
//          else if(myModel.solvePuzzle(j)==false){
//              return count;
//          }
//          else
//          {
//              for(int i =0 ; i<col;i++){
//                  count = solveCount(count, col+1);
//              }
//              return count;
//          }
//          return count;
//      }
//      return count;
//      
//    }
    private boolean isSafeMove(int row, int col)
    {
        if(row <0 || row>=myNumsQueen || col<0 || col>=myNumsQueen)
        {
            return false;
        }
        else if(this.checkLowerDiag(row, col)==true ||this.checkUpperDiag(row, col)==true ||this.checkLeft(row,col)==true)
         {
             return false;
         }
        else
        {

            return true;
        }


    }
    private boolean checkUpperDiag(int row, int col)
    {    
        if(row==0)
        {
            return false;
        }
        else
        {
            for(int i=row, j = col; i>=0 && j>=0; i--, j--)
            {
                if(myBoard[i][j]==true)
                {
                    return true;
                }
            }
            return false;
        }
    }
    private boolean checkLowerDiag(int row, int col)
    {

        if(col==0 )
        {           
            return false;             
        }
        if(row==myNumsQueen-1){
            return false;
        }
        else
        {

            for(int i = row, j = col; i<myNumsQueen && j>=0;  i++, j--)
            {   
//              System.out.println("error" + i + " " + j );
                if(j>=myNumsQueen)
                {
                    return false;
                }

                else if(myBoard[i][j]==true)
                {
                    return true;

                }


            }
            return false;
        }
    }
    private boolean checkLeft(int row, int col)
    {
        if(col==0)
        {
            return false;
        }
        else 
        {
            for(int i = col; i>=0; i--)
            {

                if(i>=myNumsQueen)
                {
                    return false;
                }

                else if(myBoard[row][i]==true)
                {
                    return true;
                }
            }
            return false;
        }

    }
    private boolean placeQueen(int row, int col)
    {
        if(col>=myNumsQueen)
        {
            return false;
        }

        myBoard[row][col] = true;
        return true;
    }
    private boolean removeQueen(int row, int col)
    {
        myBoard[row][col] = false;
        return false;
    }
    public int getPossibilities(){
        return this.myPossibilities;
    }
    public boolean[][] getBoard()
    {
        return myBoard;
    }
//    public String toString()
//    {
//        
//    }
}
solveButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        myController.solve(200);
    }
});