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);
}
}
您需要将按钮添加到框架中,请尝试gameFrame.add(gameButton);
you need to add button to frame. such as gameFrame.add(gameButton);
Add it to the panel otherwise it won't show up, ever. gameFrame.add(gameButton);
您必须将按钮添加到框架或面板:例如JFrame.add(gameButton);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.