center vertically jbutton inside jpanel -java
I have a border layout with 4 panels, north, south, east and west. For example in e开发者_开发问答ast side I have a jpanel which have four buttons. My problem is that all buttons are align to the top and I want align to the center. In css for example something like margin top or top:50%.
Any ideas?
JPanel contentor3 = new JPanel();
contentor.add(contentor3, BorderLayout.EAST);
contentor3.setBackground(Color.green);
contentor3.setPreferredSize(new Dimension(120, 750));
contentor3.add(btn[1]);
btn[1].setText("btn1");
You need to change the layout of contentor3
. Try something like:
contentor3.setLayout (new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints ();
// next two lines will place the components top-to-bottom, rather than left-to-right
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
// add as many buttons as you want in a column
contentor3.add (new JButton ("btn1"), gbc);
contentor.add (contentor3, BorderLayout.EAST);
精彩评论