我有一个名为 panneau 的类:
public class Panneau extends JPanel {
public void paintComponent(Graphics g){
try {
Image img = ImageIO.read(new File("src/disantiammo/image/image.png"));
g.drawImage(img, 0, 0, this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//for now it is not doing anything but it will...
在 JFrame 中,我有一些带有其他元素的 JPanel。
JFrame 的布局是 BorderLayout
问题是这个平底锅必须是“java.awt.BorderLayout.CENTER”,我只能这样放置它:
this.setContentPane(new Panneau());
如何将其放置在“CENTER”中?
我尝试过类似的事情
this.getContentPane().add(new Panneau(), java.awt.BorderLayout.CENTER);
似乎什么都不起作用,我唯一能做的就是放置“panneau”而不是我的 JFrame 中的内容。 编辑:
这听起来可能是一件非常简单的事情,但我不明白为什么我不能这样做,我一直在尝试这样做。
我有 jDialog,它位于 BorderLayout 中,在“南”、“北”和“中心”中,我有带有元素的 jPanel(“中心”的 jPanel 中没有任何内容)。 “center”jPanel 称为 Map。
我喜欢这样的事情: 主要内容
Graphics t = Map.getGraphics();
paintComponent(t);
不在主要部分。
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawLine(0, 0, 50, 150);
}
我无法画任何东西。
最佳答案
我发现将组件完全居中的最简单方法是为容器提供 GridBagLayout,然后将单个组件添加到该容器,但不使用任何约束。
附带问题:您的代码在绘画方法中读取图像 - 从不这样做。相反,读取图像一次并将其存储在变量中。然后在绘画方法中显示它。还要确保在您的覆盖中调用 super 的绘画方法。
例如,
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class CenteredImage extends JPanel {
private static final String IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/f/f8/Portrait_d%27une_Femme_%C3%A0_sa_Toilette%2C_by_Titian%2C_"
+ "from_C2RMF_retouched.jpg/300px-Portrait_d%27une_Femme_%C3%A0_sa_"
+ "Toilette%2C_by_Titian%2C_from_C2RMF_retouched.jpg";
private BufferedImage img;
public CenteredImage(BufferedImage img) {
this.img = img;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}
@Override
public Dimension getPreferredSize() {
Dimension prefSize = super.getPreferredSize();
if (isPreferredSizeSet() || img == null) {
return super.getPreferredSize();
}
return new Dimension(img.getWidth(), img.getHeight());
}
private static void createAndShowGui() {
BufferedImage img = null;
try {
URL imgUrl = new URL(IMG_PATH);
img = ImageIO.read(imgUrl);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
CenteredImage mainPanel = new CenteredImage(img);
JFrame frame = new JFrame("CenteredImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 650));
frame.getContentPane().setLayout(new GridBagLayout());
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
编辑:使用包装器 JPanel,如评论中所述:
您在评论中指出:
Thank you for you anwser but I meant something else : There is : a JFrame/JDialog in borderLayout, I want to put "panneau" inside of it in the center position of it, maybe I don't know how to say it ... there are elements in "south", "north" ect ...
然后将生成图像的 JPanel 包装在另一个使用 GridBagLayout 的 JPanel 中,并将包装器 JPanel 添加到 JFrame BorderLayout.CENTER
private static void createAndShowGui() {
BufferedImage img = null;
try {
URL imgUrl = new URL(IMG_PATH);
img = ImageIO.read(imgUrl);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
CenteredImage centeredImagePanel = new CenteredImage(img);
JPanel wrapperPanel = new JPanel(new GridBagLayout());
wrapperPanel.add(centeredImagePanel);
JFrame frame = new JFrame("CenteredImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 650));
// frame.getContentPane().setLayout(new GridBagLayout());
frame.getContentPane().add(wrapperPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
关于java - 一个扩展 JPanel 的类,我想在我的 JFrame 中添加 CENTER 属性,它是一个 BorderLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47087950/