我有一個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