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
Suggestions:
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);
}
}
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);
}
}
}