Use variables from other class from other file to another in java

前端 未结 2 1832
余生分开走
余生分开走 2021-01-26 14:09

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

相关标签:
2条回答
  • 2021-01-26 14:30

    Suggestions:

    • 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.

    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();
    }
    

    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);
       }
    }
    
    0 讨论(0)
  • 2021-01-26 14:32

    Try to make ActualGam as a underclass of Enter

    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 JTextField txtOne = 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);
    
        enter.addActionListener(this);
    
    }
    
    public void actionPerformed(ActionEvent e) {
        Main main = new Main();
        this.setVisible(false);
        one = txtOne.getText();
    
    }
    class ActualGame extends JPanel{
    
       private JLabel lblOne = new JLabel(one);
    
       public ActualGame() {
        setLayout(new FlowLayout());
        Enter.this.add(lblOne);
       }
    }
    }
    
    0 讨论(0)
提交回复
热议问题