gpt4 book ai didi

java - 自定义 JPanel 导致异常的 GridBag 约束布局行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:22 25 4
gpt4 key购买 nike

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

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);
}

}

}

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

enter image description here

最佳答案

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

enter image description here

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
}
}

关于java - 自定义 JPanel 导致异常的 GridBag 约束布局行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com