⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridlayoutdemo.java

📁 这是一个简单的八数码界面
💻 JAVA
字号:
// Fig. 13.26: GridLayoutDemo.java
// Demonstrating GridLayout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GridLayoutDemo extends JFrame implements ActionListener,KeyListener{
   private JButton buttons[];
   private final String names[] = 
      { "1", "2", "3", "4", "5", "6","7","8","" };
   private boolean toggle = true;
   private Container container;
   private GridLayout grid1;
   final static int CANNOT_MOVE=-1;

   // set up GUI
   public GridLayoutDemo()
   {
      super( "GridLayout Demo" );

      // set up layouts
      grid1 = new GridLayout( 3, 3, 5, 5 );
      

      // get content pane and set its layout
      container = getContentPane();
      container.setLayout( grid1 );

      // create and add buttons
      buttons = new JButton[ names.length ];

      for ( int count = 0; count < names.length; count++ ) {
         buttons[ count ] = new JButton( names[ count ] );
         buttons[ count ].addActionListener( this );
         container.add( buttons[ count ] );
         
         addKeyListener(this);
      }

      setSize( 400, 400 );
      setVisible( true );

   } // end constructor GridLayoutDemo

   // handle button events by toggling between layouts
   public void actionPerformed( ActionEvent event )
   { 
      int currentIndex=0;
      for(int i= 0;i<names.length;i++) 
      if(buttons[i]==event.getSource())
      { 
      currentIndex =i; 
      break; }
      int moveToIndex=moveTo(currentIndex); 
      if(moveToIndex!=CANNOT_MOVE){ 
      buttons[moveToIndex].setText( " " + names[currentIndex]); 
      buttons[currentIndex].setText( " "); 
      buttons[currentIndex].setEnabled(true); 

      String temp = names[currentIndex]; 
      names[currentIndex] = names[moveToIndex]; 
      names[moveToIndex] = temp;} 
      
      container.setLayout( grid1 );

      
   }
   
   public void keyPressed(KeyEvent event)
   {
   	int currentIndex=0;
   	if(event.getKeyCode()==KeyEvent.VK_UP)
   	{for(int i= 0;i<names.length;i++) 
   	if(buttons[i]==event.getSource())
   	{
   	  	currentIndex =i; 
   	  	break;
   	 }
   	  	
   	  int moveToIndex=moveTo(currentIndex); 
      if(moveToIndex!=CANNOT_MOVE){ 
      buttons[moveToIndex].setText( " " + names[currentIndex]); 
      buttons[currentIndex].setText( " "); 
      buttons[currentIndex].setEnabled(true); 

      String temp = names[currentIndex]; 
      names[currentIndex] = names[moveToIndex]; 
      names[moveToIndex] = temp;	
    }}
    
   	container.setLayout( grid1 );
    }
    
    public void keyTyped(KeyEvent event) 
    { 
    } 

    public void keyReleased(KeyEvent event) 
    { 
    } 

   
   
      
      private int moveTo(int i)
      { 
      if(i+1<names.length &&names[i+1]=="")   
      return i+1;
      if(i-1>=0&&names[i-1]=="")   
      return i-1; 
      int n=(int)Math.sqrt(names.length); 
      if(i+n<names.length&&names[i+n]=="")   
      return i+n; 
      if(i-n>=0&&names[i-n]=="") 
      return i-n; 
      return CANNOT_MOVE; 
      } 

   public static void main( String args[] )
   {
      GridLayoutDemo application = new GridLayoutDemo();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   } 

} // end class GridLayoutDemo

/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and               *
 * Prentice Hall. All Rights Reserved.                                    *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -