我有以下问题:我的游戏中有2个课程 - CONFIGUREGAME(CG)和ROULETTETABLE(RT) - 用户可以在课程CG中指定游戏的详细信息,如他的名字或金钱。在类RT中,我希望来自类CG的JTextField的输入显示在RT类的按钮上。
这是我的代码(我简化了很多):
public class CONFIGUREGAME extends JFrame implements ActionListener
{
Jframe frame = new JFrame("...");
public JTextField playername1 = new JTextField();
public JButton startgame = new JButton();
public CONFIGUREGAME()
{
startgame.addActionListener(this);
}
public static void main(String (String[] args)
{
new CONFIGUREGAME();
}
public void actionPerformed(ActionEvent aEvt)
{
if(aEvt.getSource()==startgame)
{
frame.dispose();
new ROULETTETABLE();
}
}
现在是第2课:
import ...;
public class ROULETTETABLE extends CONFIGUREGAME implements ActionListener
{
public player1 = new JButton();
public ROULETTETABLE()
{
String Strplayername1 = playername1.getText();
player1.setText(Strplayername1);
}
public static void main(String (String[] args)
{
new ROULETTETABLE();
}
}
我尝试了各种应该有所帮助的方法,但他们却没有。我的用户界面工作得非常好,所以如果它出现了错误,那是因为我弄错了它。 我非常感谢你的帮助!
答案 0 :(得分:1)
你需要这样的东西。
public class CONFIGUREGAME extends JFrame implements ActionListener
{
Jframe frame = new JFrame("...");
public JTextField playername1 = new JTextField();
public JButton startgame = new JButton();
public CONFIGUREGAME()
{
startgame.addActionListener(this);
}
public static void main(String (String[] args)
{
new CONFIGUREGAME();
}
public void actionPerformed(ActionEvent aEvt)
{
if(aEvt.getSource()==startgame)
{
frame.dispose();
new ROULETTETABLE(playername1.getText());
}
}
}
public class ROULETTETABLE extends CONFIGUREGAME implements ActionListener
{
public JButton player1 = new JButton();
public ROULETTETABLE(String playerName)
{
player1.setText(playerName);
}
public static void main(String (String[] args)
{
new ROULETTETABLE();
}
}
P.S。请学习Java方法和类符号。 CapitalizedClassName
,firstWordLowercaseMethodName
,firstWordLowercaseVariableName
,UPPER_CASE_CONSTANT_NAME
答案 1 :(得分:0)
将结果CONFIGUREGAME
传递给ROULETTETABLE
public class CONFIGUREGAME extends JFrame implements ActionListener {
//Jframe frame = new JFrame("..."); WHY?
//...
public void actionPerformed(ActionEvent aEvt) {
if (aEvt.getSource() == startgame) {
frame.dispose();
new ROULETTETABLE(playername1.getText());
}
}
public class ROULETTETABLE extends JFrame /* CONFIGUREGAME why? */implements ActionListener
{
public JButton player1 = new JButton();
public ROULETTETABLE(String playerName)
{
player1.setText(playerName);
}
}
我不喜欢这个,因为它将CONFIGUREGAME
类与ROULETTETABLE
使用JDialog
收集配置信息,然后将其传递给ROULETTETABLE
类
首先,重新配置一些类。作为一般规则,避免从像JFrame
这样的顶级类扩展,它们将代码耦合到单个访问点并降低其灵活性并重新使用
public class Roulettetable extends JPanel implements ActionListener {
private JButton player1 = new JButton();
public Roulettetable(String name) {
player1.setText(name);
}
}
public class ConfigureGame extends JPanel {
private JTextField playername1 = new JTextField();
public ConfigureGame() {
}
public String getPlayerName() {
return playername1.getText();
}
}
然后你完全把它包起来......
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
ConfigureGame configureGame = new ConfigureGame();
JOptionPane.showOptionDialog(null, configureGame, "Configure Game", JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[] {"Start"}, 0);
String name = configureGame.getPlayerName();
Roulettetable roulettetable = new Roulettetable(name);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(roulettetable);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
这个例子非常简单,它只是使用JOptionPane
来显示对话框。
有关详细信息,请查看How to Make Dialogs