java—如何使它在打开一个jframe时,另一个jframe中的计时器不会启动?

piv4azn7  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(204)

我有两个pane类,分别有jframe。我也有另一个jframe作为一个水平菜单,当我打开jframe两个定时器之一开始我肯定我错过了一些简单的东西,但有人能帮我吗。
一级
窗格类

private static final long serialVersionUID = 1L;
private int x=0;
private int h=getHeight();
private int x1=20;
private int y1=100;
private int w;
private Timer timer;
private Timer timer2;
private Timer timer3;
private int t=60;
private String ti=Integer.toString(t);
private int ccounter=0;
private int angle=60;
private int time=1;
private int initialx=5;
private int initialy=1;
private double dx=Ballphysics.xveloctiy(angle, initialx);
private double dy=Ballphysics.yveloctiy(angle, initialy, time);
private int c1x=45;
private int c2x=60;
private int c3x=75;
private int gamex=-500;
private int points=0;
private Game_Model model;
private boolean a=false;

public Game_Pane(Game_Model model) {
    this.setModel(model);
       model.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                repaint();
            }
        });
    setFocusable(true);
    addKeyListener(this);
    setBackground(Color.black);
    timer= new Timer(50,new TimerCallback());
    timer2=new Timer(5,new TimerCallback2());
    timer3=new Timer(1000,new TimerCallback3());
    timer.start();
    timer2.start();
    timer3.start();

}

public boolean isA() {
    return a;
}

public void setA(boolean a) {
    this.a = a;
}

public class TimerCallback implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {

        new Ballphysics();
        if(x1 < 0 || x1 > w-10)
        {
           dx=-dx;
        }

        if (x1 < 0)  
        {
            x1 = 0;
        } 
        if (x1 > w-10)  
        {
            x1 = w-10;
        } 
        else 
        {
            x1 = (int) (x1 + dx);
        }

        if (y1<50)
        {
            dy=-dy;
        }
        if (y1>h-50&&x1>=x&&x1<=x+150&&y1<h-40) {
            dy=-dy;
            points++;
        }

        if (y1<50) {
            y1=50;
        }

        else 
        {
            y1=(int)(y1+dy);

        }

        if (y1>1000) {
            ccounter+=1;
            y1=50;
            x1=0;
            if (ccounter==1) {
                c3x=-500;
            }
            if (ccounter==2) {
                c2x=-500;
            }
            if (ccounter==3) {
                c1x=-500;
                timer2.stop();
                timer3.stop();
                gamex=w/2-75;
                timer.stop();
            }
        }

        if (t==0) {
            new Start_Menu(model).setC(true);
            if (ccounter==0) {
                points+=50;
            }
            else if (ccounter==1) {
                points+=25;
            }
            else if (ccounter==2) {
                points+=5;
            }
            points+=50;
        }
        repaint();  
}}

public class TimerCallback2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();  
}}

public class TimerCallback3 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        t-=1;
        time++;
        ti=Integer.toString(t);
        if (t==-1) {
            timer.stop();
            timer3.stop();
            timer2.stop();
        }
    }

}

public void paintComponent(Graphics g) {

    h=getHeight();
    w=getWidth();
    super.paintComponent(g);
    g.setColor(Color.white);
    g.drawString("Lives:", 10, 20);
    g.drawString("Time Remaing:", w-140, 20);
    g.drawString(ti, w-50, 20);
    g.fillOval(c1x, 10, 10, 10);
    g.fillOval(c2x, 10, 10, 10);
    g.fillOval(c3x, 10, 10, 10);
    g.drawLine(0,50,w,50);
    g.drawLine(x, h-40, x+150, h-40);
    g.setColor(Color.red);
    g.fillOval(x1, y1, 10, 10);
    g.setFont(new Font("Times New Roman",Font.BOLD,30));
    g.drawString("Game Over", gamex, h/2);
    String str=Integer.toString(points);
    g.drawString(str, w/2-15, 30);

}

@Override
public void keyPressed(KeyEvent e) {
    h=getHeight();
    w=getWidth();
    if (x>=0) {
    if (e.getKeyCode()==37) {
        x-=5;
    }
    else if (e.getKeyCode()==39) {
        x+=5;
    }}
    if (x<0) {
        x=0;
    }
    else if (x>w-150) {
        x=w-150;}
    }

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {
}

public Game_Model getModel() {
    return model;
}

public void setModel(Game_Model model) {
    this.model = model;
}

第一个窗格的框架(级别)
窗格的框架

private static final long serialVersionUID = 1L;
private Game_Model model= new Game_Model();
private Game_Pane p= new Game_Pane(model);
public Game_Frame() {
add(p);
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Level 1");

}
第二层的窗格是相同的,所以是框架,因为我没有改变它是唯一的,所以我不会添加太多。
级别菜单

private static final long serialVersionUID = 1L;
private JButton button= new JButton("1 player level 1"); 
private JButton button2= new JButton ("1 player level 2");
private JButton button3= new JButton ("2 player level 1");
private JButton button4= new JButton ("2 player level 2");
private Dimension d= new Dimension(300,50);
private Game_Frame Game_Frame= new Game_Frame();
private Game_Frame2 Frame2= new Game_Frame2();

public Level_Menu() {
    setLayout (new GridBagLayout());
    GridBagConstraints gbc= new GridBagConstraints();
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbc.fill=GridBagConstraints.HORIZONTAL;
    add(button,gbc);
    add(button2,gbc);
    add(button3,gbc);
    add(button4,gbc);
    button.setPreferredSize(d);
    button2.setPreferredSize(d);
    button3.setPreferredSize(d);
    button4.setPreferredSize(d);
    button.setBackground(Color.GREEN);
    button2.setBackground(Color.green);
    button3.setBackground(Color.GREEN);
    button4.setBackground(Color.green);
    setBackground(Color.BLACK);
    button.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    Object a=e.getSource();
    if (a==button) {
        Game_Frame.setVisible(true);
    }
    else if (a==button2) {
        Frame2.setVisible(true);
    }
    else if (a==button3) {

    }
    else if (a==button4) {

    }

}

菜单框

private static final long serialVersionUID = 1L;
private Level_Menu level= new Level_Menu(); 
public Level_Frame() {
add(level);
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Level Menu");}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题