你好同事!JButton没有显示在JFrame中
JButton应该能够显示在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);
}
}
如果你读你得到它提供了一个小的洞察力,以您的问题异常:“增加一个窗口添加到容器”。 IE,你正在将'JFrame'添加到你的'JButton'而不是其他的方式。 – Jeffrey 2012-02-14 23:36:43
下次在询问之前实际上试图找出问题。 – Jimmt 2012-02-14 23:49:15