在NetBeans 7.0中将背景图像添加到JFrame时遇到问题 [英] Trouble Adding Background Image to JFrame in Netbeans 7.0

查看:52
本文介绍了在NetBeans 7.0中将背景图像添加到JFrame时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,请多多包涵.

This is my first question, so please bear with me.

我正在开发一个应用程序(我已经对其进行了充分的设计).现在我处于编码阶段,在将背景图像放置在JFrame上的同时仍无法让它扮演容器的角色时遇到了麻烦,因此我可以在其上放置按钮以及类似性质的东西.

I am working on an application (which I have already fully-designed). Now I am at the coding stage and I am having trouble placing a background image on the JFrame while still allowing it to play its role as a Container so I can put buttons on it and things of that nature.

我已经在Netbeans 7.0中创建了这个JFrame类文件,如果有人可以告诉我如何通过Netbeans界面来做到这一点,那将是很棒的(如果没有的话,只是代码就可以了).

I have created this JFrame class file in Netbeans 7.0 and if someone could tell me how to do this through the interface of Netbeans that would be great (if not, just the code would be fine).

我已经知道我应该重写paintComponent方法(我已经做过,但是我的图像文件没有显示).还有第二个问题,我不想放置图像的完整文件路径,我的源文件放在包装中,现在我对放置图像文件的位置感到很困惑.

I already know that I am supposed to override the paintComponent method (which I have done already, but my image file is not showing). Also I have a second questions, I don't want to put the full file-path for the image, I have my source files in packages and now I am quite confused as to where I am to put my image files.

initComponoents ()是Netbeans生成的确定JFrame属性的方法.

initComponoents() is the generated method by Netbeans that determines the properties of the JFrame.

public class TinyTowerOrganizerInterface extends javax.swing.JFrame {

/** Creates new form TinyTowerOrganizerInterface */
    Image backgroundImage = Toolkit.getDefaultToolkit().getImage("D:/Java/TinyTowerOrganizer/Images/Background.jpg");

public TinyTowerOrganizerInterface() throws IOException {

    initComponents(); 
    class BackgroundPanel extends javax.swing.JPanel{
        private Image image;
        public BackgroundPanel(Image image){
            this.image = image;
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);

        }

    }

}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Tiny Tower Organizer");
    setFont(new java.awt.Font("Pixelated", 0, 18)); // NOI18N
    setMinimumSize(new java.awt.Dimension(900, 500));
    setName("frame"); // NOI18N
    setResizable(false);
    setUndecorated(true);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 900, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 500, Short.MAX_VALUE)
    );

    pack();
}


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                new TinyTowerOrganizerInterface().setVisible(true);
            } catch (IOException ex) {
                Logger.getLogger(TinyTowerOrganizerInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

推荐答案

我已经知道我应该重写paintComponent方法

I already know that I am supposed to override the paintComponent method

JFrame没有paintComponent(...)方法.

JFrame does not have a paintComponent(...) method.

相反,您应该扩展JPanel(或JComponent)并在paintComponent()方法中添加自定义绘画.然后将面板添加到框架.

Instead you should extend JPanel (or JComponent) and add your custom painting in the paintComponent() method. Then you add the panel to the frame.

此外,不要忘记重写面板的getPreferredSize()方法以返回图像的大小.

Also, don't forget to override the getPreferredSize() method of the panel to return the size of the image.

首先,当您发布代码时,请发布 SSCCE ,以便我们复制并执行代码.我在下面添加了一个简单的SSCCE.

First of all when you post code post a SSCCE so we can copy and execute the code. I've include a simple SSCCE below.

有不同的问题.

第一个问题是getImage()方法异步读取图像,因此当显示框架时,图像没有完全加载,并且没有任何显示内容,运行代码时可以看到.而是使用ImageIO读取图像.

The first problem is that the getImage() method reads the image async so when the frame is displayed, the image is not completely loaded and there is nothing to display as you can see when run the code. Instead use ImageIO to read the image.

我什至想成就什么

What is it that I even want to get accomplished

进行上述更改并运行代码时,即使已调用pack(),您仍然只会看到一个小框架.那是因为您没有向poanel添加任何组件,所以默认首选大小是(10,10),因为您使用的是FlowLayout.因此,您需要重写getPreferredSize()方法以返回图像的大小,以便可以正确包装面板.

When you make the above change and run the code you will still only see a small frame even though pack() have been invoked. That is because you haven't added any components to the poanel so the default preferred size is (10, 10) because you are using a FlowLayout. So you need to override the getPreferredSize() method to return the size of the image so the panel can be packed properly.

import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;

public class MyApplication extends javax.swing.JFrame
{

/** Creates new form MyApplication */
    Image backgroundImage = Toolkit.getDefaultToolkit().getImage("mong.jpg");

    public MyApplication() throws IOException
    {

        this.setContentPane(new JPanel()
        {

            @Override
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.drawImage(backgroundImage, 0, 0, null);
            }
        });

        pack();
        setVisible(true);
    }


    public static void main(String[] args)
        throws Exception
    {
        new MyApplication();
    }
}

这篇关于在NetBeans 7.0中将背景图像添加到JFrame时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