gpt4 book ai didi

java - BoxLayout.X_AXIS 中 JPanel 上的 EmptyBorders

转载 作者:行者123 更新时间:2023-11-30 07:18:45 26 4
gpt4 key购买 nike

我有一个自定义的 JPanel,重写了 paintComponent 方法来绘制图像。

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

representation

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

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

最佳答案

enter image description here

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

关于java - BoxLayout.X_AXIS 中 JPanel 上的 EmptyBorders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15133274/

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