自从我开始使用Swing以来,我还不了解这一点。为什么具有Y轴排序功能的BoxLayout(从示例中)一个接一个地按一个按钮一直到面板的末尾(这就是我认为的工作方式),但是却以相同的方式放置JPanels却使它们伸展。有人会这样对我解释一下吗? (是的,示例代码已生成,但是那是我什至现在都没有写的:-))
public class MyFrame extends JFrame {
private JButton jButton1;
private JButton jButton2;
private JLabel jLabel1;
private JLabel jLabel10;
private JLabel jLabel11;
private JLabel jLabel12;
private JLabel jLabel13;
private JLabel jLabel14;
private JLabel jLabel15;
private JLabel jLabel16;
private JLabel jLabel17;
private JLabel jLabel18;
private JLabel jLabel19;
private JLabel jLabel2;
private JLabel jLabel20;
private JLabel jLabel21;
private JLabel jLabel22;
private JLabel jLabel23;
private JLabel jLabel24;
private JLabel jLabel25;
private JLabel jLabel26;
private JLabel jLabel27;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel jLabel7;
private JLabel jLabel8;
private JLabel jLabel9;
private JPanel jPanel1;
private JPanel jPanel2;
private JPanel jPanel3;
private JPanel jPanel4;
private JScrollPane jScrollPane1;
private JScrollPane jScrollPane2;
public MyFrame() {
buildGUI();
setVisible(true);
}
private void buildGUI() {
jScrollPane1 = new JScrollPane();
jPanel4 = new JPanel();
jPanel2 = new JPanel();
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel10 = new JLabel();
jLabel9 = new JLabel();
jLabel8 = new JLabel();
jLabel7 = new JLabel();
jLabel6 = new JLabel();
jLabel27 = new JLabel();
jPanel3 = new JPanel();
jLabel11 = new JLabel();
jLabel12 = new JLabel();
jLabel13 = new JLabel();
jLabel14 = new JLabel();
jLabel15 = new JLabel();
jLabel16 = new JLabel();
jLabel17 = new JLabel();
jLabel18 = new JLabel();
jLabel19 = new JLabel();
jLabel20 = new JLabel();
jLabel21 = new JLabel();
jLabel22 = new JLabel();
jLabel26 = new JLabel();
jLabel25 = new JLabel();
jLabel24 = new JLabel();
jLabel23 = new JLabel();
jScrollPane2 = new JScrollPane();
jPanel1 = new JPanel();
jButton1 = new JButton();
jButton2 = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jPanel4.setLayout(new BoxLayout(jPanel4, BoxLayout.Y_AXIS));
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
jLabel1.setText("jLabel1");
jPanel2.add(jLabel1);
jLabel2.setText("jLabel2");
jPanel2.add(jLabel2);
jLabel3.setText("jLabel3");
jPanel2.add(jLabel3);
jLabel4.setText("jLabel4");
jPanel2.add(jLabel4);
jLabel5.setText("jLabel5");
jPanel2.add(jLabel5);
jLabel10.setText("jLabel10");
jPanel2.add(jLabel10);
jLabel9.setText("jLabel9");
jPanel2.add(jLabel9);
jLabel8.setText("jLabel8");
jPanel2.add(jLabel8);
jLabel7.setText("jLabel7");
jPanel2.add(jLabel7);
jLabel6.setText("jLabel6");
jPanel2.add(jLabel6);
jLabel27.setText("jLabel27");
jPanel2.add(jLabel27);
jPanel4.add(jPanel2);
jLabel11.setText("jLabel11");
jPanel3.add(jLabel11);
jLabel12.setText("jLabel12");
jPanel3.add(jLabel12);
jLabel13.setText("jLabel13");
jPanel3.add(jLabel13);
jLabel14.setText("jLabel14");
jPanel3.add(jLabel14);
jLabel15.setText("jLabel15");
jPanel3.add(jLabel15);
jLabel16.setText("jLabel16");
jPanel3.add(jLabel16);
jLabel17.setText("jLabel17");
jPanel3.add(jLabel17);
jLabel18.setText("jLabel18");
jPanel3.add(jLabel18);
jLabel19.setText("jLabel19");
jPanel3.add(jLabel19);
jLabel20.setText("jLabel20");
jPanel3.add(jLabel20);
jLabel21.setText("jLabel21");
jPanel3.add(jLabel21);
jLabel22.setText("jLabel22");
jPanel3.add(jLabel22);
jLabel26.setText("jLabel26");
jPanel3.add(jLabel26);
jLabel25.setText("jLabel25");
jPanel3.add(jLabel25);
jLabel24.setText("jLabel24");
jPanel3.add(jLabel24);
jLabel23.setText("jLabel23");
jPanel3.add(jLabel23);
jPanel4.add(jPanel3);
jScrollPane1.setViewportView(jPanel4);
jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));
jButton1.setText("jButton1");
jPanel1.add(jButton1);
jButton2.setText("jButton2");
jPanel1.add(jButton2);
jScrollPane2.setViewportView(jPanel1);
GroupLayout layout = GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(24, 24, 24)
.add(jScrollPane1, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE)
.add(37, 37, 37)
.add(jScrollPane2, GroupLayout.PREFERRED_SIZE, 244, GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(GroupLayout.TRAILING, layout.createSequentialGroup()
.add(21, 21, 21)
.add(layout.createParallelGroup(GroupLayout.TRAILING)
.add(GroupLayout.LEADING, jScrollPane2, GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
.add(GroupLayout.LEADING, jScrollPane1, GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
.add(40, 40, 40))
);
pack();
}
public static void main(String args[]) {
new MyFrame();
}
}
最佳答案
JButton的默认最大大小等于首选大小(基于按钮中的文本/图标),这意味着它指示LayoutManager使其大小不超过其正常大小。
JPanel的默认最大大小为32767/32767,这意味着可以很好地占用您提供的几乎所有空间。
您可以使用以下代码段看一下:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for ( int i = 0; i < 5; i++ ) {
JComponent comp = new JPanel();
// JComponent comp = new JButton("Button " + i);
System.out.printf("Pref: %s Max: %s%n", comp.getPreferredSize(), comp.getMaximumSize());
comp.setBackground(new Color(100 + i * 30, 0, 0));
panel.add(comp);
}
两者的最大大小可以使用
setMaximumSize()
明确设置。