如何在包含在JLabel中的图标图像之间创建填充?

huangapple go评论88阅读模式
英文:

How do I create padding between icon images that are inside of JLabels

问题

我尝试创建一个具有以下布局的JFrame:期望效果
而我目前拥有的是这个:当前进展。如何在两个图像之间以及顶部和底部创建填充?我尝试使用空边框属性并设置左边和右边的值,但中间、顶部和底部的空白空间没有被创建。

英文:

I am trying to create a JFrame that has a layout like this image desired goal
and what I currently have is this current progress. How do I create padding between the two images as well as at the top and bottom? I have tried using the empty border property and setting the left and right at their respective values, but the gap of space in the middle, top, and bottom does not get created.

答案1

得分: 0

以下是您提供的代码部分的翻译:

使用EmptyBorder

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new BorderLayout());

            JLabel header = new JLabel("Next");
            header.setBorder(new EmptyBorder(4, 0, 4, 0));
            header.setHorizontalAlignment(JLabel.CENTER);
            header.setOpaque(true);
            header.setBackground(Color.BLUE);
            header.setForeground(Color.WHITE);

            add(header, BorderLayout.NORTH);

            JLabel footer = new JLabel("This is the way");
            footer.setBorder(new EmptyBorder(4, 0, 4, 0));
            footer.setHorizontalAlignment(JLabel.CENTER);
            footer.setOpaque(true);
            footer.setBackground(Color.BLUE);
            footer.setForeground(Color.WHITE);

            add(footer, BorderLayout.SOUTH);

            BufferedImage leftImage = ImageIO.read(getClass().getResource("/images/Mando01.jpeg"));
            BufferedImage rightImage = ImageIO.read(getClass().getResource("/images/Mando02.jpeg"));

            JLabel leftLabel = new JLabel(new ImageIcon(leftImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));
            JLabel rightLabel = new JLabel(new ImageIcon(rightImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));

            leftLabel.setBorder(new EmptyBorder(8, 8, 8, 4));
            rightLabel.setBorder(new EmptyBorder(8, 4, 8, 8));

            JPanel imagePane = new JPanel(new GridBagLayout());
            imagePane.setBackground(Color.DARK_GRAY);
            imagePane.add(leftLabel);
            imagePane.add(rightLabel);

            add(imagePane);
        }
    }
}

使用FlowLayout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new BorderLayout());

            JLabel header = new JLabel("Next");
            header.setBorder(new EmptyBorder(4, 0, 4, 0));
            header.setHorizontalAlignment(JLabel.CENTER);
            header.setOpaque(true);
            header.setBackground(Color.BLUE);
            header.setForeground(Color.WHITE);

            add(header, BorderLayout.NORTH);

            JLabel footer = new JLabel("This is the way");
            footer.setBorder(new EmptyBorder(4, 0, 4, 0));
            footer.setHorizontalAlignment(JLabel.CENTER);
            footer.setOpaque(true);
            footer.setBackground(Color.BLUE);
            footer.setForeground(Color.WHITE);

            add(footer, BorderLayout.SOUTH);

            BufferedImage leftImage = ImageIO.read(getClass().getResource("/images/Mando01.jpeg"));
            BufferedImage rightImage = ImageIO.read(getClass().getResource("/images/Mando02.jpeg"));

            JLabel leftLabel = new JLabel(new ImageIcon(leftImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));
            JLabel rightLabel = new JLabel(new ImageIcon(rightImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));

            JPanel imagePane = new JPanel(new FlowLayout(FlowLayout.CENTER, 8, 8));
            imagePane.setBackground(Color.DARK_GRAY);
            imagePane.add(leftLabel);
            imagePane.add(rightLabel);

            add(imagePane);
        }
    }
}

