JavaSwing中GridLayout中的JPanels

时间:2018-06-20 08:48:53

标签: java swing layout-manager grid-layout

每个人。我想在GridLayout中使用自定义的JPanels(我更喜欢使用绝对位置布局)。 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();
    }
}

还有MyPanel:

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的项目

enter image description here

2 个答案:

答案 0 :(得分:2)

因此,您的根本问题是您将绝对布局与布局管理器混合使用-问题是MyCustomPanel没有提供任何大小调整提示,布局管理器可使用这些提示来就如何最佳布局您的布局做出更好的决策零件。因此,如果您真的想使用绝对布局,则必须完成布局管理API会为您完成的所有工作

  

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

所有。不要局限于单一的布局管理器就能实现您想要的一切,而是将它们组合在一起以产生想要的结果。

我没有您的“确切”要求,但是我可以使用BorderLayoutGridBagLayout

For example

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);
        }

    }
}

答案 1 :(得分:1)

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