如何将我的jButtons链接到同一包中其他类的方法

时间:2014-07-17 16:58:48

标签: java swing jframe

实际上我正在使用java进行游戏,当用户点击我在课堂上创建的jButton(新游戏)时,我想开始新游戏' Window&#39 ;在包含我所有游戏组件的同一个包下。现在我想访问方法' newGame()'在我的buttonActionPerformed()方法的类Framework中定义,但没有调用框架的构造函数。

       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)    {                                         
   Framework f = new Framework();
    f.newGame();         
}   

我使用此代码,但由于我使用了

,因此显示了许多错误
          this.setContentPane(new Framework());       
在Window类的构造函数中。这是我的框架类

的一部分
public class Framework extends Canvas {
public Framework ()
{
    super();

    gameState = GameState.VISUALIZING;

    //We start game in new thread.
    Thread gameThread = new Thread() {
        @Override
        public void run(){
            GameLoop();
        }
    };
    gameThread.start();
    }
     private void GameLoop()
     {
           long visualizingTime = 0, lastVisualizingTime = System.nanoTime();

    // This variables are used for calculating the time that defines for how long we should put threat to sleep to meet the GAME_FPS.
    long beginTime, timeTaken, timeLeft;

    while(true)
    {
        beginTime = System.nanoTime();

        switch (gameState)
        {
            case PLAYING:
                gameTime += System.nanoTime() - lastTime;

                game.UpdateGame(gameTime, mousePosition());

                lastTime = System.nanoTime();
            break;
            case GAMEOVER:
                //...
            break;
            case MAIN_MENU:
                //...
            break;
            case OPTIONS:
                //...
            break;
            case GAME_CONTENT_LOADING:
                //...
            break;
            case STARTING:
                // Sets variables and objects.
                Initialize();
                // Load files - images, sounds, ...
                LoadContent();

                // When all things that are called above finished, we change game status to main menu.
                gameState = GameState.MAIN_MENU;
            break;
            case VISUALIZING:
                        if(this.getWidth() > 1 && visualizingTime > secInNanosec)
                {
                    frameWidth = this.getWidth();
                    frameHeight = this.getHeight();

                    // When we get size of frame we change status.
                    gameState = GameState.STARTING;
                }
                else
                {
                    visualizingTime += System.nanoTime() - lastVisualizingTime;
                    lastVisualizingTime = System.nanoTime();
                }
            break;
        }

        // Repaint the screen.
        repaint();               // goes to paint component in canvas

        timeTaken = System.nanoTime() - beginTime;
        timeLeft = (GAME_UPDATE_PERIOD - timeTaken) / milisecInNanosec; 
        if (timeLeft < 10) 
            timeLeft = 10; //set a minimum
        try {
             Thread.sleep(timeLeft);
        } catch (InterruptedException ex) { }
    }
      }
     public void newGame()
      {
      // We set gameTime to zero and lastTime to current time for later calculations.
       if(true)
        {
          BufferedImage blankCursorImg = 
                       new BufferedImage(16, 16,BufferedImage.TYPE_INT_ARGB);
        Cursor blankCursor =       
               Toolkit.getDefaultToolkit().createCustomCursor(blankCursorImg, new Point(0, 0), null);
        this.setCursor(blankCursor);
    }
    gameTime = 0;
    lastTime = System.nanoTime();

    game = new Game();
  } 
}

这里是WindowForm.java,它包含main()和jButtons      包直升机;

 import javax.swing.JFrame;

  public class WindowForm extends javax.swing.JFrame {
  public WindowForm() {
     this.setTitle("Helicopter battle");
     if(true) // Full screen mode
    {  
        this.setUndecorated(true);
         this.setExtendedState(this.MAXIMIZED_BOTH);
    }
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Creates the instance of the Framework.java that extends the Canvas.java and puts it on the frame.
    this.setContentPane(new Framework());
     initComponents();
     jButton1.setBounds(630,490,100,30);
     jButton2.setBounds(630,540,100,30);
      } 
     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    System.exit(0);
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    Framework f = new Framework();
    f.newGame();
      jButton1.setVisible(false);
    jButton2.setVisible(false);
}                          
  public static void main(String args[]) {
  java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new WindowForm().setVisible(true);
        }
    });
}
  public javax.swing.JButton jButton1;
  public javax.swing.JButton jButton2;
  // End of variables declaration                   
 }

请帮助和谢谢。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您想在不newGame()

的情况下创建新对象的情况下调用Framework方法

我认为你可以通过在构造函数之前在WindowForm类中声明一个final变量来实现这一点

private final Framework myFramework = new Framework();

然后将其传递给setContentPane方法

setContentPane(myFramework); 

并在jButton1ActionPerformed方法

中使用相同的对象
myFramework.newGame();

-

另一种解决方案是使用Singleton设计模式从Framework类创建静态实例,并在需要时调用Framework.getInstance()