使用GridBagLayout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = a JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new BorderLayout());

            JLabel header = new JLabel("Next");
            header.setBorder(new EmptyBorder(4, 0, 4, 0));
            header.setHorizontalAlignment(JLabel.CENTER);
            header.setOpaque(true);
            header.setBackground(Color.BLUE);
            header.setForeground(Color.WHITE);

            add(header, BorderLayout.NORTH);

            JLabel footer = new JLabel("This is the way");
            footer.setBorder(new EmptyBorder(4, 0, 4, 0));
            footer.setHorizontalAlignment(JLabel.CENTER);
            footer.setOpaque(true);
            footer.setBackground(Color.BLUE);
            footer.setForeground(Color.WHITE);

            add(footer, BorderLayout.SOUTH);

            BufferedImage leftImage = ImageIO.read(getClass().getResource("/images/Mando01.jpeg"));
            BufferedImage rightImage = ImageIO.read(getClass().getResource("/images/Mando02.jpeg"));

            JLabel leftLabel = new JLabel(new ImageIcon(leftImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));
            JLabel rightLabel = new JLabel(new ImageIcon(rightImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));

            JPanel imagePane = new JPanel(new GridBagLayout());
            imagePane.setBackground(Color.DARK_GRAY);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(8, 8, 8, 4);
            imagePane.add(leftLabel, gbc);

            gbc = new GridBagConstraints();
            gbc.insets =

<details>
<summary>英文:</summary>

See: 

* [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html)
* [How to Use Borders](https://docs.oracle.com/javase/tutorial/uiswing/components/border.html)

for more details...

# Use an `EmptyBorder`

[![enter image description here][1]][1]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;

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

        public Main() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        JFrame frame = new JFrame();
                        frame.add(new TestPane());
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }

        public class TestPane extends JPanel {

            public TestPane() throws IOException {
                setLayout(new BorderLayout());

                JLabel header = new JLabel(&quot;Next&quot;);
                header.setBorder(new EmptyBorder(4, 0, 4, 0));
                header.setHorizontalAlignment(JLabel.CENTER);
                header.setOpaque(true);
                header.setBackground(Color.BLUE);
                header.setForeground(Color.WHITE);

                add(header, BorderLayout.NORTH);

                JLabel footer = new JLabel(&quot;This is the way&quot;);
                footer.setBorder(new EmptyBorder(4, 0, 4, 0));
                footer.setHorizontalAlignment(JLabel.CENTER);
                footer.setOpaque(true);
                footer.setBackground(Color.BLUE);
                footer.setForeground(Color.WHITE);

                add(footer, BorderLayout.SOUTH);

                BufferedImage leftImage = ImageIO.read(getClass().getResource(&quot;/images/Mando01.jpeg&quot;));
                BufferedImage rightImage = ImageIO.read(getClass().getResource(&quot;/images/Mando02.jpeg&quot;));

                JLabel leftLabel = new JLabel(new ImageIcon(leftImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));
                JLabel rightLabel = new JLabel(new ImageIcon(rightImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));

                leftLabel.setBorder(new EmptyBorder(8, 8, 8, 4));
                rightLabel.setBorder(new EmptyBorder(8, 4, 8, 8));

                JPanel imagePane = new JPanel(new GridBagLayout());
                imagePane.setBackground(Color.DARK_GRAY);
                imagePane.add(leftLabel);
                imagePane.add(rightLabel);

                add(imagePane);
            }
        }
    }

# Use a `FlowLayout`

[![enter image description here][2]][2]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;

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

        public Main() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        JFrame frame = new JFrame();
                        frame.add(new TestPane());
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }

        public class TestPane extends JPanel {

            public TestPane() throws IOException {
                setLayout(new BorderLayout());

                JLabel header = new JLabel(&quot;Next&quot;);
                header.setBorder(new EmptyBorder(4, 0, 4, 0));
                header.setHorizontalAlignment(JLabel.CENTER);
                header.setOpaque(true);
                header.setBackground(Color.BLUE);
                header.setForeground(Color.WHITE);

                add(header, BorderLayout.NORTH);

                JLabel footer = new JLabel(&quot;This is the way&quot;);
                footer.setBorder(new EmptyBorder(4, 0, 4, 0));
                footer.setHorizontalAlignment(JLabel.CENTER);
                footer.setOpaque(true);
                footer.setBackground(Color.BLUE);
                footer.setForeground(Color.WHITE);

                add(footer, BorderLayout.SOUTH);

                BufferedImage leftImage = ImageIO.read(getClass().getResource(&quot;/images/Mando01.jpeg&quot;));
                BufferedImage rightImage = ImageIO.read(getClass().getResource(&quot;/images/Mando02.jpeg&quot;));

                JLabel leftLabel = new JLabel(new ImageIcon(leftImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));
                JLabel rightLabel = new JLabel(new ImageIcon(rightImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));

                JPanel imagePane = new JPanel(new FlowLayout(FlowLayout.CENTER, 8, 8));
                imagePane.setBackground(Color.DARK_GRAY);
                imagePane.add(leftLabel);
                imagePane.add(rightLabel);

                add(imagePane);
            }
        }
    }

