public class GameBoardGUI extends JFrame {
        private JButton newGameButton = null;
        private JTextField player1TextField = null;
        public String playerName;
PlayersNameGUI name = new PlayersNameGUI(playerName);
      
        public GameBoardGUI() {
super();
initialize();
}        public JTextField getPlayer1TextField() {
if (player1TextField == null) {
player1TextField = new JTextField();
player1TextField.setText("Player 1 Name");
player1TextField.setEditable(false);
}
return player1TextField;
}        private JButton getNewGameButton() {
if (newGameButton == null) {
newGameButton = new JButton();
newGameButton.setText("New Game");
newGameButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
name.setVisible(true);

// initializes everything that has been through when it is pressed 
// TODO Auto-generated Event stub actionPerformed()
}
});
}
return newGameButton;
}
}下面是子窗口。public class PlayersNameGUI extends JFrame {
      private JTextField player2TextField = null;
      private JButton exitButton = null;
      
      public PlayersNameGUI(String playerName) {
super();
initialize();

} /**
 * This method initializes this
 * 
 * @return void
 */
public void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("Players' Name");
this.setVisible(false);
}        public JTextField getPlayer2TextField() {
if (player2TextField == null) {
player2TextField = new JTextField();
player2TextField.setText("Player 2");
}
return player2TextField;
}        private JButton getSaveButton() {
if (saveButton == null) {
saveButton = new JButton();
saveButton.setText("save");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String playerName = player2TextField.getText();
// TODO Auto-generated Event stub actionPerformed()
}
});
}
return saveButton;

}
}怎样才能将player2TextField.getText() 的值传到 父窗口的player1TextField里?

解决方案 »

  1.   

    给你一种方法:在主窗口类里写一个方法 setPlayer2Name(String name);当然,要有个成员变量保存这个属性
    在子窗口按钮的actionPerformed方法理调用这个方法,首先创建子窗口是要保留父窗口的引用。
    比如把构造方法加个参数 JFrame parent
      

  2.   

    在父窗口里定義一個變量   用來保存player2TextField.getText() 的值  就可以了呀~
      

  3.   

    GameBoardGUI 中写一个sePplayer1TextFieldText(String text){//
    player1TextField.setText(text);//
    }方法 
    PlayersNameGUI name = new PlayersNameGUI(playerName,this); //PlayersNameGUI 中的构造方法中加一个父窗口实例的参数.private GameBoardGUI gameBoardGUI= null; //
           
          public PlayersNameGUI(String playerName, GameBoardGUI gameBoardGUI) {// 
               this.gameBoardGUI=gameBoardGUI;//
               super(); 
               initialize(); 
    } 再在你的事件中调用gameBoardGUI的sePplayer1TextFieldText;
    saveButton.addActionListener(new java.awt.event.ActionListener() { 
    public void actionPerformed(java.awt.event.ActionEvent e) { 
    String playerName = player2TextField.getText(); 
    gameBoardGUI.sePplayer1TextFieldText(playerName);//

    });