JPanel 未显示在 JFrame 中 - Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2167844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:48:26  来源:igfitidea点击:

JPanel not displaying in JFrame - Java

javaswingjframejpanel

提问by Dois

Server is a class I made that extends JFrame.

Server 是我创建的一个扩展 JFrame 的类。

    Server serverApp = new Server(TITLE, WIDTH, HEIGHT, true, false);

I've effectively removed almost all the other code but the problem still remains!

我已经有效地删除了几乎所有其他代码,但问题仍然存在!

    c = getContentPane();
    c.setLayout(new BorderLayout());

    //Components  /***AHHHHH***/
    lblEnterMessage = new JLabel("Enter Message ");
    txtEnterMessage = new JTextField(50);
    txtEnterMessage.addActionListener(this);
    btnSend = new JButton("Send");
    btnSend.addActionListener(this);
    taDisplay = new JTextArea("Test, test test.", 10, 0);
    taDisplay.setEditable(false);
    JScrollPane jspDisplay = new JScrollPane(taDisplay);

    pnlChatTop = new JPanel(new FlowLayout());
    pnlChatTop.add(lblEnterMessage);
    pnlChatTop.add(txtEnterMessage);
    pnlChatTop.add(btnSend);
    pnlChat = new JPanel(new BorderLayout());
    pnlChat.add(pnlChatTop, BorderLayout.CENTER);
    pnlChat.add(jspDisplay, BorderLayout.SOUTH);

    c.add(pnlChat, BorderLayout.CENTER);

Oh dang, it just suddenly WORKED... And I was about to remove this question but I ran it again a few times it and just randomly WORKS and doesn't WORK at times.

Oh dang,它突然起作用了......我正要删除这个问题,但我又运行了几次,只是随机起作用,有时不起作用。

I've just remembered having this problem before with other 'projects' and my solution was to make the window resizable. Whenever I simply resized it, the components would display.

我只记得之前在其他“项目”中遇到过这个问题,我的解决方案是调整窗口大小。每当我简单地调整它的大小时,组件就会显示出来。

This time, I'm making a game and I don't want it to be resizable... and I wanna know how to fix this problem for good and in the proper way anyway.

这一次,我正在制作一个游戏,我不希望它可以调整大小……我想知道如何以正确的方式永久地解决这个问题。

Help! Does anyone know why this is happening?

帮助!有谁知道为什么会这样?

Thanks.

谢谢。

Edit:

编辑:

public Server(String title, int sizeW, int sizeH, boolean visibility, boolean resizability) {

    /* Initialization */
    //JFrame settings
    setTitle(title);
    setSize(sizeW, sizeH);
    setVisible(visibility);
    setResizable(resizability);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(this);

Will that help?

那会有帮助吗?

回答by McDowell

The problem is not apparent from the code you have provided.

从您提供的代码来看,问题并不明显。

It sounds like you want some combination of the pack(), setSize(int,int), setExtendedState(int)and/or setResizable(boolean)methods prior to calling setVisible(true).

听起来您想要在调用setVisible(true)之前对pack()setSize(int,int)setExtendedState(int)和/或setResizable(boolean)方法进行某种组合。



Edit:

编辑:

setTitle(title);
setSize(sizeW, sizeH);
setVisible(visibility);
setResizable(resizability);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

There is a race condition in this code. Sometimes the main thread will get the components into the correct state to be painted before the frame displays; sometimes the frame wins and starts painting before everything is ready.

此代码中存在竞争条件。有时主线程会在框架显示之前让组件进入正确的状态进行绘制;有时框架获胜并在一切准备就绪之前开始绘画。

The thing about using Swing is that you are automatically working with multi-threaded code. Although it is generally safe to initialize controls on the main thread, once you cause the event dispatch threadto start (as setVisible(true)will surely do), all bets are off.

使用 Swing 的一点是您可以自动使用多线程代码。尽管在主线程上初始化控件通常是安全的,但是一旦您使事件调度线程启动(setVisible(true)肯定会这样做),所有赌注都将关闭。

Delay calling setVisible(true)as long as possible. Preferably, don't call it from within your JFrameconstructor.

setVisible(true)尽可能延迟通话。最好不要从JFrame构造函数中调用它。

If you need to modify Swing controls after you've kicked off your application, you'll need to do it via the event dispatch thread(see the invokeLaterand invokeAndWaitmethods in SwingUtilities, among others).

如果在启动应用程序后需要修改 Swing 控件,则需要通过事件调度线程来完成(参见SwingUtilities 中invokeLaterinvokeAndWait方法等)。

回答by trashgod

Intermittent failures of this sort suggest synchronization problems. Be sure you build and run your GUI on the EDT. In addition, you might like to see this very simple, ~100 line, GUI chat program.

此类间歇性故障表明同步问题。确保在EDT上构建和运行 GUI 。此外,您可能希望看到这个非常简单的、约 100 行、GUI聊天程序

回答by Vincent Ramdhanie

The call to setVisible is too early. It runs immediately and paints the window at the time that it is called. If you have not added all components to the Frame then they are not painted. That is why resizing the frame seems to make it appear. Because resizing causes a repaint to execute.

调用 setVisible 为时过早。它会立即运行并在调用时绘制窗口。如果您尚未将所有组件添加到框架中,则它们不会被绘制。这就是为什么调整框架大小似乎使它出现的原因。因为调整大小会导致重绘执行。

Make setVisible the last call in your JFrame's constructor.

使 setVisible 成为 JFrame 构造函数中的最后一个调用。