【问题标题】:JTextField is blanking out my JPanelsJTextField 正在清除我的 JPanels
【发布时间】: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


【解决方案1】:

setVisible 应该放在最后。您当前将可见设置为 true,然后添加面板。

mainwindow.setVisible(true);
mainwindow.add(new gameWindow());

setVisible放在setDeaultCLoseOperation之后的末尾

【讨论】:

  • +1 代码对我有用,但你说的是一个大道理,我认为这就是问题所在
  • 啊,所以当组件添加到框架时,框架是可见的。调用validate() 也可以解决问题,因此我建议使用pack() 也有效。
【解决方案2】:

我不完全确定它为什么会这样,也许其他人可以解释一下。

我所知道的是,我通常会打电话给pack(),这似乎让你的问题消失了。

private static void mainWindow(){
    final RPGCreator mainwindow = new RPGCreator();
    mainwindow.setMinimumSize(new Dimension(1200, 800));
    mainwindow.setResizable(false);
    mainwindow.setTitle("RPG Creator");
    mainwindow.setVisible(true);
    mainwindow.add(new gameWindow());
    mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainwindow.pack(); //This usually goes after you've added all of your components
    mainwindow.setLocationRelativeTo(null);
}

一些注意事项:

  1. 我不得不更改为mainwindow.setMinimumSize(new Dimension(1200, 800)); 以避免框架看起来被压扁。虽然我通常会让布局管理器处理事物的大小。
  2. 在调用pack()之后再调用setLocationRelativeTo(null),以达到想要的效果。再次不知道为什么,但我通过一些困难学会了这一点。

【讨论】:

  • 谢谢你,丹。我很困惑是什么导致了这个问题
  • 查看@peeskillet 的回答。我可能也会选择他作为选择的答案。它触及了问题的根源。基本上,方法调用的顺序很重要。您在添加组件之前使 JFrame 可见,因此 Swing 有点混乱。调用 pack() 会强制 JFrame 重新渲染,从而使其恢复正常。最终,您可以更改方法调用的顺序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 2013-10-11
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2012-02-10
相关资源
最近更新 更多