# Use a `GridBagLayout`

[![enter image description here][3]][3]


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.Insets;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;

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

        public Main() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        JFrame frame = new JFrame();
                        frame.add(new TestPane());
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }

        public class TestPane extends JPanel {

            public TestPane() throws IOException {
                setLayout(new BorderLayout());

                JLabel header = new JLabel(&quot;Next&quot;);
                header.setBorder(new EmptyBorder(4, 0, 4, 0));
                header.setHorizontalAlignment(JLabel.CENTER);
                header.setOpaque(true);
                header.setBackground(Color.BLUE);
                header.setForeground(Color.WHITE);

                add(header, BorderLayout.NORTH);

                JLabel footer = new JLabel(&quot;This is the way&quot;);
                footer.setBorder(new EmptyBorder(4, 0, 4, 0));
                footer.setHorizontalAlignment(JLabel.CENTER);
                footer.setOpaque(true);
                footer.setBackground(Color.BLUE);
                footer.setForeground(Color.WHITE);

                add(footer, BorderLayout.SOUTH);

                BufferedImage leftImage = ImageIO.read(getClass().getResource(&quot;/images/Mando01.jpeg&quot;));
                BufferedImage rightImage = ImageIO.read(getClass().getResource(&quot;/images/Mando02.jpeg&quot;));

                JLabel leftLabel = new JLabel(new ImageIcon(leftImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));
                JLabel rightLabel = new JLabel(new ImageIcon(rightImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));

                JPanel imagePane = new JPanel(new GridBagLayout());
                imagePane.setBackground(Color.DARK_GRAY);

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(8, 8, 8, 4);
                imagePane.add(leftLabel, gbc);

                gbc = new GridBagConstraints();
                gbc.insets = new Insets(8, 4, 8, 8);
                imagePane.add(rightLabel, gbc);

                add(imagePane);
            }
        }
    }

# Use a `GridLayout` (and `EmptyBorder`)

[![enter image description here][4]][4]


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;

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

        public Main() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        JFrame frame = new JFrame();
                        frame.add(new TestPane());
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }

        public class TestPane extends JPanel {

            public TestPane() throws IOException {
                setLayout(new BorderLayout());

                JLabel header = new JLabel(&quot;Next&quot;);
                header.setBorder(new EmptyBorder(4, 0, 4, 0));
                header.setHorizontalAlignment(JLabel.CENTER);
                header.setOpaque(true);
                header.setBackground(Color.BLUE);
                header.setForeground(Color.WHITE);

                add(header, BorderLayout.NORTH);

                JLabel footer = new JLabel(&quot;This is the way&quot;);
                footer.setBorder(new EmptyBorder(4, 0, 4, 0));
                footer.setHorizontalAlignment(JLabel.CENTER);
                footer.setOpaque(true);
                footer.setBackground(Color.BLUE);
                footer.setForeground(Color.WHITE);

                add(footer, BorderLayout.SOUTH);

                BufferedImage leftImage = ImageIO.read(getClass().getResource(&quot;/images/Mando01.jpeg&quot;));
                BufferedImage rightImage = ImageIO.read(getClass().getResource(&quot;/images/Mando02.jpeg&quot;));

                JLabel leftLabel = new JLabel(new ImageIcon(leftImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));
                JLabel rightLabel = new JLabel(new ImageIcon(rightImage.getScaledInstance(200, -1, Image.SCALE_SMOOTH)));

                JPanel imagePane = new JPanel(new GridLayout(1, 2, 8, 8));
                imagePane.setBorder(new EmptyBorder(8, 8, 8, 8));
                imagePane.setBackground(Color.DARK_GRAY);

                imagePane.add(leftLabel);
                imagePane.add(rightLabel);

                add(imagePane);
            }
        }
    }

# Or a combination of any...

Based on your needs


  [1]: https://i.stack.imgur.com/QZORw.png
  [2]: https://i.stack.imgur.com/Qv8Ph.png
  [3]: https://i.stack.imgur.com/Rey86.png
  [4]: https://i.stack.imgur.com/JEH7o.png

</details>



huangapple
  • 本文由 发表于 2023年3月23日 10:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定