如何使“proximo”(下一个)按钮工作并移动火箭(foguete)

时间:2018-05-11 00:19:18

标签: java swing object jframe jbutton

我需要让我的火箭相应地移动到正确的asnwer,如果他(她)做对了,它会移动(从行星(左)到(右)行星,否则,它什么都不做,我有一个正确问题的反击,但我不知道接下来该做什么。

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.Enumeration;
    import java.util.HashMap;

    import javax.swing.AbstractButton;
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;


    public class Jogo0 extends JFrame implements ActionListener{

我的变量

    JPanel QUIZ;
    JRadioButton opcao1;//choice1
    JRadioButton opcao2;//choice2
    JRadioButton opcao3;//choice3
    ButtonGroup escolha;//buttongroup
    JLabel questao; //question
    JButton proximo; //next button
    String [] [] alternativa; //alternatives
    String [] [] correta; //correct answer
    int acerto; //my counter


    int posifogx = 0; 
    int posifogy = 300;

图像代码,火箭,背景,行星

    ImageIcon imagfogut = new         v         ImageIcon(getClass().getResource("Foguetao2.gif")); 
    ImageIcon imagespac = new ImageIcon(getClass().getResource("Background.jpg"));
    ImageIcon imageplantale = new ImageIcon(getClass().getResource("giphy.gif"));
    ImageIcon imageterra = new ImageIcon(getClass().getResource("terra.gif"));


    JLabel fog = new JLabel (imagfogut);
    JLabel background = new JLabel (imagespac);
    JLabel planetale = new JLabel (imageplantale);
    JLabel terra = new JLabel (imageterra);

我的方法jogo,将运行整个游戏
        公共Jogo0(){

    Janela(); //window
    Imagens(); //img
    QuizMetodo(); //my quizmethod


                }

    public void Imagens (){ //imgs

    background.setBounds(0, 0, 1920, 1080);
    fog.setBounds(posifogx, posifogy, 300, 200);
    planetale.setBounds(-150, 300, 259, 259);
    terra.setBounds(1000, 0, 379, 379);


                        }


    public void Janela()//window                {

    setTitle("Game");
    setLocation(0,0);
    setVisible(true);
    setSize(1920,1080);
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(fog);
    add(planetale);
    add(terra);
    add(background);

                                    }

    public void foguetex(int moverfoguetex) { //position of the rocket
    this.posifogx = moverfoguetex;
    moverfoguetex = moverfoguetex + 100;
                                        }

    public void foguetey(int moverfoguetey) {
    this.posifogy = moverfoguetey;
    moverfoguetey = moverfoguetey + 100;
                                        }

      public synchronized void QuizMetodo(){
      questoes();
      Container cont=getContentPane();
      cont.setLayout(null);          
      cont.setBackground(Color.GRAY);
      escolha=new ButtonGroup();     
      opcao1=new JRadioButton("Opção1",true);
      opcao2=new JRadioButton("Opção2",false);
      opcao3=new JRadioButton("Opção3",false);
      escolha.add(opcao1);
      escolha.add(opcao2);
      escolha.add(opcao3);
      questao= new JLabel("Salve seu planeta!");
      questao.setForeground(Color.WHITE);
      questao.setFont(new Font("tahoma", Font.BOLD, 14));
      proximo=new JButton("Proximo");
      proximo.setForeground(Color.BLACK);                
      proximo.addActionListener(aa);
      opcao1.addActionListener(aa);
      opcao2.addActionListener(aa);
      opcao3.addActionListener(aa);
      QUIZ=new JPanel();
      QUIZ.setBackground(Color.DARK_GRAY);
      QUIZ.setLocation(250,530);
      QUIZ.setSize(800,150);
      QUIZ.setLayout(new GridLayout(6,2));
      QUIZ.add(questao);
      QUIZ.add(opcao1);
      QUIZ.add(opcao2);
      QUIZ.add(opcao3);
      QUIZ.add(proximo);
      cont.add(QUIZ);
      setVisible(true);
      acerto = 0;
      i = acerto;
      lerqr(acerto);
        }

      public String getSelection(){
    String selectedChoice=null;
    Enumeration<AbstractButton> buttons=escolha.getElements(); 
    while(buttons.hasMoreElements()) 
    { 
    JRadioButton temp=(JRadioButton)buttons.nextElement(); 
    if(temp.isSelected()) 
                { 
                            selectedChoice=temp.getText();
                } 
     }  
    return(selectedChoice);

                            }

ActionListener aa = new ActionListener(){ //**I DONT KNOW WHAT TO DO HERE**

    public void actionPerformed(ActionEvent e) {



    }
    };

    public void questoes() { //questions
    alternativa = new String [10][4];

    alternativa[0][0] ="Qual é o composição química da água?";
    alternativa[0][1] = "(2) Hidrogênio e (1) Carbono";
    alternativa[0][2] = "(1) Hidroênio e (2) Oxigênio";
    alternativa[0][3] = "(2) Hidrogênio e (1) Oxigênio";

    alternativa[1][0] = "Pra que serve o Protocolo de Kyoto?";
    alternativa[1][1] = "Defesa dos animais";
    alternativa[1][2] = "Proteção contra emissão de gases";
    alternativa[1][3] = "Codigo da reciclagem";


    correta = new String [10] [2];
    correta[0][0] = "Qual é o composição química da água?";
    correta[0][1] = "(2) Hidrogênio e (1) Oxigênio";

    correta [1][0] = "Pra que serve o Protocolo de Kyoto?";
    correta [1][1] = "Proteção contra emissão de gases";


                        }


    public void lerqr(int id){
    questao.setText("  "+alternativa[id][0]);
    opcao1.setText(alternativa[id][1]);
    opcao2.setText(alternativa[id][2]);
    opcao3.setText(alternativa[id][3]);
    opcao1.setSelected(true);
    }



    public static void main(String[] args) { //my main method


    new Jogo0();
    }

1 个答案:

答案 0 :(得分:0)

我不知道你是否需要方法foguetex();和foguetey();在你实现它们的方式中,但两种方法中的最后一行似乎都没有以任何方式影响程序。

要使火箭移动并计算移动,您可以简单地修改foguetex();像这样的方法(或者如果你还需要foguetex(),则写一个不同名称的方法;代码中的其他东西):

public void foguetex(int moverfoguetex) { // position of the rocket
    this.posifogx = moverfoguetex + 100;
}

这将导致值posifogx更新为火箭应该在的新x值(您还可以修改foguetey();如果需要)。执行此操作后,您需要更新显示火箭的JLabel的位置。 因此,您在ActionListener中重新分配JLabel的边界,类似于此(您可能希望将其调整为您自己编写的ActionListener):

proximo.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        foguetex(posifogx); //calculating new x-position
        fog.setBounds(posifogx, posifogy, 300, 200); //updating the label's position
    }
});

希望这会对你有所帮助。