Java.awt.event类管理

时间:2015-04-10 16:14:16

标签: java swing

我开始在一个类中用Java编写游戏(也在线编写代码部分),现在我试图将其分成多个类,但是awt.event给了我一些问题。 我希望类MainGUI实现awt.event类,但是keyPressed和actionPerformed方法只能包含对另一个类中的方法的调用,该类包含与事件发生相对应的代码。

我这样做是因为我希望类mainGUI只实现游戏窗口,将控件管理留给另一个类。

我尝试传输代码并实现调用方法,除了方法requestFocus之外,一切正常。 当我从用于控件的类中间接调用它时,要求他将焦点移动到游戏窗口时,它不起作用。

所以,我要破解的类是MainGUI,而我想要管理控件的类是Logic:

public class MainGUI extends JFrame implements ComponentListener, ActionListener, FocusListener, KeyListener {

//STATIC ATTRIBUTES

private final static int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3, STOP = 4; 
private final static int NUMBER_OF_ROWS = 70;
private final static int NUMBER_OF_COLUMNS = 70; 
private static int direction; 
private static int currentX, currentY;

//INSTANCE ATTRIBUTES

private BoardPanel drawPanel;
private JPanel bottomPanel;
private JLabel message;
private Timer timer;

//CONSTRUCTOR

public MainGUI() {
   super("Grid");
   createGUI();
}

//PUBLIC INSTANCE METHODS

public void createbottomPanel(){
   this.bottomPanel=new JPanel();
   this.message = new JLabel("To START, Press \"ENTER\"", JLabel.CENTER);
   this.message.setBackground(Color.LIGHT_GRAY);
   this.message.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
   this.bottomPanel.add(this.message);
}

public void createGUI(){
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.setPreferredSize(new Dimension(800, 600));
   this.drawPanel = new BoardPanel();
   createbottomPanel();

   addComponentListener(this);
   addKeyListener(this);
   addFocusListener(this);
   this.message.addKeyListener(this);

   Container contPane = getContentPane();
   contPane.setLayout(new BorderLayout());
   contPane.add(this.drawPanel,BorderLayout.CENTER);
   contPane.add(this.bottomPanel, BorderLayout.SOUTH);

   this.currentX = 50; 
   this.currentY = 30;          
   this.direction = STOP;       
   this.message.requestFocus();

   pack();
}

public void componentResized(ComponentEvent e) {
   this.drawPanel.setGridUnit();
}

public void componentHidden(ComponentEvent e) {
   //do-nothing
}

public void componentMoved(ComponentEvent e) {
   //do-nothing
 }

public void componentShown(ComponentEvent e) {
    //do-nothing
 }

public void actionPerformed(ActionEvent e) {
  switch (direction) {
     case UP:
        if (currentY > 0)
            currentY--;    
        BoardPanel.fillCell(currentX,currentY);
        this.drawPanel.repaint();   
        break;
     case DOWN:
        if (currentY < NUMBER_OF_ROWS-1)
            currentY++;  
        BoardPanel.fillCell(currentX,currentY);
        this.drawPanel.repaint();
        break;
     case RIGHT:
        if (currentX < NUMBER_OF_COLUMNS-1)
            currentX++;  
        BoardPanel.fillCell(currentX,currentY);
        this.drawPanel.repaint();
        break;
     case LEFT:
        if (currentX > 0)
            currentX--;  
        BoardPanel.fillCell(currentX,currentY);
        this.drawPanel.repaint();
        break;
  }
}

public void focusGained(FocusEvent e) {
   direction=STOP;  
   message.setText("To PAUSE, Press \"P\"");
   timer = new Timer(50,this); 
   timer.start();
}

public void focusLost(FocusEvent e) {
   if (timer != null)
       timer.stop();
   timer = null;
   message.setText("To START, Press \"ENTER\"");
}

public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();  

  if (code == KeyEvent.VK_LEFT)
      direction = LEFT;
  else if (code == KeyEvent.VK_RIGHT)
           direction = RIGHT;
  else if (code == KeyEvent.VK_UP)
           direction = UP;
  else if (code == KeyEvent.VK_DOWN)
           direction = DOWN;
  else if (code == KeyEvent.VK_P)
           message.requestFocus();
  else if (code == KeyEvent.VK_ENTER)
           this.requestFocus();
}

public void keyReleased(KeyEvent e) {
   //do-nothing    
}

public void keyTyped(KeyEvent e) {
   //do-nothing    
}

//STATIC METHODS

public static void openWindow() {
   javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run () {
                             new MainGUI().setVisible(true);
      }
   });
} 

} // end class

逻辑课程

public class Logic {

//STATIC ATTRIBUTES

private static MainGUI test = new MainGUI();

//STATIC METHODS   

public static void initGame(){
   test.openWindow();

}
//This method must be called from actionPerformed in MainGUI and 
//contain the code to be executed
public static void logicPerforms(){
  //empty-for-now
}
//This method must be called from keyPressed in MainGUI and 
//contain the code to be executed
public static void logicPress(){
   //empty-for-now
}

} //end-class

这些类在不同的包中,但我省略了各种导入。 Boardpanel类绘制网格并通过其fillCells方法为单元格坐标(x,y)着色。 主要课程是:

public class Main {

public static void main(String[] args) {
   Logic.initGame();

}

} //end-class

我在逻辑类中传输了必要的变量,从MainGUI的actionPerformed和keyPressed方法中复制了代码,并创建了从logicPerforms和logicPress调用的静态方法(例如运行重绘)来解决调用问题但是这些行不起作用:

else if (code == KeyEvent.VK_P)
          test.askMessageFocus();
else if (code == KeyEvent.VK_ENTER)
          test.askGUIFocus();

其中askMessageFocus e askGUIFocus是前面提到的两个静态方法(它们在MainGUI类中),其代码为:

this.message.requestFocus(); 

用于askMessageFocus和

this.requestFocus();

另一个。

我为我的英语和其他错误道歉,我希望有人可以帮助我! 几乎任何建议都会受到欢迎,甚至旨在优化代码的其他部分! 谢谢!!

0 个答案:

没有答案