[英]Background contentPane of a jframe interferes with the jpanel on top of it
I have a jframe with a jpanel on top of it and the panel often has to change its contents with repaint() and revalidate(). 我有一个jframe,上面有一个jpanel,面板通常必须使用repaint()和revalidate()来更改其内容。 I have managed to place the images, texts and buttons just the I way I want them to be on this jpanel.
我设法将图像,文本和按钮按我希望它们放在此jpanel上的方式放置。 Everything is working, but now I am trying to set a background to the jframe, that does not interfere with the contents above it.
一切正常,但是现在我尝试为jframe设置背景,这不会干扰其上方的内容。 For example, if there is a drawing of a tree, it should appear behind the text of the jpanel, without disrupting it.
例如,如果有一棵树的绘图,它应该出现在jpanel文本的后面,而不会破坏它。 I found that the thing that semi works is to use setContentPane() on the jframe, adding a class, that has extended jpanel and has overrode paintComponent().
我发现半有效的方法是在jframe上使用setContentPane(),添加一个类,该类扩展了jpanel并覆盖了paintComponent()。 Everything appears on the screen, but the text is squashed vertically and the elements are moved towards the top of the frame.
一切都出现在屏幕上,但是文本被垂直压缩,并且元素向框架顶部移动。
If instead of using setContentPane() I just add the background class to the frame, it doesn't appear, no matter the setOpaque() of the jpanel. 如果不使用setContentPane()而是将背景类添加到框架中,则不管jpanel的setOpaque()都不会显示该背景类。 I also tried using jLayeredPane, since the things I read on the internet suggest that this is the right answer.
我还尝试使用jLayeredPane,因为我在互联网上阅读的内容表明这是正确的答案。 However, I couldn't make it work and the background remained hidden.
但是,我无法使其正常工作,并且背景仍然隐藏。
private final int WIDTH = 1024;
private final int HEIGHT = 768;
Frame()
{
JFrame frame = new JFrame();
panel = new JPanel();
gbc = new GridBagConstraints();
//Unrelated elements
//font = new Font(Font.MONOSPACED, Font.PLAIN, 20);
//border = BorderFactory.createEmptyBorder();
//imageResizer = new ImageResizer();
frame.setTitle("Shady Path");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(new ImageIcon("res/human.png").getImage());
frame.setContentPane(new DrawPanel());
panel.setLayout(new GridBagLayout());
panel.setOpaque(false);
gbc.anchor = GridBagConstraints.PAGE_START;
frame.add(panel);
frame.setVisible(true);
}
//One of the two methods that change the contents of the jpanel
void appendMain(String mainImage, JTextArea mainText, JButton button)
{
panel.removeAll();
image = new JLabel(imageResizer.resize(200, 200, mainImage));
gbc.insets = new Insets(0, 0, 30, 0);
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(image, gbc);
formatText(mainText);
panel.add(mainText, gbc);
button.setFont(font);
button.setForeground(Color.WHITE);
button.setBackground(Color.BLACK);
button.setBorder(border);
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(50, 0, 70, 0);
panel.add(button, gbc);
panel.revalidate();
panel.repaint();
}
//This is for the text formating
private void formatText(JTextArea baseText)
{
baseText.setEditable(false);
baseText.setForeground(Color.WHITE);
baseText.setFont(font);
baseText.setLineWrap(true);
baseText.setWrapStyleWord(true);
baseText.setMargin(new Insets(0, 300, 0, 300));
baseText.setOpaque(false);
gbc.insets = new Insets(30, 0, 0, 0);
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
}
//The following code is for the paintComponent() class
//The imageResizer is another class that I made, but it just resizes images and it is unrelated.
public class DrawPanel extends JPanel
{
private Image image;
public DrawPanel()
{
ImageResizer imageResizer = new ImageResizer();
ImageIcon imageIcon = imageResizer.resize(1024, 768, "res/test.png");
image = imageIcon.getImage();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
Well... It looks like @HovercraftFullOfEels was right with his comment. 好吧……@HovercraftFullOfEels似乎对他的评论是正确的。 Literally, I just had to set the layout of the DrawPanel to a BorderLayout and everything was fixed.
从字面上看,我只需要将DrawPanel的布局设置为BorderLayout即可,并且一切都已固定。
public DrawPanel()
{
this.setLayout(new BorderLayout());
ImageResizer imageResizer = new ImageResizer();
ImageIcon imageIcon = imageResizer.resize(1024, 768, "res/test.png");
image = imageIcon.getImage();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.