创建填充其父级的摇摆按钮

时间:2013-01-16 00:37:14

标签: java swing layout layout-manager windowbuilder

我在Eclipse Indigo中使用WindowBuilder来处理不同的布局。基本上我只是想彻底了解每个人如何工作以及哪个最适合不同的场景。下面的代码有效,但有一些我想调整的东西,但我不知道如何达到我想要的效果。

public class Dashboard extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Dashboard frame = new Dashboard();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Dashboard() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 602, 372);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel drawingPanel = new JPanel();
    drawingPanel.setBounds(6, 6, 487, 251);
    contentPane.add(drawingPanel);
    drawingPanel.setLayout(null);

    JScrollPane propertiesScrollPane = new JScrollPane();
    propertiesScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    propertiesScrollPane.setBounds(6, 264, 487, 80);
    contentPane.add(propertiesScrollPane);

    JPanel selectionPanel = new JPanel();
    propertiesScrollPane.setViewportView(selectionPanel);
    selectionPanel.setLayout(new BoxLayout(selectionPanel, BoxLayout.X_AXIS));

    JPanel surfacesPanel = new JPanel();
    selectionPanel.add(surfacesPanel);
    surfacesPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
    surfacesPanel.setLayout(new BoxLayout(surfacesPanel, BoxLayout.Y_AXIS));

    JCheckBox surfaceCheck = new JCheckBox("Visible");
    surfaceCheck.setHorizontalAlignment(SwingConstants.CENTER);
    surfacesPanel.add(surfaceCheck);

    JButton surfaceButton = new JButton("Surfaces");
    surfacesPanel.add(surfaceButton);

    JPanel spacesPanel = new JPanel();
    selectionPanel.add(spacesPanel);
    spacesPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
    spacesPanel.setLayout(new BoxLayout(spacesPanel, BoxLayout.Y_AXIS));

    JCheckBox spacesCheck = new JCheckBox("Visible");
    spacesCheck.setHorizontalAlignment(SwingConstants.CENTER);
    spacesPanel.add(spacesCheck);

    JButton spacesButton = new JButton("Spaces");
    spacesPanel.add(spacesButton);

    JScrollPane scrollPane_1 = new JScrollPane();
    scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane_1.setBounds(504, 6, 92, 251);
    contentPane.add(scrollPane_1);

    JLabel logoLabel = new JLabel("Logo Here");
    LogoLabel.setHorizontalAlignment(SwingConstants.CENTER);
    LogoLabel.setBounds(505, 264, 91, 80);
    contentPane.add(logoLabel);
}
}

具体做法是:

我希望selectionPanel锚定在左下角,propertiesPanel锚定在右上角,logoLabel锚定在右下角,drawingPanel锚定在左上角。这个想法是,当调整主窗体的大小时,滚动面板和标签应该保持在它们相对于父窗体边缘的精确位置,而drawingPanel应该可以水平和垂直调整大小,以便占用所有剩余空间与顶部框架的大小有关。徽标不应在任何维度上调整大小,而selectionPanel应在x维度中调整大小,并且属性窗口应在y维度中调整大小。最后,我希望表面和空间面板内的按钮占据面板内的最大空间。也就是说,无需进行反复试验或指定幻数值,如果面板为50x50像素,并且复选框高度为10像素,则按钮应为40像素高。

1 个答案:

答案 0 :(得分:2)

请参阅this answer,其中显示了将按钮拉伸到父容器大小的两种方法。