3

我是 Java 新手,我想向 Composite 添加背景图像。我只能使用 SWT,不能使用 JFace。我正在使用 eclipse indigo IDE (3.8),当我想设置背景图像时,首先我将 Image 类初始化为图像对象,但是当我按 CTRL + SPACE 帮助选择构造函数时,我有 5 个不同的构造函数。我不知道该选择什么。

我必须使用相对路径。该包具有以下结构:

org.mypackage.program //the package name
org.mypackage.program/src/org.mypackage.program //the plugin-project automated created classes
org.mypackage.program/src/views // all views

org.mypackage.program/car_image.jpg // the image what I would set in background
org.mypackage.program/views/View.java // the class where I want to set the background

这是我做的,但它不起作用:

Image image = new Image(Display.getCurrent(),  this.getClass().getClassLoader().getResource("car_image.jpg"));
compImage.setBackgroundImage(image);

我也是 OOP 的新手,我只编写了结构化/模块程序。

4

3 回答 3

2

请尝试以下代码:-

请注意,如何提及相对路径

.\\src\\org\\mypackage\\program\\car_image.gif

这里 src 是应用程序的根文件夹。


package org.mypackage.program;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Demo {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Display display = new Display();
            Shell shell = new Shell(display);
            Image img;
            shell.setMaximized(true);
            img= new Image(display,".\\src\\org\\mypackage\\program\\car_image.gif");
            Composite comp= new Composite(shell, SWT.NONE);
            comp.setBackgroundImage(img);
            shell.setLayout(new FillLayout());
            shell.open();
            while (!shell.isDisposed())
            {       
                if (!display.readAndDispatch())
                {         
                    display.sleep();       
                }     
            }     
            display.dispose();
    }
}
于 2012-11-29T05:58:49.177 回答
2

或者您可以尝试使用图形方式,使您能够从组件的起点 (x,y) 绘制图像。当然,您可以缩放图像,使其适合您想要用特定图像覆盖的空间Image 对象的 scale 方法。示例:

package org.mypackage.program;

//your imports here

public class MyClassName extends JFrame{

public Image myImg;
public int startingX = 0,startingY = 0,newWidth = 300,newHeight = 300;

public static void main(String[] args){
    MyClassName frame = new MyClassName();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setVisible(true);
}

public MyClassName(){
    super("JFrame title")

    myImg = Toolkit().getDefaultToolkit().getImage(MyClassName.class.getResource("org/mypackage/program/car_image.jpg"));
    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setMaximized(true);

    myImg = myImg.getScaledInstance(newWidth,newHeight,0);

    Composite comp= new Composite(shell, SWT.NONE){

        protected void paintComponent(Graphics g){
            g.drawImage(myImg, startingX, startingY, this);
        }

    };


}
}
于 2012-12-01T17:46:57.530 回答
0

使用 swt/jface 应用程序设置动态或相对图像路径


Button btnNewButton = new Button(parent, SWT.NONE);
//Absolute path never use
//btnNewButton.setImage(SWTResourceManager.getImage("D:\\Bharat\\Icon\\uncheck.png"));
btnNewButton.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/delete.png"));
btnNewButton.setBounds(18, 370, 68, 23);

/////Launching time your application can not generate some image related problem 

1)Control: Drag and drop Button control
2)Add image to Button control 
3)select image chooser property and click plugin resource radiobutton
4)select your project,select your image folder after set image to button

以编程方式有用这个答案....祝您有美好的一天....谢谢

于 2017-01-19T11:04:35.887 回答