java boxlayout组合

时间:2010-09-28 18:59:45

标签: java swing layout

自从我开始使用Swing以来,我还没有理解这一点。为什么BoxLayout与Y轴排序(从示例中)按钮一个在另一个下面,一直到面板的末端(这就是我认为它是如何工作的),但放置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();
    }
}

1 个答案:

答案 0 :(得分:3)

JButton的默认最大大小相当于首选大小(基于按钮中的文本/图标),这意味着它指示LayoutManager不要将其大小设置为大于正常大小。

JPanel的默认最大大小为32767/32767,这意味着它可以占用您提供的任何空间。

你可以看看这个snippit:

    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()显式设置两者的最大大小。