JButton没有出现在JFrame中

Nic*_*ong -2 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.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)
Run Code Online (Sandbox Code Playgroud)

码:

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

    }


}
Run Code Online (Sandbox Code Playgroud)

dan*_*dev 6

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