java 无法向JPanel(GridBagLayout)添加填充,因为JPanel中有2个其他JPanel

snz8szmq  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(145)

你能帮助我理解为什么在dataInputPanel中没有填充(红色边框)吗?
目标:在dataInputPanel中的顶部、右侧、底部和左侧有10个填充
错误:没有错误。就是不管用
主要:

public class Main {
    public static void main(String[] args) {
        JFrame win = new JFrame();
        win.setTitle("Lord of the Rings - Battle Simulator");
        win.setSize(500, 600);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setLayout(new GridLayout(2, 1));
        win.setResizable(false);
        //-------------

        JPanel dataInputPanel = new JPanel();
        dataInputPanel.setLayout(new GridLayout(1, 2, 10, 0));
        //dataInputPanel.setBackground(Color.RED);
        dataInputPanel.setBorder(new LineBorder(Color.RED, 4, true));

        JPanel battleOutupPanel = new JPanel();
        //battleOutupPanel.setBackground(Color.GREEN);
        battleOutupPanel.setBorder(new LineBorder(Color.GREEN, 4, true));
        //-------------------------------------

        int paddingSize = 50;
        Insets paddingInsets = new Insets(paddingSize, paddingSize, paddingSize, paddingSize);

        // Heroes Data Input Panel - dataInputPanel GridBagLayout 1 row, 2 columns
        JPanel heroesDataInputPanel = new JPanel();
        //heroesDataInputPanel.setBackground(Color.YELLOW);
        heroesDataInputPanel.setBorder(new LineBorder(Color.YELLOW, 4, true));

        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.gridx = 0;
        gbc1.gridy = 0;
        gbc1.insets = paddingInsets;
        dataInputPanel.add(heroesDataInputPanel, gbc1);

        // Beasts Data Input Panel - dataInputPanel - GridBagLayout 1 row, 2 columns
        JPanel beastsDataInputPanel = new JPanel();
        //beastsDataInputPanel.setBackground(Color.ORANGE);
        beastsDataInputPanel.setBorder(new LineBorder(Color.ORANGE, 4, true));

        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.gridx = 1;
        gbc2.gridy = 0;
        gbc2.insets = paddingInsets;
        dataInputPanel.add(beastsDataInputPanel, gbc2);
        //-------------

        win.add(dataInputPanel);
        win.add(battleOutupPanel);
        win.setVisible(true);
    }
}

赢:

public class Win extends JFrame {
}

Pnl:

public class Pnl extends JPanel {
}

谢谢你!

xnifntxz

xnifntxz1#

您真的需要了解一下How to use borders-并重点关注CompoundBorder

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        public TestPane() {
            setLayout(new GridLayout(2, 1));
            JPanel dataInputPanel = new JPanel();
            dataInputPanel.setLayout(new GridLayout(1, 2, 50, 0));
            dataInputPanel.setBorder(new CompoundBorder(
                    new LineBorder(Color.RED, 4, true), 
                    new EmptyBorder(10, 10, 10, 10)
            ));
            //dataInputPanel.setBackground(Color.RED);
//            dataInputPanel.setBorder(new LineBorder(Color.RED, 4, true));

            JPanel battleOutupPanel = new JPanel();
            //battleOutupPanel.setBackground(Color.GREEN);
            battleOutupPanel.setBorder(new LineBorder(Color.GREEN, 4, true));
            //-------------------------------------

            int paddingSize = 50;
            Insets paddingInsets = new Insets(paddingSize, paddingSize, paddingSize, paddingSize);

            // Heroes Data Input Panel - dataInputPanel GridBagLayout 1 row, 2 columns
            JPanel heroesDataInputPanel = new JPanel();
            //heroesDataInputPanel.setBackground(Color.YELLOW);
            heroesDataInputPanel.setBorder(new LineBorder(Color.YELLOW, 4, true));

            dataInputPanel.add(heroesDataInputPanel);

            // Beasts Data Input Panel - dataInputPanel - GridBagLayout 1 row, 2 columns
            JPanel beastsDataInputPanel = new JPanel();
            //beastsDataInputPanel.setBackground(Color.ORANGE);
            beastsDataInputPanel.setBorder(new LineBorder(Color.ORANGE, 4, true));

            dataInputPanel.add(beastsDataInputPanel);
            //-------------

            add(dataInputPanel);
            add(battleOutupPanel);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 600);
        }
    }
}

相关问题