Java 使用JButtons设置网格

Java 使用JButtons设置网格,java,swing,graphics,jpanel,Java,Swing,Graphics,Jpanel,我必须为一个项目填写代码。我已经为第一段代码键入了我认为正确的内容,但我仍然感到困惑,因为它没有弹出正确的图形 NestedLayoutPractice课程 /* Practice using layouts Anderson, Franceschi */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class NestedLayoutPractice extends JFr

我必须为一个项目填写代码。我已经为第一段代码键入了我认为正确的内容,但我仍然感到困惑,因为它没有弹出正确的图形

NestedLayoutPractice课程

   /*  Practice using layouts
    Anderson, Franceschi
*/

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

public class NestedLayoutPractice extends JFrame
{
 private Container contents;
 private Game game;

 private BorderLayout bl;

 private JLabel bottom;

 // ***** Task 1: declare a JPanel named top
 // also declare three JButton instance variables
 // that will be added to the JPanel top
 // these buttons will determine the grid size of the game:
 //  3-by-3, 4-by-4, or 5-by-5
private JPanel top;
private JButton [] topButtons;
private String [] topGridSize = 
{"3-by-3",
 "4-by-4",
 "5-by-5",      
};
private JButton reset;








 // task 1 ends here

 public NestedLayoutPractice( )
 {
  super( "Practicing layout managers" );
  contents = getContentPane( );

  // ***** Task 2: student code starts here
  // instantiate the BorderLayout manager bl


  // set the layout manager of the content pane contents to bl


  game = new Game( 3 ); // instantiating the GamePanel object
  // add panel to the center of the content pane


  // task 2 ends here

  bottom = new JLabel( "Have fun playing this Tile Puzzle game",
                        SwingConstants.CENTER );

  // ***** Task 3: Student code restarts here
  // instantiate the JPanel component named top


  // set the layout of top to a 1-by-3 grid


  // instantiate the JButtons that determine the grid size




  // add the buttons to JPanel top





  // add JPanel top to the content pane as its north component


  // task 3 ends here

  // ***** Task 5: Student code restarts here
  // Note: search for and complete Task 4 before performing this task
  // declare and instantiate an ActionListener


  // register the listener on the 3 buttons
  // that you declared in Task 1




  // task 5 ends here

  contents.add( bottom, BorderLayout.SOUTH );

  setSize( 325, 325 );
  setVisible( true );
 }

 // ***** Task 4: Student code restarts here
 // create a private inner class that implements ActionListener
 // your method should identify which of the 3 buttons
 //    was the source of the event
 // depending on which button was pressed,
 //    call the setUpGame method of the Game class
 //    with arguments 3, 4, or 5
 // the API of that method is:
 //   public void setUpGame( int nSides )










 // task 4 ends here

 public static void main( String [] args )
 {
  NestedLayoutPractice nl = new NestedLayoutPractice( );
  nl.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 }
}
TilePuzzle类

/* TilePUzzle class
    Anderson, Franceschi
 */

 public class TilePuzzle
 {
  private int side; // grid size for game 1
  private String[][] tiles;
  private int emptyRow;
  private int emptyCol;

  public TilePuzzle( int newSide )
  {
   setUpGame( newSide );
  }

  public void setUpGame( int newSide )
  {
   if ( side > 0 )
     side = newSide;
   else
     side = 3;
   side = newSide;
   tiles = new String[side][side];
   emptyRow = side - 1;
   emptyCol = side - 1;

   for ( int i = 0; i < side; i++ )
   {
    for ( int j = 0; j < side; j++ )
    {
     tiles[i][j] = String.valueOf( ( side * side )
                             - ( side * i + j + 1 ) );
    }
   }
   // set empty tile to blank
   tiles[side - 1][side - 1] = "";
  }

  public int getSide( )
  {
    return side;
  }
/*
  public int getEmptyRow( )
  {
    return emptyRow;
  }

  public int getEmptyCol( )
  {
    return emptyCol;
  }
*/
  public String[][] getTiles( )
  {
    return tiles;
  }

  public boolean tryToPlay( int row, int col )
  {
   if ( possibleToPlay( row, col ) )
   {
    // play: switch empty String and tile label at row, col
    tiles[emptyRow][emptyCol] = tiles[row][col];
    tiles[row][col] = "";

    emptyRow = row;
    emptyCol = col;
    return true;
   }
   else
    return false;
  }

  public boolean possibleToPlay( int row, int col )
  {
   if ( ( col == emptyCol && Math.abs( row - emptyRow ) == 1 )
          || ( row == emptyRow && Math.abs( col - emptyCol ) == 1 ) )
     return true;
   else
     return false;
  }

  public boolean won( )
  {
   for ( int i = 0; i < side ; i++ )
   {
    for ( int j = 0; j < side; j++ )
    {
      if ( !( tiles[i][j].equals(
                        String.valueOf( i * side + j + 1 ) ) )
                        && ( i != side - 1 || j != side - 1 ) )
        return false;
    }
   }
   return true;
  }

 }
