退出或退出actionListener

时间:2015-05-05 18:35:59

标签: java actionlistener

我正在使用java创建通用Battleships游戏并且我正在制作GUI,我已经设置了GUI,以便用户将位置输入到JTextField中,然后单击一个Button,它执行Label.getText()我的代码如下。

        int state = 0;

        if (state == 0) {
          System.out.println("How many Submarines would you like");
          label1.setText("How many Submarines would you like");
          button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              String subsString = textField1.getText();
              int subs = Integer.parseInt(subsString);
              for (int i = 0; i < subs; i++) {
                ship.add(new Ship("Sub", 1));
                CPUship.add(new CPUShip("Sub", 1));
                System.out.println("Added Sub");
              }
              label1.setText("How many Friggates would you like");
            }
          });
          state = 1;
        }

        if (state == 1) {
          // number of frigates in play
          System.out.println("How many Friggates would you like");
          button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              String frigsString = textField1.getText();
              int frigs = Integer.parseInt(frigsString);
              for (int i = 0; i < frigs; i++) {
                ship.add(new Ship("Frig", 2));
                CPUship.add(new CPUShip("Frig", 2));
                System.out.println("Added Frig");
              }
              label1.setText("How many Battleships would you like");

            }
            state = 2;

          });
        }


        if (state == 2) {
          // number of battle ships in play
          button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              System.out.println("How many Battleships would you like");
              String battleshipsString = textField1.getText();
              int battleships = Integer.parseInt(battleshipsString);
              for (int i = 0; i < battleships; i++) {
                ship.add(new Ship("Battleship", 3));
                CPUship.add(new CPUShip("Battleship", 3));
                System.out.println("Added Battleship");
              }
              label1.setText("Done");
            }
          });
          state = 3;
        }
        if (state == 3) {
          System.out.println("Done");

问题是,当我运行代码时,它似乎跳到应该的位置,所以打印到控制台是“你想要多少艘潜艇你想要多少Friggates”,标签是“多少个子你想要“,一旦我进入文本字段并单击提交按钮,标签就会变成”你想要多少Friggates“并且控制台打印出”你想要多少战舰添加战舰添加Frig已添加Sub“然后每次我再次按提交,只需打印4个结果即可。

我认为这是因为我从未实际退出actionListener,所以没有完成等效的break语句来阻止它运行,尽管我可能不正确。我已经添加了状态和if语句,看看我是否可以解决问题,虽然它没有帮助,所以它们可以被删除没问题。

提前感谢并为这么长的帖子道歉。

1 个答案:

答案 0 :(得分:0)

似乎你只需要在button1上添加一次addActionListener(...),如下所示:

state = 0 // state is initialized to 0
label1.setText("How many Submarines would you like");
button1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
         switch(getState()){
              case 0:
                    //Your How-many-submarines logic goes here

                    label1.setText("How many Friggates would you like");
                    setState(1);
                    break;
              case 1:
                    // Your how-many-friggates logic goes here
                     label1.setText("How many Battleships would you like");
                     setState(2);
                     break;

              case 2:
                    // Your how-many-battleships logic goes here
                    label1.setText("Done");
                    setState(3);
                    break;

              case 3:
                    System.out.println("Done");

         }
    }
});