【问题标题】:Create swing buttons that fill their parent [closed]创建填充其父级的摆动按钮[关闭]
【发布时间】:2013-01-16 00:37:14
【问题描述】:

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

【问题讨论】:

  • 您的问题在于您使用空布局而不是尝试使用布局管理器。这是他们发光的地方。就像您选择丢弃 Swing 库中更强大的功能之一,然后因为您正在这样做而被卡住。解决方案是避免像瘟疫一样的空布局和绝对定位。我建议您暂时放弃窗口构建器并阅读布局管理器教程,以便更好地了解它们的工作原理。
  • 我切换到 gridbag 但遇到了完全相同的问题...我需要看什么才能更好地理解这些东西?
  • 看起来约束是我想在这里学习的...?
  • 这里有很多关于布局管理器的帖子,很多都是我回答的,为什么不看看一些。但首先阅读教程,因为它在那里描述得很好。学习嵌套 JPanel,每个 JPanel 使用自己的布局,除非绝对必要,否则避免使用 GridBagLayout,甚至考虑下载一些布局,例如 MigLayout,但主要是试验、尝试、测试。
  • 我在这里浏览了很多关于布局管理器的帖子(并注意到了你的许多答案)。似乎我发现的大部分内容都是针对特定问题的问题。我已经阅读了教程,并且正在尝试应用我所阅读的内容。我想我会坚持下去,当我遇到更具体的问题时,我会发布一个问题。感谢您的回复!

标签: java swing layout layout-manager windowbuilder


【解决方案1】:

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

【讨论】:

  • 完美!迷人的!谢谢!
  • 不客气。很高兴你把事情解决了。 :)
猜你喜欢
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多