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

前端 未结 2 1831
余生分开走
余生分开走 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: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);
       }
    }
    }
    

提交回复
热议问题