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里?
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里?
解决方案 »
- 准备再学习一遍Java 求大神推荐本书!
- 请教关于HashMap的问题
- 我的AppleT不能显示,怎么了?急急急,望大侠指教!
- 从文件中读取所有的学生对象,怎么搞?求救。我的代码如下
- Java“出血”者
- 我正要学java,现在项目中要在java下实现打印的功能有关图像文件的,我要看什么书?很急
- java中时间转换问题!(在线)
- 本论坛的"将贴子提前"功能是怎么实现的?是不是先删除,然后再插入?
- 如何给一个多维数组赋值(不要在出始化定义的时候)?String[][] a;
- 输入本机IP,连接不成功,改成127.0.0.1才可以
- 嗯一切为了金钱,,,找一个人气点的J2EE群,,,
- String a="1"; a.getbyte()的第一个字节和 (byte)'1' 一样吗?
在子窗口按钮的actionPerformed方法理调用这个方法,首先创建子窗口是要保留父窗口的引用。
比如把构造方法加个参数 JFrame parent
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);//
}
});