jpanel -java内部垂直jbutton

anv*_*nvd 3 java swing center jbutton

我有一个边框布局,有4个面板,北,南,东,西.例如在东侧我有一个jpanel,它有四个按钮.我的问题是所有按钮都对齐到顶部,我想对齐中心.在css中,例如保证金顶部或顶部: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");
Run Code Online (Sandbox Code Playgroud)

Ste*_*rie 9

你需要改变布局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);
Run Code Online (Sandbox Code Playgroud)