首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使“proximo”(下一步)按钮工作并移动火箭(Foguete)

如何使“proximo”(下一步)按钮工作并移动火箭(Foguete)
EN

Stack Overflow用户
提问于 2018-05-11 08:19:18
回答 1查看 46关注 0票数 0

我需要让我的火箭相应地移动到正确的方向,如果他(她)做对了,它将(从行星(左)移动到(右)行星,否则,它将什么都不做,我有一个计数器来回答正确的问题,但我不知道下一步该做什么。

代码语言:javascript
复制
    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{

我的变量

代码语言:javascript
复制
    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;

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

代码语言:javascript
复制
    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,它将运行整个游戏

代码语言:javascript
复制
     public Jogo0 (){
代码语言:javascript
复制
    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();
    }
EN

回答 1

Stack Overflow用户

发布于 2018-05-20 08:29:16

我不确切地知道您是否需要方法foguetex();和foguetey();,就像您实现它们的方式一样,但是这两个方法中的最后一行似乎对程序没有任何影响。

要让火箭移动并计算运动,您可以简单地修改foguetex();方法,如下所示(或者,如果您还需要foguetex();用于代码中的其他内容,则使用不同的名称编写一个方法):

代码语言:javascript
复制
public void foguetex(int moverfoguetex) { // position of the rocket
    this.posifogx = moverfoguetex + 100;
}

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

代码语言:javascript
复制
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
    }
});

希望这对你有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50283227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档