Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将变量/对象从操作侦听器传递到驱动程序类?_Java_User Interface_Actionlistener - Fatal编程技术网

Java 如何将变量/对象从操作侦听器传递到驱动程序类?

Java 如何将变量/对象从操作侦听器传递到驱动程序类?,java,user-interface,actionlistener,Java,User Interface,Actionlistener,我有这些类:main和GUI。在GUI中有一个actionListener,它从用户界面面板收集信息。如何将这些变量发送到驱动程序类,以便在那里执行所有操作?我需要将它们添加到无限循环来绘制移动对象,绘制方法在另一个类中。GUI具有扩展框架 以下是课程: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUI extends Frame implements WindowListene

我有这些类:main和GUI。在GUI中有一个actionListener,它从用户界面面板收集信息。如何将这些变量发送到驱动程序类,以便在那里执行所有操作?我需要将它们添加到无限循环来绘制移动对象,绘制方法在另一个类中。GUI具有扩展框架

以下是课程:

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

public class GUI extends Frame implements WindowListener,ActionListener {
 JLabel name1  = new JLabel("Name");
 JLabel color1  = new JLabel("Color");
 JLabel diam1  = new JLabel("Diameter");
 JLabel dist1  = new JLabel("Distance");
 JLabel speed1  = new JLabel("Speed");
 JTextField name2 = new JTextField();
 JTextField color2  = new JTextField();
 JTextField diam2  = new JTextField();
 JTextField dist2  = new JTextField();
 JTextField speed2  = new JTextField();

public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
double distance;
int Speed;
double diameter;

public void actionPerformed(ActionEvent e) {
createVariables();
}
public void createVariables(){

try {
      distance = Double.parseDouble(dist2.getText());
      Speed = Integer.parseInt(speed2.getText());
      diameter = Double.parseDouble(diam2.getText());
    }
    catch(NumberFormatException i) {
    }   
    Planet cP = new        Planet(name2.getText(),distance,diameter,color2.getText(),Speed  );
    Main plnt = new Main(cP);
}

public void createFrame1(){
addWindowListener(this);
setLayout(new GridLayout(6,6,5,5));
JButton mygt = new JButton("Add planet");
mygt.addActionListener(this);
name2.setText("belekoks");color2.setText("RED");diam2.setText("30");dist2.setText(" 60");speed2.setText("2");
add(name1);add(name2);add(color1);add(color2);add(diam1);
add(diam2);add(dist1);add(dist2);add(speed1);add(speed2);
add(mygt);

}
public GUI(String title){
super(title);
createFrame1();
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
主要内容:

import java.awt.event.ActionEvent;
import java.util.Random;
public class Main{
/**
 * @param args
 */

public Main(Planet name){
    super(name);
}

public static void main(String[] args) {
SolarSystem system = new SolarSystem(300, 300);
GUI p = new GUI("New Planet");
p.pack();
p.setVisible(true);
Sun sun = new Sun("Sun", 0, 90, "YELLOW", 0);
Planet venus = new Planet("Venus",70,15,"ORANGE",2);
Planet earth = new Planet("Earth",100,20,"BLUE",1);
Moon moon = new Moon("Moon",earth.dist,5,"GRAY", 1, 30, 3);
Moon[] moons = new Moon[100];
moons[0] = moon;
Planet[] planets = new Planet[100];
planets[0] = earth;
planets[1] = venus;
Random rG = new Random();
Asteroid[] asteroids = new Asteroid[100];
for(int i=0; i<100;i++){
    Asteroid Asteroids = new Asteroid("Asteroid",     rG.nextDouble()+rG.nextInt(10)+45,rG.nextDouble()+rG.nextInt(10),"DARK_GRAY",rG.nextInt(360        ), 1);
    asteroids[i]=Asteroids;
}   

/*LinkedList list = new LinkedList();
for (int i=0;i<1;i++){
    list.add(moons[i]);
}
for (int i=0;i<2;i++){
    list.add(planets[i]);
}
for (int i=0;i<100;i++){
    list.add(asteroids[i]);
}
for (int i=0;i<100;i++){
    list.add(sun);
}

for(int y=0;y<2;y++){
    planets[2].move();
    planets[2].drawOn(system);
    system.finishedDrawing();
}

    for (int i=0; i>=0; i++){
    sun.drawOn(system);

    for(int y=0;y<3;y++){
        planets[y].move();
        planets[y].drawOn(system);
    }

    for (int y=0; y<100; y++){
        asteroids[y].move();
        asteroids[y].drawOn(system);
    }
    for (int y=0; y<1; y++){
        moons[y].move();
        moons[y].drawOn(system);
    }

    system.finishedDrawing();
    }
*/
}
}

还有CosmicEntity类,它保存了所有变量和方法,可以从传递的变量(super(名称、距离、直径、col、速度、0))中绘制行星

根据提供的信息很难确切知道您需要什么,但我认为您不希望使用无限循环,而不是GUI。大多数简单的动画都可以通过摆动计时器轻松驱动

至于您的另一个问题,一般规则是,如果对象具有对其他对象的有效引用,那么它们可以调用其他对象的方法。如何为您的项目执行此操作将取决于您的代码。通常我们将一个对象的引用作为方法或构造函数参数传递给另一个对象。比如说,

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class FooMain {
   public static void main(String[] args) {
      FooNonGui nonGuiReference = new FooNonGui();
      FooGui fooGui = new FooGui(nonGuiReference);

      fooGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fooGui.pack();
      fooGui.setLocationRelativeTo(null);
      fooGui.setVisible(true);

   }
}

class FooGui extends JFrame implements ActionListener {
   private FooNonGui nonGuiVariable;
   private int counter = 0;

   public FooGui(FooNonGui nonGuiParameter) {
      super("GUI");
      this.nonGuiVariable = nonGuiParameter;
      JButton button = new JButton("Button");
      button.addActionListener(this); // I hate doing this, but for brevity's sake...
      add(button);
   }

   public void actionPerformed(ActionEvent e) {
      nonGuiVariable.nonGuiMethod(counter);
      counter++;
   }
}

class FooNonGui {
   public void nonGuiMethod(int counter) {
      System.out.print("In non-GUI method.  ");
      System.out.println("counter is " + counter);
   }
}
我建议您向我们提供更多信息,以便我们能够更了解您的问题,并为您提供更好的帮助。此链接可能有助于:


编辑A:你要把你的行星添加到什么?太阳系物体?无论它是哪个类,都可能有或应该有一个公共addPLanet(行星)方法。如果是SolarSystem,那么您需要将对该对象的引用传递到您的GUI类中,类似于我上面所做的。

根据提供的信息很难确切地知道您需要什么,但我认为您不想使用无限循环,而不是GUI。大多数简单的动画都可以通过摆动计时器轻松驱动

至于您的另一个问题,一般规则是,如果对象具有对其他对象的有效引用,那么它们可以调用其他对象的方法。如何为您的项目执行此操作将取决于您的代码。通常我们将一个对象的引用作为方法或构造函数参数传递给另一个对象。比如说,

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class FooMain {
   public static void main(String[] args) {
      FooNonGui nonGuiReference = new FooNonGui();
      FooGui fooGui = new FooGui(nonGuiReference);

      fooGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fooGui.pack();
      fooGui.setLocationRelativeTo(null);
      fooGui.setVisible(true);

   }
}

class FooGui extends JFrame implements ActionListener {
   private FooNonGui nonGuiVariable;
   private int counter = 0;

   public FooGui(FooNonGui nonGuiParameter) {
      super("GUI");
      this.nonGuiVariable = nonGuiParameter;
      JButton button = new JButton("Button");
      button.addActionListener(this); // I hate doing this, but for brevity's sake...
      add(button);
   }

   public void actionPerformed(ActionEvent e) {
      nonGuiVariable.nonGuiMethod(counter);
      counter++;
   }
}

class FooNonGui {
   public void nonGuiMethod(int counter) {
      System.out.print("In non-GUI method.  ");
      System.out.println("counter is " + counter);
   }
}
我建议您向我们提供更多信息,以便我们能够更了解您的问题,并为您提供更好的帮助。此链接可能有助于:


编辑A:你要把你的行星添加到什么?太阳系物体?无论它是哪个类,都可能有或应该有一个公共addPLanet(行星)方法。如果是SolarSystem,那么您需要将对该对象的引用传递到您的GUI类中,类似于我上面所做的。

好的,有一个drawOn方法,它获取参数并绘制给定的行星。行星被画在那个框架上(太阳系)。我想从用户那里得到这些参数,并把它们画在同一个太阳系框架上。但什么样的集合可以容纳所有的行星呢?SolarSystem类是否有ArrayList?任何类都有addPlanet(Planet)方法吗?如果我理解你的意思,没有任何类可以保存行星,它只是和带有参数的对象,这些参数传递给solarsystem会创建一个图形图像。有些东西必须保存它们,但看起来它可能保存在一个数组Planet[]planets=Planet[100]的主类中。所以Main可能需要一个addPlanet(Planet)方法,GUI可以调用它。鉴于上面的代码,很难说清楚,因为我仍然不知道您的程序是如何完全工作的。我看不到您是如何设置动画的,也看不到动画将如何移动行星(假设它是这样做的),也看不到绘图部分将如何迭代行星以绘制行星。如果你仍然有问题,你需要给我们更多的细节,更多的细节。好吧,有一种drawOn方法,它获取参数并按照给定的方式绘制行星。行星被画在那个框架上(太阳系)。我想从用户那里得到这些参数,并把它们画在同一个太阳系框架上。但什么样的集合可以容纳所有的行星呢?SolarSystem类是否有ArrayList?任何类都有addPlanet(Planet)方法吗?如果我理解你的意思,没有任何类可以保存行星,它只是和带有参数的对象,这些参数传递给solarsystem会创建一个图形图像。有些东西必须保存它们,但看起来它可能保存在一个数组Planet[]planets=Planet[100]的主类中。所以Main可能需要一个addPlanet(Planet)方法,GUI可以调用它。鉴于上面的代码,很难说清楚,因为我仍然不知道您的程序是如何完全工作的。我看不到您是如何设置动画的,也看不到动画将如何移动行星(假设它是这样做的),也看不到绘图部分将如何迭代行星以绘制行星。如果你仍然有问题,你需要给我们更多的细节,更多的细节。