0

I've been working on this for a bit and for the life of me can't get anywhere. I'm trying to set the buttons and labels to set on top of one another rather than side by side. I figure I have to change the layout form FlowLayout to BoxLayout but I can't figure out how to do that. This is what I have so far.

import javax.swing.JFrame;

public class VoteCounter
{
    //----------------------------------------------
    // Creates the main program frame.
    //----------------------------------------------
    public static void main(String[] args)
{
    JFrame frame = new JFrame("Vote Counter");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new VoteCounterPanel());
    frame.pack();
    frame.setVisible(true);
}
}

Any help would be greatly appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Joe Lucas
  • 21
  • 2
  • 3
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jan 21 '15 at 13:51

2 Answers2

1
frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));

quite simple.

For further information, please refer to: http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

X-Fate
  • 323
  • 4
  • 13
1

The default Layout of JPanel is FlowLayout. You can change the panel's Layout like:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(null,BoxLayout.Y_AXIS));

Here is an example to use BoxLayout:

choosing the best Layout for this jframe

Also, you can use BoxLayout to set white space:

https://stackoverflow.com/a/22525005/3378204

Hope that it will help you.

Community
  • 1
  • 1
Eugene
  • 10,627
  • 5
  • 49
  • 67