我的边框布局有4个面板,分别是北,南,东和西。例如在东边,我有一个带有四个按钮的jpanel。我的问题是所有按钮都与顶部对齐,而我想与中心对齐。例如在css中,诸如margin top或top:50%之类的东西。
有任何想法吗?
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");
最佳答案
您需要更改contentor3
的布局。尝试类似的方法:
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);