gpt4 book ai didi

java - 如何为嵌套 JPanel 选择最佳布局?

转载 作者:行者123 更新时间:2023-12-01 20:17:29 25 4
gpt4 key购买 nike

我计划设计一个 gui 程序,其中包含一个外部 JPanel,其中包含许多具有文本和按钮的重复 JPanel。但是,我无法决定哪些布局适合此任务。我希望它像这:

enter image description here

我刚刚复制并粘贴了第一个 JPanel,它将以编程方式重复出现,如图所示。我应该使用哪种布局才能获得这样的结果?

最佳答案

在我看来,它是这样的:

JScrollPane > JPanel (outerPane) > JPanel (innerPane [many])

基于此,我们需要思考 outerPane 是哪个布局管理器将使用和 innerPane是...

为了在 innerPane 之间提供间距我会选择 GridLayout (rows, columns, hgap, vgap) 像:

GridLayout(0, 1, 5, 5);

现在为每个innerPane我们可以选择 GridLayout , GridBagLayoutFlowLayout ,让我们看看每个会发生什么:

  • 如果我们使用FlowLayout组件不会处于“网格”或“表格”之类的布局中,因此,这是不行的......这就是它的样子:

    enter image description here

    虽然它们看起来像我们需要的,但我不确定每个标签是否会随着时间的推移而改变,但你可以尝试...

    • 使用GridLayout将使我们的JButton s 占用单元格的整个空间,并且看起来不太好(至少在调整大小时),这是调整 GUI 大小之前和之后的图像以显示我的意思(裁剪以不使用大量空间在帖子中):

    enter image description here

    如果你的 GUI 无法调整大小,你可以使用此路径,如果可以,那么你应该使用其他布局。

    • GridBagLayout在这种情况下是我最喜欢的,因为每个标签都会保留在自己的列中,并且按钮不会填充所有可用空间,并且我们的 GUI 看起来更像您发布的图像:

    enter image description here

    在上面的示例中,我使用了 CustomBorder类以在 innerPane 之间提供间距s 和 outerPane同时还提供彩色边框并显示垂直 JScrollPane总是。

    产生这些输出的代码是:

    package sof;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import java.awt.geom.Rectangle2D;

    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    import javax.swing.border.AbstractBorder;

    public class NestedPanels {
    private JFrame frame;
    private JPanel outerPane;
    private JPanel innerPane;
    private GridBagConstraints gbc;
    private JScrollPane scrollPane;

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new NestedPanels()::createAndShowGui);
    }

    private void createAndShowGui() {
    frame = new JFrame(getClass().getSimpleName());

    outerPane = new JPanel();
    outerPane.setLayout(new GridLayout(0, 1, 5, 5));

    for (int i = 0; i < 5; i++) {
    innerPane = new JPanel();
    innerPane.setLayout(new GridBagLayout());

    gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(10, 10, 10, 10);

    innerPane.add(new JLabel("Recurring JLabel1"), gbc);

    gbc.gridx++;
    innerPane.add(new JLabel("Recurring JLabel2"), gbc);
    gbc.gridx++;
    innerPane.add(new JLabel("Recurring JLabel3"), gbc);
    gbc.gridx++;
    innerPane.add(new JButton("Recurring JButton1"), gbc);

    innerPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    outerPane.add(innerPane);
    }
    outerPane.setBorder(new CustomBorder(5, Color.BLACK));

    scrollPane = new JScrollPane(outerPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @SuppressWarnings("serial")
    class CustomBorder extends AbstractBorder {
    private int gap;
    private Color color;
    public CustomBorder(int gap, Color color) {
    this.gap = gap;
    this.color = color;
    }

    @Override
    public Insets getBorderInsets(Component c) {
    return new Insets(gap, gap, gap, gap);
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    super.paintBorder(c, g, x, y, width, height);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(color);
    g2d.draw(new Rectangle2D.Double(x, y, width - 1, height - 1));
    }
    }
    }

调整边框样式以获得所需的边框样式,我在 GUI 上用 -1 像素绘制边框,如果不这样做,它只会显示左侧和顶部边框...

  • 另一种选择是使用 JTable但我把这个留给你

关于java - 如何为嵌套 JPanel 选择最佳布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com