Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton未显示在JFrame中_Java_Swing_Jframe_Jbutton - Fatal编程技术网

Java JButton未显示在JFrame中

Java JButton未显示在JFrame中,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,各位程序员好 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.ad

各位程序员好

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)
代码:


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

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

将其添加到面板中,否则它永远不会显示。
游戏框架。添加(游戏按钮)

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

如果您阅读了获得的异常,它会为您的问题提供一些见解:“向容器添加窗口”。也就是说,你正在将你的
JFrame
添加到你的
JButton
中,而不是以相反的方式。下一次,在询问之前,试着找出问题所在。
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);

    }


}