Java Swing 中 GridLayout 中的 JPanel

JPanels in GridLayout in JavaSwing

各位。我想在 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 项目

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

所以,您的根本问题是您将绝对布局与布局管理器混合在一起 - 问题是 MyCustomPanel 没有提供布局管理器可以用来更好地决定如何最好地调整大小的提示布局你的组件。所以,如果你真的想使用绝对布局,你将不得不做布局管理 API 会为你做的所有工作

Can you tell me which layout manager will be the best for my use?

全部。不要专注于实现您想要的一切的单个布局管理器,而是将它们组合在一起以产生您想要的结果。

我没有你的 "exact" 要求,但我可以通过使用 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);
        }

    }
}