-1
我想知道如何將一個Timer和我的所有監聽器應用爲內部類。我仍然對內部類感到困惑,所以有人可以解釋,我怎樣才能將它應用到我自己的程序中,這將非常感激。我剛剛學會了計時器,但我不確定在哪裏以及如何設置它。我想設定60秒。內部監聽器和計時器的足球遊戲程序
這裏是我的代碼:
package runningback;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainPanel extends JPanel implements KeyListener
{
JLabel text = new JLabel("Touchdowns: ");
JLabel text2 = new JLabel("Tackles: ");
JLabel touchdownsLbl;
JLabel tacklesLbl;
JButton movingButton = new JButton("key code");
JButton startBtn = new JButton("Start");
JLabel background=new JLabel(new ImageIcon("playground.png"));
JLabel rb=new JLabel(new ImageIcon("rb.png"));
JLabel tackler=new JLabel(new ImageIcon("tackler.png"));
int x =0;
int y = 10;
int touchdowns;
int tackles;
Player mainPlayer;
public MainPanel()
{
mainPlayer = new Player();
touchdowns = 0;
tackles = 0;
touchdownsLbl = new JLabel(Integer.toString(touchdowns));
tacklesLbl = new JLabel(Integer.toString(tackles));
setLayout(null);
//setBackground(Color.pink);
add(text);
add(text2);
add(touchdownsLbl);
add(tacklesLbl);
text.setBounds(new Rectangle(350, 20, 350, 30));
text2.setBounds(new Rectangle(450, 20, 350, 30));
touchdownsLbl.setBounds(new Rectangle(435, 20, 350, 30));
tacklesLbl.setBounds(new Rectangle(505, 20, 350, 30));
setFocusable(true);
addKeyListener(this);
add(rb);
add(background);
background.setBounds(new Rectangle(0, 94, 624, 416));
rb.setBounds(new Rectangle(mainPlayer.x, mainPlayer.y, 50, 44));
//add(movingButton);
startBtn.setBounds(new Rectangle(250, 20, 80, 30));
add(startBtn);
startBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//System.out.println("Button Pressed");
gameReset();
}
});
//---------------------TIMER------------------------------
int delay = 1000; // milliseconds
tim = new Timer(delay,this);
tim.start(); // starts the timer
//---------------------TIMER------------------------------
tim.setDelay(200); // sets a new delay
tim.stop(); // stops the timer // stops the timer
}
}
public void keyPressed(KeyEvent evt)
{
//System.out.println("Key pressed");
int kk = evt.getKeyCode();
if(kk == evt.VK_LEFT) {mainPlayer.x=mainPlayer.x-mainPlayer.speed;}
if(kk == evt.VK_RIGHT) {mainPlayer.x=mainPlayer.x+mainPlayer.speed;}
if(kk == evt.VK_UP) {mainPlayer.y=mainPlayer.y-mainPlayer.speed;}
if(kk == evt.VK_DOWN) {mainPlayer.y=mainPlayer.y+mainPlayer.speed;}
update();
//movingButton.setText(""+evt.getKeyCode()+","+evt.getKeyChar());
//movingButton.setBounds(new Rectangle(x,y,80,30));
}
void update(){
rb.setBounds(new Rectangle(mainPlayer.x, mainPlayer.y, 50, 44));
}
void gameReset(){
mainPlayer.x = 30;
mainPlayer.y = 280;
update();
}
public void keyReleased(KeyEvent evt) { }
public void keyTyped(KeyEvent evt) { }
public void paintComponent(Graphics g)
{
//System.out.println("Painting");
super.paintComponent(g);
requestFocusInWindow();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(624, 500);
}
}
您已經在使用上面一些你的ActionListeners的「內部」類 - 所以請註明儘可能它是什麼,究竟是混淆你。我們越理解這個問題,通常答案就越好。我自己,我有點困惑,爲什麼你要調用'requestFocusInWindow();'paintComponent方法內部。這看起來像是一件危險的事情,我不確定你什麼時候會想要這種功能。 –
另外,我沒有看到上面的任何定時器代碼,所以請澄清你的意思。你指的是一個Swing Timer(javax.swing.Timer)實例嗎? –
我想出了我的內部類,並添加了一個計時器。 int delay = 1000; //毫秒 tim = new Timer(delay,this); tim.start(); //啓動計時器 tim.setDelay(200); //設置新的延遲 tim.stop(); //停止計時器//停止計時器 –