2

画像を描画するために paintComponent メソッドがオーバーライドされるカスタム JPanel があります。

これらのカスタム パネルのいくつかをコンテナの垂直方向の中央に挿入したいと考えています。これを行うために、レイアウト マネージャーとして 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

境界線が尊重されていないように見える根本的な理由は、パネルがデフォルトで不透明であるためです。つまり、その領域の各ピクセルを完全に塗りつぶされた背景色で塗りつぶすことが保証されます。境界線で覆われた領域はパネルの領域の一部であるため、パネルの背景も塗りつぶす必要があります。

とにかくカスタムペイントを行っているように見えるので、その不透明度をfalseとして報告し、境界領域内の背景(および/または背景画像)のみをペイントすることを検討してください。

// 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 に答える