Hello其他程序员!
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);
}
}
答案 0 :(得分:6)
您需要将按钮添加到框架中,尝试gameFrame.add(gameButton);
答案 1 :(得分:4)
您需要添加按钮到框架。
例如gameFrame.add(gameButton);
答案 2 :(得分:4)
将其添加到面板中,否则它将无法显示。 gameFrame.add(gameButton);
答案 3 :(得分:0)
您必须向框架或面板添加按钮:例如JFrame.add(gameButton);