【问题标题】:Custom JPanel causing unusual GridBagConstraint layout behavoiur自定义 JPanel 导致不寻常的 GridBag 约束布局行为
【发布时间】:2012-12-06 06:58:30
【问题描述】:

我正在开发一个简单的图像编辑程序来调整亮度/对比度。我的自定义 JPanel 仅用于显示加载的图像,但它导致下一列上的滑块和标签显示不正确。

import java.awt.*;
import javax.swing.*;

public class IMView extends JFrame  implements Observer {

JButton selectIMGFolder = new JButton("Select Folder");
ImagePanel originalImage;
//Labels and sliders to control image
JLabel brightnessLabel = new JLabel("Brightness");
JSlider brightness = new JSlider(-255,255,0);
JLabel contrastLabel = new JLabel("Contrast");
JSlider contrastSlider = new JSlider(-255,255,0);

JLabel random = new JLabel("Random Test Label");

public IMView (){
    super("Image manipulation");

    JPanel mainJPanel = new JPanel();
    mainJPanel.setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    originalImage = new ImagePanel();
    //first column

    gc.anchor = GridBagConstraints.LINE_START;
    gc.weightx = 0.5;
    gc.weighty = 0.5;

    gc.gridx = 0;
    gc.gridy = 0;
    mainJPanel.add(selectIMGFolder, gc);

    gc.gridx = 0;
    gc.gridy = 1;

    mainJPanel.add(originalImage, gc);

    gc.gridx = 0;
    gc.gridy = 2;
    mainJPanel.add(random, gc);

    //second column
    //brightness

    gc.gridx = 1;
    gc.gridy = 0;

    mainJPanel.add(brightnessLabel, gc);

    gc.gridx = 1;
    gc.gridy = 1;
    mainJPanel.add(brightness, gc);

    gc.gridx = 1;
    gc.gridy = 2;
    mainJPanel.add(contrastLabel, gc);

    gc.weighty = 10;
    gc.anchor = GridBagConstraints.FIRST_LINE_START;
    gc.gridx = 1;
    gc.gridy = 3;       
    mainJPanel.add(contrastSlider, gc);

    this.add(mainJPanel);
    this.setVisible(true);
    this.setPreferredSize(new Dimension(800, 600));
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

//other code
}

这是我的简单自定义面板:

public class ImagePanel extends JPanel {

Image image;

public ImagePanel(){
    this.setPreferredSize(new Dimension(400,300));

}   

//set image file some where

@Override
public void paintComponent(Graphics g){
    super.paintComponents(g);
    g.setColor(Color.red);
    g.fillRect(0, 0, 400,300);
    if(image != null){
         g.drawImage(image, 0,0, this);
    }

}

}

下面是代码的结果(其中红色方块代表图像),我基本上希望将右侧的滑块和标签打包在一起。如果我不将自定义面板添加到 mainJPanel,它似乎看起来像我想要的那样。

【问题讨论】:

  • “我的自定义 JPanel 仅用于显示加载的图像” 在滚动窗格中心的标签中显示图像,如 ImageViewer 所示。跨度>
  • 另请参阅How to create screenshots?,了解有关调整大小和裁剪的一些重要技巧。
  • 谢谢,这为我提供了另一种显示图像的方式。但这并没有解决问题。

标签: java swing jpanel layout-manager gridbaglayout


【解决方案1】:

GridBagLayout 足够复杂,无需在单个容器中布置所有内容。将您的表单分解为责任区域并使用单独的容器。

public class BadLayout08 {

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

    public BadLayout08() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new IMView();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class IMView extends JFrame {

        JButton selectIMGFolder = new JButton("Select Folder");
        JPanel originalImage;
//Labels and sliders to control image
        JLabel brightnessLabel = new JLabel("Brightness");
        JSlider brightness = new JSlider(-255, 255, 0);
        JLabel contrastLabel = new JLabel("Contrast");
        JSlider contrastSlider = new JSlider(-255, 255, 0);
        JLabel random = new JLabel("Random Test Label");

        public IMView() {
            super("Image manipulation");

            JPanel mainJPanel = new JPanel();
            mainJPanel.setLayout(new GridBagLayout());

            GridBagConstraints gc = new GridBagConstraints();

            originalImage = new JPanel();
            originalImage.setPreferredSize(new Dimension(400, 300));
            originalImage.setBackground(Color.RED);
            //first column

            gc.anchor = GridBagConstraints.LINE_START;

            gc.gridx = 0;
            gc.gridy = 0;
            mainJPanel.add(selectIMGFolder, gc);

            gc = new GridBagConstraints();
            gc.anchor = GridBagConstraints.LINE_START;
            gc.gridx = 0;
            gc.gridy = 1;
            mainJPanel.add(originalImage, gc);

            gc = new GridBagConstraints();
            gc.gridx = 0;
            gc.gridy = 2;
            mainJPanel.add(random, gc);

            //second column
            //brightness

            JPanel sidePane = new JPanel(new GridBagLayout());
            gc = new GridBagConstraints();

            gc.gridx = 1;
            gc.gridy = 0;

            sidePane.add(brightnessLabel, gc);

            gc.gridx = 1;
            gc.gridy = 1;
            sidePane.add(brightness, gc);

            gc.gridx = 1;
            gc.gridy = 2;
            sidePane.add(contrastLabel, gc);

            gc.weighty = 10;
            gc.anchor = GridBagConstraints.FIRST_LINE_START;
            gc.gridx = 1;
            gc.gridy = 3;
            sidePane.add(contrastSlider, gc);

            gc = new GridBagConstraints();
            gc.gridx = 1;
            gc.gridy = 1;
            gc.gridheight = GridBagConstraints.REMAINDER;
            gc.anchor = GridBagConstraints.NORTH;
            mainJPanel.add(sidePane, gc);

            this.add(mainJPanel);
            this.setVisible(true);
            this.setPreferredSize(new Dimension(800, 600));
            this.pack();
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
//other code
    }
}

【讨论】:

  • 谢谢,这正是我需要的!
【解决方案2】:

在这种情况下,您可以使用 BorderLayout。红色面板需要最大水平和垂直拉伸。可以将所有其他组件推到红色面板上的最小尺寸。

如果您需要使用 GridBagLayout,请参阅: http://blue-walrus.com/?p=582

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 2023-04-02
    相关资源
    最近更新 更多