我有一个JFrame
的子类,并在其中有下面的布局。我有一个大panel
和一个小buttonsPanel
与两个JButtons
。我将按钮添加到较小的面板并将该面板添加到第一个面板。按钮应该居中,但它不会发生。在嵌套JPanel中心对齐JButton(BoxLayout)
panel=new JPanel();
add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button1=new JButton("button1");
JButton button2=new JButton("button2");
buttonsPanel=new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(button1, CENTER_ALIGNMENT);
buttonsPanel.add(button2, CENTER_ALIGNMENT);
panel.add(buttonsPanel, BorderLayout.CENTER);
我该怎么办?
应该垂直居中还是水平居中? – VGR
@VGR,Horizontaly。 – parsecer