在java中使用其他类中的变量从其他文件到另一个类 [英] Use variables from other class from other file to another in java

查看:123
本文介绍了在java中使用其他类中的变量从其他文件到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两名玩家将在JTextFields中输入他们的名字。我想要做的是,我从Enter.java中的Welcome框架输入的数据将被传输到ActualGame.java中的JLabel。

There are two players who will input their name in the JTextFields. What I want to do is that the data I entered from the Welcome frame in Enter.java will be transferred to the JLabels in ActualGame.java.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Enter extends JFrame implements ActionListener {

    private String one = "";
    private String two = "";
    private JTextField txtOne = new JTextField();
    private JTextField txtTwo = new JTextField();

    public Enter() {
        this.setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Welcome");
        setSize(200, 130);
        setVisible(true);
        setResizable(false);
        setLocationRelativeTo(null);

        add(txtOne);
        add(txtTwo);

        enter.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {
        Main main = new Main();
        this.setVisible(false);
        one = txtOne.getText();
        two = txtTwo.getText();
    }
}

Main是保存ActualGame的JFrame的主类()以及Enter()的主类。

Main is the main class that holds the JFrame of ActualGame() and also the main class of Enter().

import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Main extends JFrame {

    public Main() {
        add(new ActualGame());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Main");
        setSize(400, 557);
        setVisible(true);
        setResizable(false);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        Enter enter=new Enter();
    }
}

ActualGame:

the ActualGame:

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

public class ActualGame extends JPanel{

    private JLabel lblOne = new JLabel(one);//how i wish it would be that easy
    private JLabel lblTwo = new JLabel(two);

    public ActualGame() {

        setLayout(new FlowLayout());
        add(lblOne);
        add(lblTwo);

    }
}

我该怎么做才能够使用从Enter.java到ActualGame.java的String变量一和二?
我是编程特别是java swing的新手和菜鸟。接受批评和建议。谢谢。

What should I do to be able to use the String variable one and two from Enter.java to ActualGame.java? I'm new and noob in programming especially java swing. Open to criticisms and suggestions. Thank you.

推荐答案

建议:


  • 将信息从一个对象传递到另一个对象与Swing和其他Java程序没有什么不同。您可以调用方法或构造函数并通过参数传递信息。

  • 传递信息时的关键区别是。对于事件驱动的程序,这通常由事件,一个监听器触发,因此使用观察者设计模式是注释。

  • 出于您的目的,第一个窗口可以是一个模态对话框,例如JOptionPane或模态JDialog,它可以更容易地确定何时传递信息。使用模式对话框时,在对话框可见时,调用程序中的所有代码流都会暂停,然后在对话框不再可见时恢复。一旦发生这种情况,让调用程序查询对话框很容易,因为你会准确地知道代码中会出现这种情况。

  • 你要避免过多地显示不同的内容应用程序中的窗口,因为它可能很快让用户烦恼。这里和那里有一些对话框可以,特别是如果您需要以模态方式提供信息,但通常最好在需要时交换GUI视图,而CardLayout对此有好处。

  • 但话虽如此,单独的视图通常由不同的类创建,因此来回传递信息的问题仍然是上述类似解决方案的问题。

  • Passing information from one object to another is no different with Swing as with other Java programs. You can call methods or constructors and pass information in via parameters.
  • A key difference though is when to pass information. With event driven programs, this is often triggered by an event, a listener, and so use of the observer design pattern is comment.
  • For your purposes, the first window could be a modal dialog such as a JOptionPane or a modal JDialog which will make it easier to figure out when to pass information. When using a modal dialog, all code flow in the calling program is paused while the dialog is visible, and then resumes once the dialog is no longer visible. It's easy then to have the calling program query the dialog once this occurs, because you'll know precisely where in your code this will occur.
  • You'll want to avoid excessive showing of different windows in your application as it can quickly get annoying to the user. A few dialogs here and there are OK, especially if you need the information to be given in a modal fashion, but in general it's better to swap GUI "views" when needed, and a CardLayout is good for this.
  • But having said this, separate views are often created by separate classes, so the problem of passing information back and forth remains a problem with similar solutions as described above.

具体来说,给你的Enter类一个getText方法,允许其他对象查询它的JTextField的状态:

Specifically, give your Enter class a getText method that will allow other objects to query it for the state of its JTextField:

public String getTxtOneText() {
  return txtOne.getText();
}

此外,更改ActualGame类,以便在需要时可以接受字符串信息:

Also, change your ActualGame class so that it can accept String information when needed:

class ActualGame extends JPanel {

   private JLabel lblOne = new JLabel();

   public ActualGame(String text) {
      lblOne.setText(text);
      setLayout(new FlowLayout());
      add(lblOne);

   }

   public void setLblOneText(String text) {
      lblOne.setText(text);
   }
}






例如,


e.g.,

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

public class Foo {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            ActualGame actualGame = new ActualGame("");
            Main main = new Main(actualGame);
            main.pack();
            Enter enter = new Enter(main);
            enter.setVisible(true);

            actualGame.setLblOneText(enter.getTxtOneText());
            main.pack();
            main.setLocationRelativeTo(null);
            main.setVisible(true);
         }
      });
   }
}

class Enter extends JDialog implements ActionListener {

   private String one = "";
   private JTextField txtOne = new JTextField(10);
   private JButton enter = new JButton("Enter");

   public Enter(JFrame frame) {
      super(frame, "Welcome", true);
      this.setLayout(new FlowLayout());

      enter.addActionListener(this);
      txtOne.addActionListener(this);

      add(txtOne);
      add(enter);
      pack();
      setLocationRelativeTo(null);

      // this has to be done last
      // setVisible(true);
   }

   public String getTxtOneText() {
      return txtOne.getText();
   }

   public void actionPerformed(ActionEvent e) {
      setVisible(false);
   }
}

class Main extends JFrame {
   ActualGame actualGame;

   public Main(ActualGame actualGame) {
      super("Main");
      this.actualGame = actualGame;
      add(actualGame);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

}

class ActualGame extends JPanel {

   private JLabel lblOne = new JLabel();

   public ActualGame(String text) {
      lblOne.setText(text);
      setLayout(new FlowLayout());
      add(lblOne);

   }

   public void setLblOneText(String text) {
      lblOne.setText(text);
   }
}

这篇关于在java中使用其他类中的变量从其他文件到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