2

我有一个自定义的 JPanel,它的paintComponent 方法被覆盖以绘制图像。

我想在容器中垂直居中插入几个这样的自定义面板。为此,我创建了一个以 BoxLayout.X_AXIS 作为布局管理器的 jpanel。

表示

这很好用,显示了我想要的,但我想在自定义面板之间添加边距。

EmptyMargins 只是被忽略了,棘手的部分是我不能(或不想......)在它们之间添加支柱或框,因为我需要从循环中获取每个自定义面板,该循环需要容器的所有组件并将它们投射到 CustomPanel 中。

看到问题了吗?如果我在面板之间添加支柱,则会出现演员表异常并且 EmptyBorders 不起作用......欢迎任何想法!

注意:我对其他布局管理器提议持开放态度!;-)

这是代码:

public class StackExemple {

public StackExemple() {

    JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(600, 300));

    JPanel container = new JPanel();
    container.setPreferredSize(new Dimension(600, 300));
    container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

    CustomPanel customPanel1 = new CustomPanel();
    CustomPanel customPanel2 = new CustomPanel();
    CustomPanel customPanel3 = new CustomPanel();

    container.add(customPanel1);
    container.add(customPanel2);
    container.add(customPanel3);

    frame.getContentPane().add(container);
    frame.pack();
    frame.setVisible(true);

    //Loop which takes the custompanels
    for(Component comp : container.getComponents()) {
        CustomPanel panel = (CustomPanel)comp;
        //DO SOMETHING

        System.out.println("Hello World");
    }
}

private class CustomPanel extends JPanel{

    private BufferedImage image;

    public CustomPanel() {
        setPreferredSize(new Dimension(100, 100));
        setMinimumSize(getPreferredSize());
        setMaximumSize(getPreferredSize());
        setBorder(BorderFactory.createEmptyBorder(0,50,0,0));
        setBackground(Color.RED);

//      try {
//          image = ImageIO.read(ClassLoader.getSystemClassLoader().getResource("Ressources/img.png"));
//      } catch (IOException ex) {
//           System.out.println("Ooops... ");
//      }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
//      int x = (this.getWidth() - image.getWidth()) / 2;
//      int y = (this.getHeight() - image.getHeight()) / 2;
//      g.drawImage(image, x, y, null);
    }
}
}
4

2 回答 2

2

在此处输入图像描述

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class StackExemple {

    public StackExemple() {
        JFrame frame = new JFrame();
        JPanel container = new JPanel();
        container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
        CustomPanel customPanel1 = new CustomPanel(Color.blue);
        CustomPanel customPanel2 = new CustomPanel(Color.red);
        CustomPanel customPanel3 = new CustomPanel(Color.green);
        container.add(customPanel1);
        container.add(customPanel2);
        container.add(customPanel3);
        frame.getContentPane().add(container);
        frame.pack();
        frame.setVisible(true);
        for (Component comp : container.getComponents()) {
            CustomPanel panel = (CustomPanel) comp;
            System.out.println("Hello World");
        }
    }

    private class CustomPanel extends JPanel {

        private BufferedImage image;

        @Override
        public Dimension getMinimumSize() {
            return new Dimension(100, 80);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 160);
        }

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(400, 320);
        }

        public CustomPanel(Color c) {
            setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createEmptyBorder(10, 10, 10, 10), 
                    BorderFactory.createLineBorder(Color.black, 1)));
            //setBorder(BorderFactory.createCompoundBorder(
            //BorderFactory.createLineBorder(Color.black, 1), 
            //BorderFactory.createEmptyBorder(10, 10, 10, 10)));
            setBackground(c);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                StackExemple stackExemple = new StackExemple();
            }
        });
    }
}
于 2013-02-28T11:06:00.353 回答
2

边框似乎不受尊重的根本原因是默认情况下面板是不透明的,即它保证用完全纯色的背景颜色填充其区域的每个像素。边框覆盖的区域是面板区域的一部分,因此也必须填充面板的背景。

由于您似乎无论如何都在进行自定义绘画,您可能会考虑将其不透明性报告为错误,并且仅在边界区域内绘制背景(和/或背景图像):

// in constructor
setOpaque(false);

@Override
protected void paintComponent(Graphics g) {
    // take over background filling inside the border
    Insets insets = getInsets();
    g.setColor(getBackground());
    g.fillRect(insets.left, insets.top, 
            getWidth() - insets.left - insets.right, 
            getHeight() - insets.top - insets.bottom);

    super.paintComponent(g);
    // for a background image, you would need to take the insets
    // into account as well 
    // int x = (this.getWidth() - image.getWidth()) / 2;
    // int y = (this.getHeight() - image.getHeight()) / 2;
    // g.drawImage(image, x, y, null);
}
于 2013-02-28T11:25:38.570 回答