3

我正在开发一个简单的图像编辑程序来调整亮度/对比度。我的自定义 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它似乎看起来像我想要的那样。

在此处输入图像描述

4

2 回答 2

2

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
    }
}
于 2012-12-19T01:05:00.057 回答
1

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

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

于 2012-12-19T10:35:11.613 回答