This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Java Swing: Buttons too big

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67036
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 11017
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  •  
    Mona Smith
    Greenhorn
    Posts: 19
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Saloon Keeper
    Posts: 11017
    243
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!