Hello, I am trying to make a snake program but I'm having some trouble with the main screen. I am trying to make the Snake title, a start button, and a back button, but the Start button seems to be taking up the whole page and I can't figure out why. I am also trying to use Box Layout to create vertical boxes but that's not working either. My code is attached below:
Any help would be appreaciated as to why this is happening.
It is usually a bad idea to make your classes extend display classes. You don't need a subtype of JFrame, but a JFrame instance to add the other components to.It looks like a layout problem. You are adding one component only (the button) so it will occupy the entire “box”. Please check; you appear to be adding two action listeners doing the same thing.
I suggest you learn how to use a λ rather than an anonymous class (see the Java™ Tutorials).
I don't like the idea of having a frame which disappears and then another frame. Consider a dialogue window instead of the first frame. Dialogues are designed to appear and disappear.
The problem was probably caused because by default, the content pane of a JFrame uses BorderLayout and adding a component to a BorderLayout without specifying a constraint will place it in the center, blowing up the component until it takes up all unused space.
Using BoxLayout is a decent solution, but you did it in the wrong way. You specified this as the target container, but really the content pane of the JFrame is the target container, not the JFrame itself. This would have fixed it:
There is an easier way however:
You have other issues in your code:
You are running UI code on the main thread. Initialize and call your UI using SwingUtilities.invokeLater().
Class names should start with a capital letter.
Don't extend JFrame. Add a field of type JFrame to your class.
Never call pack() or setVisible() from a constructor. Call them from a separate method after the form is created.
Why are you calling setLayout(null) on your buttons?
You set the layout manager twice.
Name your custom frame something more descriptive than Frame. I recommend something like SnakeGui (which also shouldn't extend JFrame).
Instead of adding anonymous action listeners in your constructor, declare Action implementations as fields in your class.
posted 2 weeks ago
Thanks to everyone who replied. I ended up using the GridBag layout which worked better in my case, since my two buttons were different sizes. Now I'm just having some troubles linking together the JFrames to the main Frame once 'Start' is pressed, but I should be able to figure that out soon!
Stephan van Hulst
posted 2 weeks ago
Hey Mona, I don't intend to "mow the grass from under your feet" as we say in Dutch, but a few days ago I wrote some example code to help you out. At the time I thought it was too much to post here, but you may have advanced enough that it can inspire you to solve the remaining problems in your own code:
Skool. Stay in. Smartness. Tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps