java components_java – JComponents没有显示图片背景?

在这里首先看一下这个小例子,如果你明白这里发生了什么,请告诉我.然后我们只会慢慢地走得更远.试着通过这个例子,我将向你展示如何在JPanel上绘制

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.URL;

import javax.imageio.ImageIO;

import javax.swing.*;

public class PaintingExample {

private CustomPanel contentPane;

private void displayGUI() {

JFrame frame = new JFrame("Painting Example");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

contentPane = new CustomPanel();

frame.setContentPane(contentPane);

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String... args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new PaintingExample().displayGUI();

}

});

}

}

class CustomPanel extends JPanel {

private BufferedImage image;

public CustomPanel() {

setOpaque(true);

setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));

try {

/*

* Since Images are Application Resources,

* it's always best to access them in the

* form of a URL, instead of File, as you are doing.

* Uncomment this below line and watch this answer

* of mine, as to HOW TO ADD IMAGES TO THE PROJECT

* https://stackoverflow.com/a/9866659/1057230

* In order to access images with getClass().getResource(path)

* here your Directory structure has to be like this

* Project

* |

* ------------------------

* | |

* bin src

* | |

* --------- .java files

* | |

* package image(folder)

* ( or |

* .class 404error.jpg

* files, if

* no package

* exists.)

*/

//image = ImageIO.read(

// getClass().getResource(

// "/image/404error.jpg"));

image = ImageIO.read(new URL(

"http://i.imgur.com/8zgHpH8.jpg"));

} catch(IOException ioe) {

System.out.println("Unable to fetch image.");

ioe.printStackTrace();

}

}

/*

* Make this one customary habbit,

* of overriding this method, when

* you extends a JPanel/JComponent,

* to define it's Preferred Size.

* Now in this case we want it to be

* as big as the Image itself.

*/

@Override

public Dimension getPreferredSize() {

return (new Dimension(image.getWidth(), image.getHeight()));

}

/*

* This is where the actual Painting

* Code for the JPanel/JComponent

* goes. Here we will draw the image.

* Here the first line super.paintComponent(...),

* means we want the JPanel to be drawn the usual

* Java way first (this usually depends on the opaque

* property of the said JComponent, if it's true, then

* it becomes the responsibility on the part of the

* programmer to fill the content area with a fully

* opaque color. If it is false, then the programmer

* is free to leave it untouched. So in order to

* overcome the hassle assoicated with this contract,

* super.paintComponent(g) is used, since it adheres

* to the rules, and performs the same task, depending

* upon whether the opaque property is true or false),

* then later on we will add our image to it, by

* writing the other line, g.drawImage(...).

*/

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, 0, 0, this);

}

}

编译并运行:

>启动命令提示符/ terminal / cmd.并移动到所提到的位置

通过Project文件夹

>现在使用以下代码编译代码:

C:\Project>javac -d bin src\*.java

>现在通过发出命令移动到bin文件夹:

C:\Project>cd bin

>进入bin文件夹后,发出以下命令运行:

C:\Project\bin>java PaintingExample

以下是使用JLabel作为图像基础的代码:

import java.awt.*;

import java.net.MalformedURLException;

import java.net.URL;

import javax.swing.*;

public class LabelExample {

private JPanel contentPane;

private JLabel imageLabel;

private void displayGUI() {

JFrame frame = new JFrame("Label Example");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

contentPane = new JPanel();

contentPane.setOpaque(true);

contentPane.setBorder(

BorderFactory.createLineBorder(Color.BLACK, 5));

//imageLabel = new JLabel(

// new ImageIcon(

// getClass().getResource(

// "/image/404error.jpg")));

try {

imageLabel = new JLabel(new ImageIcon(

new URL("http://i.imgur.com/8zgHpH8.jpg")));

} catch(MalformedURLException mue) {

System.out.println(

"Unable to get Image from"

+ "the Resource specified.");

mue.printStackTrace();

}

contentPane.add(imageLabel);

frame.setContentPane(contentPane);

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String... args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new LabelExample().displayGUI();

}

});

}

}

以下是上述代码的输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值