I would like to know how I can apply a Timer and all my listeners as inner classes. I'm still confused about inner classes so can someone explain and how I can apply it to my own program, that would be much appreciated. I just learned timer but I'm not sure where and how to set it. I would like to set for 60 seconds.
我想知道如何将Timer和我的所有听众都用作内部类。我仍然对内部课程感到困惑,所以有人可以解释以及如何将其应用到我自己的课程中,我将不胜感激。我刚刚学会了计时器,但我不知道在哪里以及如何设置它。我想设置60秒。
here is my code:
这是我的代码:
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);
}
}
1 个解决方案
#1
3
public class MainPanel {
// setting timer
public void startTimer(double delay){
long milisecond = 1000l;
Timer t = new Timer((int) (delay*milisecond), new TimerListener());
t.setInitialDelay((int) (delay*milisecond));
t.start();
}
// inner classes
public class TimerListener implements ActionListener{...}
public class KeyListener extends KeyAdapter{...}
}
read more on inner classes.
阅读更多内部课程。
read more on timers
详细了解定时器
#1
3
public class MainPanel {
// setting timer
public void startTimer(double delay){
long milisecond = 1000l;
Timer t = new Timer((int) (delay*milisecond), new TimerListener());
t.setInitialDelay((int) (delay*milisecond));
t.start();
}
// inner classes
public class TimerListener implements ActionListener{...}
public class KeyListener extends KeyAdapter{...}
}
read more on inner classes.
阅读更多内部课程。
read more on timers
详细了解定时器