内心倾听者&足球比赛计划的计时器

时间:2016-08-01 00:16:11

标签: java swing timer inner-classes listeners

我想知道如何将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);
}

}

1 个答案:

答案 0 :(得分: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{...}
}

inner classes上阅读更多内容。

详情请阅读timers