【问题标题】:JPanels in GridLayout in JavaSwingJava Swing 中 GridLayout 中的 JPanel
【发布时间】:2018-06-20 08:48:53
【问题描述】:

每个人。我想在 GridLayout 中有自定义 JPanel(我更喜欢使用绝对位置布局)。 GridLayout 在 ScrollPane 中。

public class App extends JFrame {
    public App() {
        super("bamlOperator");
        JPanel panel = new JPanel(new GridLayout(0, 1));
        JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        panel.add(new MyCustomPanel());
        panel.add(new MyCustomPanel());
        panel.add(new MyCustomPanel());
        panel.add(new MyCustomPanel());
        panel.add(new MyCustomPanel());
        panel.add(new MyCustomPanel());

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new App();
    }
}

还有我的面板:

public class MyCustomPanel extends JPanel {

    private JLabel aaa = new JLabel("aaa:");
    private JLabel bbb = new JLabel("bbb");
    private JLabel ccc = new JLabel("ccc:");

    public MyCustomPanel() {

        setPreferredSize(new Dimension(100,100));
        JPanel amlPanel = new JPanel();
        amlPanel.setLayout(null);
        amlPanel.setBounds(0,0,100,100);
        aaa.setBounds(10,20,30,40);
        amlPanel.add(aaa);
        bbb.setBounds(20,30,40,50);
        amlPanel.add(bbb);
        ccc.setBounds(30,40,50,60);
        amlPanel.add(ccc);
        add(amlPanel);
    }
}

但它不起作用。

我说,我更喜欢绝对位置布局,但我知道这是不好的做法。我可以使用另一个,但我需要这样的 JPanel:

JPanel 项目

【问题讨论】:

  • “但它不起作用。” 不是对问题的描述。请描述您正在经历什么以及您期待什么。
  • new MyCosutomPanel() 是否意味着 new AMLPanel() ?
  • @Gimhani 没错,我已经改进了。
  • @Ben 这确实意味着:我有 GridLayout 的空白框架。
  • 所以,您的根本问题是您将绝对布局与布局管理器混合在一起 - 问题是 MyCustomPanel 没有提供任何尺寸提示,布局管理器可以使用这些提示来更好地决定如何最好布局你的组件。因此,如果您真的想使用绝对布局,您将不得不完成布局管理 API 为您完成的所有工作

标签: java swing layout-manager grid-layout


【解决方案1】:

所以,您的根本问题是您将绝对布局与布局管理器混合在一起 - 问题是 MyCustomPanel 没有提供任何尺寸提示,布局管理器可以使用这些提示来更好地决定如何最好地布局您的组件。因此,如果您真的想使用绝对布局,您将不得不完成布局管理 API 为您完成的所有工作

您能告诉我哪个布局管理器最适合我使用吗?

所有这些。不要执着于单一的布局管理器来实现您想要的一切,而是将它们组合在一起以产生您所追求的结果。

我没有您的“确切”要求,但我能够通过使用 BorderLayoutGridBagLayout 来实现这一点

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test extends JFrame {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        public MainPane() {
            // You could use a GridLayout, but GridBagLayout will
            // honour the preferred sizs of each component
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(10, 10, 10, 10));
            GridBagConstraints gbc = new GridBagConstraints();
            add(new LeftPane(), gbc);
            add(new MiddleLeftPane(), gbc);
            add(new MiddlePane(), gbc);
            add(new RightPane(), gbc);
        }

    }

    public class LeftPane extends JPanel {

        public LeftPane() {
            setLayout(new GridBagLayout());

            JPanel main = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            for (int index = 0; index < 6; index++) {
                if (index % 2 == 0) {
                    gbc.anchor = GridBagConstraints.LINE_START;
                } else {
                    gbc.anchor = GridBagConstraints.LINE_END;
                }
                main.add(new JLabel("Label"), gbc);
            }

            gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(main, gbc);

            gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            add(new JButton("Button"));
        }

    }

    public class MiddleLeftPane extends JPanel {

        public MiddleLeftPane() {
            setLayout(new BorderLayout());
            BufferedImage img = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.RED);
            g2d.drawLine(0, 0, 200, 200);
            g2d.drawLine(200, 0, 0, 200);
            g2d.dispose();

            JLabel label = new JLabel(new ImageIcon(img));
            label.setBorder(new LineBorder(Color.GRAY));
            add(label);
        }

    }

    public class RightPane extends JPanel {

        public RightPane() {
            setLayout(new BorderLayout());
            BufferedImage img = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.RED);
            g2d.drawLine(0, 0, 200, 200);
            g2d.drawLine(200, 0, 0, 200);
            g2d.dispose();

            JLabel label = new JLabel(new ImageIcon(img));
            label.setBorder(new LineBorder(Color.GRAY));
            add(label);
        }

    }

    public class MiddlePane extends JPanel {

        public MiddlePane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);

            add(new JButton("Button"), gbc);
            gbc.gridx++;
            add(new JButton("Button"), gbc);

            gbc.gridwidth = 2;
            gbc.gridx = 0;
            gbc.gridy = 2;
            add(new JButton("Button"), gbc);

            gbc.gridy = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(new JScrollPane(new JTextArea("Text Area", 5, 10)), gbc);
        }

    }
}

【讨论】:

  • 我刚刚开始根据您的提示进行布局。你刚才介绍的正是我需要的!感谢您的帮助,我正在准备修改:)
  • 你能告诉我如何将左面板移到左侧,居中居中,将右面板移到右侧吗?
  • 我真的不明白。您的意思是要将它们与顶部、左侧/右侧位置对齐吗?
  • 最简单的方法之一是在将MiddlePane 添加到MainPane 时将GridBagConstraints weightx 属性设置为1 - 请记住重置它到0,然后再添加任何其他组件
  • 好的,当所有窗格都可见时它可以工作。但是当我隐藏 MiddleLeftPane、RightPane 和 MiddlePane 时,LeftPane 不会与左侧对齐。
【解决方案2】:

您正在使用null 布局来布局自定义面板。当您将其添加到另一个网格布局面板中时,由于未设置大小,因此不会绘制。尝试为您的自定义面板使用适当的布局。 并且,请参阅Using a JPanel with a null layout 的答案。

【讨论】:

  • 感谢您的回复。我现在不明白,你能告诉我哪个布局管理器最适合我使用吗?您可以在图片上看到我要实现的目标。
  • @M.Lup 您仍然只向主面板添加了标签面板。最好先组织主面板(例如 1*5 网格布局),然后添加具有适当布局(例如网格布局或流式布局)的子面板。
  • MyCustomPanel() 不使用空布局
  • @c0der amlpanel 布局为空。它需要更改为适当的布局。
猜你喜欢
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多