gpt4 book ai didi

java - 嵌套 JPanel 中的背景图像?

转载 作者:行者123 更新时间:2023-11-29 08:06:09 25 4
gpt4 key购买 nike

我有一个 JPanel,其中包含另外 2 个 JPanel。位于左侧(leftBox)和右侧(rB),我想在右侧的JPanel(rB)上添加一个背景图片。

但是我得到的结果是

http://i.imgur.com/tHz1x.jpg

我想要的结果

http://i.imgur.com/xHbpx.jpg

public void paintComponent(Graphics g)
{
//this.paintComponent(g);
if(wdimage != null) g.drawImage(wdimage,0,0,800,800,rB); //(image,location x, location y, size x, size y)

}

rB 面板挡住了图像,我想要的是在 JPanel 上显示图像,然后在 JPanel 和图像之上添加一些 jlabels 和文本字段。

最佳答案

BACKGROUND IMAGE OUTPUT

这里显示没有任何问题,看看:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class PanelExample
{
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Panel Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBorder(
BorderFactory.createMatteBorder(
5, 5, 5, 5, Color.WHITE));
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new BorderLayout(10, 10));

ImagePanel imagePanel = new ImagePanel();
//imagePanel.createGUI();
BlankPanel blankPanel = new BlankPanel();

contentPane.add(blankPanel, BorderLayout.LINE_START);
contentPane.add(imagePanel, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PanelExample().createAndDisplayGUI();
}
});
}
}

class ImagePanel extends JPanel
{
private BufferedImage image;

public ImagePanel()
{
setOpaque(true);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
try
{
image = ImageIO.read(new URL("http://gagandeepbali.uk.to/gaganisonline/images/background.jpg"));
}
catch(Exception e)
{
e.printStackTrace();
}
createGUI();
}

public void createGUI()
{
setLayout(new GridBagLayout());
JPanel loginPanel = new JPanel();
loginPanel.setOpaque(false);
loginPanel.setLayout(new GridLayout(2, 2, 2, 2));
JLabel userLabel = new JLabel("USERNAME : ");
userLabel.setForeground(Color.WHITE);
JTextField userField = new JTextField(10);
JLabel passLabel = new JLabel("PASSWORD : ");
passLabel.setForeground(Color.WHITE);
JPasswordField passField = new JPasswordField(10);

loginPanel.add(userLabel);
loginPanel.add(userField);
loginPanel.add(passLabel);
loginPanel.add(passField);

add(loginPanel);
System.out.println("I am finished");
}

@Override
public Dimension getPreferredSize()
{
return (new Dimension(300, 300));
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}

class BlankPanel extends JPanel
{
public BlankPanel()
{
setOpaque(true);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
setBackground(Color.WHITE);
}

@Override
public Dimension getPreferredSize()
{
return (new Dimension(100, 300));
}
}

关于java - 嵌套 JPanel 中的背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11113159/

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