【发布时间】:2013-12-30 16:43:22
【问题描述】:
我正在尝试了解有关创建更多动态 GUI 的更多信息。我希望添加具有不同内容的不同面板,并且当您按下一个主面板上的按钮时,它会更改相邻面板。我添加了两个面板和一些按钮,当我测试程序时,它显示正确。问题是当我添加JTextField(或JTextArea)时,面板是空白的并且没有按钮。奇怪的是我没有将JTextField 添加到任一面板中。我只创建了一个全局变量。如果我将其注释掉,则程序可以正常运行。我错过了一些非常简单的东西吗?
这是gameWindow 类,它具有JTextField
package rpgcreator;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class gameWindow extends JPanel {
JPanel startWindowPanel;
JPanel settingsPanel;
JPanel characterPanel;
JPanel scenarioPanel;
JPanel mapPanel;
JButton CharacterButton = new JButton("Create your character");
JButton StoryButton = new JButton("Choose your Story line");
JButton MapButton = new JButton("Choose your World");
//JTextField nameField = new JTextField(15); //comment or uncomment to see issue
public gameWindow() {
setLayout(new GridLayout(0,2,5,0));
startWindowPanel = new JPanel(new FlowLayout());
settingsPanel = new JPanel(new GridLayout(2,1));
startWindowPanel.setBackground(Color.blue);
settingsPanel.setBackground(Color.black);
startWindowPanel.add(MapButton);
startWindowPanel.add(StoryButton);
startWindowPanel.add(CharacterButton);
add(startWindowPanel);
add(settingsPanel);
}
}
这里是主要的
package rpgcreator;
import javax.swing.JFrame;
public class RPGCreator extends JFrame{
private static void mainWindow(){
RPGCreator mainwindow = new RPGCreator();
mainwindow.setSize(1200, 800);
mainwindow.setResizable(false);
mainwindow.setLocationRelativeTo(null);
mainwindow.setTitle("RPG Creator");
mainwindow.setVisible(true);
mainwindow.add(new gameWindow());
mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO code application logic here
mainWindow();
}
}
【问题讨论】:
-
等等,你到底在哪里添加文本字段/文本区域?我看到了注释掉的文本字段,但我没有看到您实际添加它的位置。
-
我还没有将 JtextField 添加到任何面板中。我只创建了变量,如果我不加注释,它会清除我的面板。这就是为什么我被卡住并且似乎无法弄清楚发生了什么。
-
尝试移动可见性代码,看我的回答。看看有没有帮助
标签: java swing jframe jpanel jtextfield