Reputation: 77
In my program, I need to have three buttons. I am using a BoxLayout
in the JPanel
, and I have managed to move them to the dead center of the screen. They are the correct size and are in the correct horizontal position, but I want to move them up to the top of my frame. What should I use to do this?
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Library extends JFrame {
private JFrame jf1;
private JPanel jp1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = (JPanel) jf1.getContentPane();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb2 = new JButton("Author");
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb3 = new JButton("Title");
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jp1.add(Box.createRigidArea(new Dimension(300, 0)));
jp1.add(jb1);
jp1.add(Box.createRigidArea(new Dimension(20,0)));
jp1.add(jb2);
jp1.add(Box.createRigidArea(new Dimension(20,0)));
jp1.add(jb3);
}
public static void main(String[] args) {
Library shoe = new Library();
}
}
Upvotes: 0
Views: 617
Reputation: 11
Use button_name.setAlignmentY(TOP_ALIGNMENT); // for all three buttons , Thus your code changes to:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Library extends JFrame {
private JFrame jf1;
private JPanel jp1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = (JPanel) jf1.getContentPane();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb1.setAlignmentY(TOP_ALIGNMENT);
jb2 = new JButton("Author");
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb2.setAlignmentY(TOP_ALIGNMENT);
jb3 = new JButton("Title");
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jb3.setAlignmentY(TOP_ALIGNMENT);
jp1.add(Box.createRigidArea(new Dimension(300, 0)));
jp1.add(jb1);
jp1.add(Box.createRigidArea(new Dimension(20,0)));
jp1.add(jb2);
jp1.add(Box.createRigidArea(new Dimension(20,0)));
jp1.add(jb3);
}
public static void main(String[] args) {
Library shoe = new Library();
}
Upvotes: -1
Reputation: 324118
but I want to move them up to the top of my frame.
By default the content pane of the frame is a BorderLayout so I would keep this as the layout and then just create a panel for the buttons and add this panel to the top of the frame.
JPanel panel = new JPanel( new FlowLayout() );
panel.add(button1)
...
add(panel, BorderLayout.PAGE_START);
Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples.
I am using a BoxLayout in the JPanel,
However if you want to do it by using a BoxLayout then you have to tell the BoxLayout how to align each component vertically. The default value for a JButton is to align it in the center of the available space.
If you want it at the top then you need to change each button:
jb1.setAlignmentY(0.0f);
Also, don't hardcode the "rigid area" at the start of the panel. Instead you want to use Box.createHorizontalGlue()
at the start and end of the panel. Then the components will adjust as the width of the frame is changed.
Again, the tutorial has a section on How to Use BoxLayout
that will explain the above.
Upvotes: 3