/*TilePUzzle类
安德森,弗朗西斯科
*/
公共类TilePuzzle
{
private int side;//游戏1的网格大小
私有字符串[][]瓷砖;
私人int emptyRow;
私有int emptyCol;
公共TilePuzzle(intnewside)
{
设置游戏(newSide);
}
公共void设置游戏(int newSide)
{
如果(侧>0)
边=新闻边;
其他的
侧面=3;
边=新闻边;
tiles=新字符串[side][side];
emptyRow=侧面-1;
emptyCol=侧-1;
对于(int i=0;i
游戏课

/* Using GridLayout to organize our window
   Anderson, Franceschi
*/

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

public class Game extends JPanel
{
 private JButton [][] squares;
 private TilePuzzle game;

 public Game( int newSide )
 {
  game = new TilePuzzle( newSide );
  setUpGameGUI( );
 }

 public void setUpGame( int newSide )
 {
  game.setUpGame( newSide );
  setUpGameGUI( );
 }

 public void setUpGameGUI( )
 {
   removeAll( ); // remove all components
   setLayout( new GridLayout( game.getSide( ),
              game.getSide( ) ) );

   squares = new JButton[game.getSide( )][game.getSide( )];

   ButtonHandler bh = new ButtonHandler( );

   // for each button: generate button label,
   // instantiate button, add to container,
   // and register listener
   for ( int i = 0; i < game.getSide( ); i++ )
   {
    for ( int j = 0; j < game.getSide( ); j++ )
    {
     squares[i][j] = new JButton( game.getTiles( )[i][j] );
     add( squares[i][j] );
     squares[i][j].addActionListener( bh );
    }
   }

   setSize( 300, 300 );
   setVisible( true );
 }

 private void update( int row, int col )
 {
   for ( int i = 0; i < game.getSide( ); i++ )
   {
    for ( int j = 0; j < game.getSide( ); j++ )
    {
     squares[i][j].setText( game.getTiles( )[i][j] );
    }
   }

   if ( game.won( ) )
   {
    JOptionPane.showMessageDialog( Game.this,
              "Congratulations! You won!\nSetting up new game" );
    // int sideOfPuzzle = 3 + (int) ( 4 * Math.random( ) );
    // setUpGameGUI( );
   }
 }

 private class ButtonHandler implements ActionListener
 {
  public void actionPerformed( ActionEvent ae )
  {
   for( int i = 0; i < game.getSide( ); i++ )
   {
    for( int j = 0; j < game.getSide( ); j++ )
    {
     if ( ae.getSource( ) == squares[i][j] )
     {
       if ( game.tryToPlay( i, j ) )
          update( i, j );
       return;
     } // end if 
    } // end inner for loop
   } // outer for loop
  } // end actionPerformed method
 } // end ButtonHandler class
} // end Game class
/*使用GridLayout组织窗口
安德森,弗朗西斯科
*/
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类游戏扩展JPanel
{
私人JButton[][]正方形;
私人TilePuzzle游戏;
公共游戏(int newSide)
{
游戏=新的TilePuzzle(newSide);
setUpGameGUI();
}
公共void设置游戏(int newSide)
{
游戏设置游戏(newSide);
setUpGameGUI();
}
public void setUpGameGUI()
{
removeAll();//删除所有组件
setLayout(新的GridLayout(game.getSide()),
game.getSide());
squares=newjbutton[game.getSide()][game.getSide()];
ButtonHandler bh=新的ButtonHandler();
//对于每个按钮:生成按钮标签,
//实例化按钮,添加到容器,
//和注册侦听器
for(inti=0;i
您是否尝试过将
游戏添加到框架中?不,您将如何操作?返回您的课程笔记,看一看,好的,就可以了,谢谢!