JPanel没有在JFrame中显示 - Java [英] JPanel not displaying in JFrame - Java

查看:261
本文介绍了JPanel没有在JFrame中显示 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器是我制作的扩展JFrame的类。

Server is a class I made that extends 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.

我刚刚记得在使用其他'项目'和我的解决方案之前遇到这个问题是为了使窗口可调整大小。每当我调整它的大小时,组件就会显示出来。

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?

谢谢。

编辑:

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);

这会有帮助吗?

推荐答案

问题在你提供的代码中并不明显。

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

听起来你想要一些 pack() setSize(int,int) setExtendedState(int)和/或 setResizable( boolean)调用之前的方法 setVisible(true)

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).

编辑:

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.

使用Swing的事情是你自动使用多线程代码。虽然初始化主线程上的控件通常是安全的,但是一旦你导致事件调度线程启动(因为 setVisible(true)肯定会所有投注都已关闭。

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 thread to start (as setVisible(true) will surely do), all bets are off.

尽可能延迟调用 setVisible(true)。最好不要在你的 JFrame 构造函数中调用它。

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

如果你需要在你之后修改Swing控件已经启动了您的应用程序,您需要通过事件派发线程来执行此操作(请参阅 invokeLater SwingUtilities <中的invokeAndWait 方法/ a>,等等。)

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 invokeLater and invokeAndWait methods in SwingUtilities, among others).

这篇关于JPanel没有在JFrame中显示 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