Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建填充其父项的swing按钮_Java_Swing_Layout_Layout Manager_Windowbuilder - Fatal编程技术网

Java 创建填充其父项的swing按钮

Java 创建填充其父项的swing按钮,java,swing,layout,layout-manager,windowbuilder,Java,Swing,Layout,Layout Manager,Windowbuilder,我在EclipseIndigo中使用WindowBuilder来处理不同的布局。基本上,我只是想彻底了解每种方法的工作原理,以及哪种方法最适合不同的场景。下面的代码可以工作,但有一些事情我想调整,但我不知道如何获得我想要的效果 public class Dashboard extends JFrame { private JPanel contentPane; public static void main(String[] args) { EventQueue.invokeLate

我在EclipseIndigo中使用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像素。请参见以下哪种方法显示了将按钮拉伸到父容器大小的两种方法


您的问题在于使用空布局,而不是尝试使用布局管理器。这就是它们发光的地方。这就像你选择扔掉Swing库中一个更强大的功能,然后因为你这么做而陷入困境。解决方案是避免像瘟疫一样的零布局和绝对定位。我建议您暂时不要使用window builder,阅读布局管理器教程,以便更好地了解它们的工作原理。我切换到gridbag,但遇到了完全相同的问题。。。为了更好地理解这些东西,我需要看什么?看起来这些约束是我想在这里学习的…?这里有很多关于布局管理器的帖子,很多都是我回答的,为什么不看一些呢。但首先阅读教程,因为它描述得很好。学会嵌套JPanels,每个都使用自己的布局,避免使用GRIDBAG布局,除非绝对必要,甚至考虑下载一些布局,如MigStayes,但主要是实验、尝试、测试。我一直在浏览布局管理器上的许多帖子(并且注意到了很多答案)。看来我发现的大部分问题都是针对特定问题的。我已经阅读了教程,并且正在尝试应用我所阅读的内容。我想我会坚持下去,当我遇到更具体的问题时,我会发一个问题。谢谢你的回复!完美的可爱!谢谢不客气。很高兴你把它整理好了。:)