JPanel 未在 JFrame 中显示 - Java

发布于 2024-08-20 01:22:07 字数 1628 浏览 9 评论 0 原文

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

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

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

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

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

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

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

帮助!有谁知道为什么会发生这种情况?

谢谢。

编辑:

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

这会有帮助吗?

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨光如昨 2024-08-27 01:22:08

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

听起来您想要 pack(), setSize(int,int), setExtendedState(int) 和/或 setResizes(boolean) 方法awt/Window.html#setVisible(boolean)" rel="nofollow noreferrer">setVisible(true)。


编辑:

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

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

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

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

如果您在启动应用程序后需要修改 Swing 控件,则需要通过事件调度线程来完成此操作(请参阅 invokeLater SwingUtilities 中的 invokeAndWait 方法等)。

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


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

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

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

≈。彩虹 2024-08-27 01:22:08

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

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.

与之呼应 2024-08-27 01:22:08

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文