[英]How to pass variables/objects from action listener to driver class?
我上那些課:主要和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();
}
*/
}
}
我不希望您瀏覽整個代碼,但是最主要的是,我在GUI的actionListener中獲取了變量,我想將它們傳遞給main以便創建一個以后可以繪制的對象。
Planet類創建一個對象:
public class Planet extends CosmicEntity {
public Planet(String name, double distance, double diameter, String col, int speed) {
super(name,distance,diameter,col,speed, 0);
}
}
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);
}
}
我建議您向我們提供更多信息,以便我們對您的問題有更多的了解,並為您提供更好的幫助。 該鏈接可能有幫助: 聰明的問題
編輯-答:您要添加什么行星? SolarSystem對象? 無論是哪個類,都有或應該有一個公共的addPLanet(Planet planet)方法。 如果是SolarSystem,那么您將要像上面我所做的那樣,將對該對象的引用傳遞到您的GUI類中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.