如何连接一个';s类按钮到java中的另一个类方法

如何连接一个';s类按钮到java中的另一个类方法,java,swing,button,methods,Java,Swing,Button,Methods,我有一个战斗类和一个右面板类。我缺少的是连接这两个类的代码。基本上,我想做的是说,好的,选择你的武器,然后点击按钮,我将设置工件的武器,然后代码将继续我的if-else语句,我已经完成了 public class Battle { private MovePiece mpref; //constructors public Battle(MovePiece mpref){ this.mpref =mpref; } //the battle system public Piece w

我有一个战斗类和一个右面板类。我缺少的是连接这两个类的代码。基本上,我想做的是说,好的,选择你的武器,然后点击按钮,我将设置工件的武器,然后代码将继续我的if-else语句,我已经完成了

public class Battle {

private MovePiece mpref;

//constructors
public Battle(MovePiece mpref){
    this.mpref =mpref;
}

//the battle system
public Piece winner (Piece piecetwo, Piece pieceone){
    while(pieceone.getRemlife()>0||piecetwo.getRemlife()>0){

       //what i want to code is Click wind, fire or earth button then
       pieceone.setAttack("wind");

       //then...

       if (pieceone.getAttack().equals("fire")){

             if(piecetwo.getAttack().equals("wind")){
                    int life = piecetwo.getLife();
                    life=life-1;
                    piecetwo.setLife(life);
                    System.out.println("Player 1 wins");
                    }
             else if(piecetwo.getAttack().equals("earth")){
                    int life = pieceone.getLife();
                    life=life-1;
                    pieceone.setLife(life);
                    System.out.println("Player 2 wins");
                    }
          //and so forth

}
我的右面板类有所有的按钮,看起来像这样

public class RightPanel extends JPanel {

    public RightPanel(){
    this.setPreferredSize(dimension);
    this.setLayout(new GridBagLayout());

    JLabel option = new JLabel("Choose your weapon: ");
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.insets = new Insets(0, 20, 0, 20);
    wpanel.add(option, gbc);

    //----Attack Buttons----//
    JPanel wabuttons = new JPanel(new GridLayout(0, 3, 3, 3));
    wabuttons.setEnabled(false);
    gbc.gridx = 0;
    gbc.gridy = 6;
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.insets = new Insets(0, 20, 10, 20);
    wpanel.add(wabuttons, gbc);

     //Wind
        ImageIcon icnWind = new ImageIcon(imageFolderPath + "button/wind.png");
        Image imgWind = icnWater.getImage() ;  
        Image newWind = imgWater.getScaledInstance(25, 25, SCALE_SMOOTH ) ;  
        icnWind = new ImageIcon(newWind);
        btnWind = new JButton("",icnWind);
        btnWind.setEnabled(false);
        btnWind.setForeground(Color.BLUE);
        btnWind.setFont(new Font("Lucida Sans Unicode", Font.ITALIC, 15));
        btnWind.addActionListener(new actWind());
        wabuttons.add(btnWind);
       //Fire//
        ImageIcon icnFire = new ImageIcon(imageFolderPath + "button/fire.png");
        Image imgFire = icnFire.getImage() ;  
        Image newFire = imgFire.getScaledInstance(25, 25, SCALE_SMOOTH ) ;  
        icnFire = new ImageIcon(newFire);
        btnFire = new JButton("",icnFire);
        btnFire.setEnabled(false);
        btnFire.setForeground(Color.RED);
        btnFire.setFont(new Font("Lucida Sans Unicode", Font.ITALIC, 15));
        btnFire.addActionListener(new actFire());
        wabuttons.add(btnFire);
       //Earth//
        ImageIcon icnEarth = new ImageIcon(imageFolderPath + "button/earth.png");
        Image imgEarth = icnEarth.getImage() ;  
        Image newEarth = imgEarth.getScaledInstance(25, 25, SCALE_SMOOTH ) ;  
        icnEarth = new ImageIcon(newEarth);
        btnEarth = new JButton("",icnEarth);
        btnEarth.setEnabled(false);
        btnEarth.setForeground(Color.GREEN);
        btnEarth.setFont(new Font("Lucida Sans Unicode", Font.ITALIC, 15));
        btnEarth.addActionListener(new actEarth());
        wabuttons.add(btnEarth);

    }

    class actWind implements ActionListener {
    public void actionPerformed (ActionEvent e) {

    }


    class actFire implements ActionListener {
    public void actionPerformed (ActionEvent e) {

    }

    class actEarth implements ActionListener {
    public void actionPerformed (ActionEvent e) {

    }





}

您可以使用MVC或模型视图控制模式,其中视图是GUI,模型是您的战斗类,控件将两者连接起来。例如:

import java.awt.event.*;
import javax.swing.*;

public class TestRightPanel {
   private static void createAndShowGui() {
      // create your pieces
      View view = new View();
      Model model = new Model();

      // and let the Control tie them all together
      Control control = new Control(model, view);

      JFrame frame = new JFrame("TestRightPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(view);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class View extends JPanel {
   private Control control;

   public View(Control control) {
      this();
      this.control = control;
   }

   public View() {
      JButton btnFire = new JButton(new FireAction("Fire"));
      add(btnFire);
   }


   public void setControl(Control control) {
      this.control = control;
   }


   private class FireAction extends AbstractAction {
      public FireAction(String name) {
         super(name);
         putValue(MNEMONIC_KEY, KeyEvent.VK_F);
      }

      public void actionPerformed(ActionEvent e) {
         if (control != null) {
           control.fire();  // communicate user input to control
         }
      }

   }
}

class Control {

   private Model model;
   private View view;

   public Control(Model model, View view) {
      this.model = model;
      this.view = view;
      view.setControl(this);
   }

   public void fire() {
      model.fire();
   }

}

class Model {

   public void fire() {
      // TODO finish this code          
   }

}

此外,您的代码可能需要考虑线程,因为您的Battle类似乎有一些长期运行的代码。如果是这样,它不能在Swing事件线程上运行,而是在后台线程中运行。

您可以使用MVC或模型视图控制模式,其中视图是GUI,模型是您的战斗类,控件将两者连接起来。例如:

import java.awt.event.*;
import javax.swing.*;

public class TestRightPanel {
   private static void createAndShowGui() {
      // create your pieces
      View view = new View();
      Model model = new Model();

      // and let the Control tie them all together
      Control control = new Control(model, view);

      JFrame frame = new JFrame("TestRightPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(view);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class View extends JPanel {
   private Control control;

   public View(Control control) {
      this();
      this.control = control;
   }

   public View() {
      JButton btnFire = new JButton(new FireAction("Fire"));
      add(btnFire);
   }


   public void setControl(Control control) {
      this.control = control;
   }


   private class FireAction extends AbstractAction {
      public FireAction(String name) {
         super(name);
         putValue(MNEMONIC_KEY, KeyEvent.VK_F);
      }

      public void actionPerformed(ActionEvent e) {
         if (control != null) {
           control.fire();  // communicate user input to control
         }
      }

   }
}

class Control {

   private Model model;
   private View view;

   public Control(Model model, View view) {
      this.model = model;
      this.view = view;
      view.setControl(this);
   }

   public void fire() {
      model.fire();
   }

}

class Model {

   public void fire() {
      // TODO finish this code          
   }

}

此外,您的代码可能需要考虑线程,因为您的Battle类似乎有一些长期运行的代码。如果是这样,它不能在Swing事件线程上运行,而是在后台线程中运行。

您可以使用MVC或模型视图控制模式,其中视图是GUI,模型是您的战斗类,控件将两者连接起来。例如:

import java.awt.event.*;
import javax.swing.*;

public class TestRightPanel {
   private static void createAndShowGui() {
      // create your pieces
      View view = new View();
      Model model = new Model();

      // and let the Control tie them all together
      Control control = new Control(model, view);

      JFrame frame = new JFrame("TestRightPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(view);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class View extends JPanel {
   private Control control;

   public View(Control control) {
      this();
      this.control = control;
   }

   public View() {
      JButton btnFire = new JButton(new FireAction("Fire"));
      add(btnFire);
   }


   public void setControl(Control control) {
      this.control = control;
   }


   private class FireAction extends AbstractAction {
      public FireAction(String name) {
         super(name);
         putValue(MNEMONIC_KEY, KeyEvent.VK_F);
      }

      public void actionPerformed(ActionEvent e) {
         if (control != null) {
           control.fire();  // communicate user input to control
         }
      }

   }
}

class Control {

   private Model model;
   private View view;

   public Control(Model model, View view) {
      this.model = model;
      this.view = view;
      view.setControl(this);
   }

   public void fire() {
      model.fire();
   }

}

class Model {

   public void fire() {
      // TODO finish this code          
   }

}

此外,您的代码可能需要考虑线程,因为您的Battle类似乎有一些长期运行的代码。如果是这样,它不能在Swing事件线程上运行,而是在后台线程中运行。

您可以使用MVC或模型视图控制模式,其中视图是GUI,模型是您的战斗类,控件将两者连接起来。例如:

import java.awt.event.*;
import javax.swing.*;

public class TestRightPanel {
   private static void createAndShowGui() {
      // create your pieces
      View view = new View();
      Model model = new Model();

      // and let the Control tie them all together
      Control control = new Control(model, view);

      JFrame frame = new JFrame("TestRightPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(view);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class View extends JPanel {
   private Control control;

   public View(Control control) {
      this();
      this.control = control;
   }

   public View() {
      JButton btnFire = new JButton(new FireAction("Fire"));
      add(btnFire);
   }


   public void setControl(Control control) {
      this.control = control;
   }


   private class FireAction extends AbstractAction {
      public FireAction(String name) {
         super(name);
         putValue(MNEMONIC_KEY, KeyEvent.VK_F);
      }

      public void actionPerformed(ActionEvent e) {
         if (control != null) {
           control.fire();  // communicate user input to control
         }
      }

   }
}

class Control {

   private Model model;
   private View view;

   public Control(Model model, View view) {
      this.model = model;
      this.view = view;
      view.setControl(this);
   }

   public void fire() {
      model.fire();
   }

}

class Model {

   public void fire() {
      // TODO finish this code          
   }

}

此外,您的代码可能需要考虑线程,因为您的Battle类似乎有一些长期运行的代码。如果是这样,它不能在Swing事件线程上运行,而是在后台线程中运行。

您需要两名玩家吗?或者,你从哪里得到第二个棋子呢?是的,但是一步一个脚印,你需要两个玩家吗?或者,你从哪里得到第二个棋子呢?是的,但是一步一个脚印,你需要两个玩家吗?或者,你从哪里得到第二个棋子呢?是的,但是一步一个脚印,你需要两个玩家吗?或者你会从哪里得到第二件呢?是的,但只是试着一步一个脚印地完成time@trashgod:是的,这是一个非常好而且更详细的例子。但愿我能再次投票@垃圾神:是的,这是一个非常好而且更加详细的例子。但愿我能再次投票@垃圾神:是的,这是一个非常好而且更加详细的例子。但愿我能再次投票@垃圾神:是的,这是一个非常好而且更加详细的例子。但愿我能再次投票!