带有背景图像的 JPanel

发布于 2024-10-04 10:36:02 字数 1783 浏览 1 评论 0原文

我有以下代码:

class ImagePanel extends JPanel {

          private Image img;

          public ImagePanel(String img) {
            this(new ImageIcon(img).getImage());
          }

          public ImagePanel(Image img) {
            this.img = img;
            Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
            setLayout(null);
          }

          public void paintComponent(Graphics g) {
            g.drawImage(img, 0, 0, null);
          }
        }

和以下代码:

public GUIVenDetails() throws MalformedURLException, IOException{

        mapPanel = new ImagePanel("http://www.netstate.com/states/symb/gamebirds/images/wild_turkey.jpg");
        mapPanel.setPreferredSize(new Dimension(200,200));
        mapPanel.setMinimumSize(mapPanel.getPreferredSize());
        mapPanel.setMaximumSize(mapPanel.getPreferredSize());


        add(mapPanel);
    }

public static void main(String[] args) throws MalformedURLException, IOException, XPathExpressionException, SAXException, ParserConfigurationException {


        GUIVenDetails gui = new GUIVenDetails();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = gui.getSize();

        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        JFrame f=new JFrame();
        f.setSize(new Dimension(400,800));
        f.setLocation(windowX, windowY); 

        f.add(gui);
        f.setVisible(true);
    }

当我运行代码时,除了白色之外我什么也看不到......这是为什么?

I have the following code:

class ImagePanel extends JPanel {

          private Image img;

          public ImagePanel(String img) {
            this(new ImageIcon(img).getImage());
          }

          public ImagePanel(Image img) {
            this.img = img;
            Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
            setLayout(null);
          }

          public void paintComponent(Graphics g) {
            g.drawImage(img, 0, 0, null);
          }
        }

and the following:

public GUIVenDetails() throws MalformedURLException, IOException{

        mapPanel = new ImagePanel("http://www.netstate.com/states/symb/gamebirds/images/wild_turkey.jpg");
        mapPanel.setPreferredSize(new Dimension(200,200));
        mapPanel.setMinimumSize(mapPanel.getPreferredSize());
        mapPanel.setMaximumSize(mapPanel.getPreferredSize());


        add(mapPanel);
    }

public static void main(String[] args) throws MalformedURLException, IOException, XPathExpressionException, SAXException, ParserConfigurationException {


        GUIVenDetails gui = new GUIVenDetails();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = gui.getSize();

        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        JFrame f=new JFrame();
        f.setSize(new Dimension(400,800));
        f.setLocation(windowX, windowY); 

        f.add(gui);
        f.setVisible(true);
    }

When I run the code, I don't see anything but white... Why is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2024-10-11 10:36:02

这不是从互联网上读取文件的方式。尝试类似:

String imagePath = "http://duke.kenai.com/misc/Bullfight.jpg";
Image image = null;

try 
{
    URL url = new URL(imagePath);
    image = ImageIO.read(url);
} 
catch (IOException e) 
{
     System.out.println(e);
}

或者您仍然可以使用

new ImageIcon(new URL(...));

但是字符串不会仅仅因为它以“http”开头而成为 URL。

That is not how you read a file from the internet. Try something like:

String imagePath = "http://duke.kenai.com/misc/Bullfight.jpg";
Image image = null;

try 
{
    URL url = new URL(imagePath);
    image = ImageIO.read(url);
} 
catch (IOException e) 
{
     System.out.println(e);
}

Or you can still use

new ImageIcon(new URL(...));

But a String does not become a URL just because it starts with "http".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文