JButton 未显示在 JFrame 中

发布于 2025-01-05 19:40:37 字数 1382 浏览 3 评论 0原文

各位程序员大家好!

JButtons 应该能够显示在 JFrame 中吗?我在 JButton 上使用了 setVisible 方法,但它不会出现。

错误消息:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at javax.swing.AbstractButton.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at FrameTest.initializeGameFrame(FrameTest.java:27)
    at FrameTest.main(FrameTest.java:17)

代码:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class FrameTest extends JFrame{

    private static final int gameWindowHeight = 700;
    private static final int gameWindowLength = 700;

    /** Set up frame for game window
     * 
     */

    public static void main(String[] args)
    {
        FrameTest.initializeGameFrame();

    }

    public static void initializeGameFrame()
    {
        FrameTest gameFrame = new FrameTest();
        gameFrame.setSize(gameWindowLength, gameWindowHeight);
        gameFrame.setTitle("Frame Test- by Me");
        JButton gameButton =  new JButton("Start Game");
        gameButton.add(gameFrame);
        gameButton.setLocation(250, 250);
        gameButton.setVisible(true);
        gameFrame.setVisible(true);

    }


}

Hello fellow programmers!

Are JButtons supposed to be able to show up in JFrame? I used the setVisible method on JButton but it would not appear.

Error Message:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at javax.swing.AbstractButton.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at FrameTest.initializeGameFrame(FrameTest.java:27)
    at FrameTest.main(FrameTest.java:17)

Code:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class FrameTest extends JFrame{

    private static final int gameWindowHeight = 700;
    private static final int gameWindowLength = 700;

    /** Set up frame for game window
     * 
     */

    public static void main(String[] args)
    {
        FrameTest.initializeGameFrame();

    }

    public static void initializeGameFrame()
    {
        FrameTest gameFrame = new FrameTest();
        gameFrame.setSize(gameWindowLength, gameWindowHeight);
        gameFrame.setTitle("Frame Test- by Me");
        JButton gameButton =  new JButton("Start Game");
        gameButton.add(gameFrame);
        gameButton.setLocation(250, 250);
        gameButton.setVisible(true);
        gameFrame.setVisible(true);

    }


}

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

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

发布评论

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

评论(4

小镇女孩 2025-01-12 19:40:37

您需要将按钮添加到框架中,尝试 gameFrame.add(gameButton);

You need to add the button to the frame, try gameFrame.add(gameButton);

幻梦 2025-01-12 19:40:37

您需要将按钮添加到框架中。
gameFrame.add(gameButton);

you need to add button to frame.
such as gameFrame.add(gameButton);

抱猫软卧 2025-01-12 19:40:37

将其添加到面板中,否则它永远不会显示。
gameFrame.add(gameButton);

Add it to the panel otherwise it won't show up, ever.
gameFrame.add(gameButton);

遗忘曾经 2025-01-12 19:40:37

您必须向框架或面板添加按钮:例如 JFrame.add(gameButton);

you must have to add button to frame or panel : for example JFrame.add(gameButton);

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