如何在框架中显示图像? [英] How to display an image in a frame?
问题描述
public class guikopie extends javax.swing.JFrame {
public guikopie(){
a = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
a.setIcon(new javax.swing.ImageIcon(C:\\\Users\\\Public\\\Pictures\\\Sample Pictures\\\Tulpen.jpg));
add(a); //这里我将其添加到jlabel
pack();
}
public static void main(String args []){
java.awt.EventQueue.invokeLater(new Runnable(){
public void run {
new guikopie()。setVisible(true);
}
});
}
private javax.swing.JLabel a;
}
我的问题是:为什么这段代码不显示图片? p>
我认为人们可能会恨我重复:P:
- 不扩展JFrame类
- 类名以字母开头,即
Guikopie
另外取决于背景是用于ie,如果它的标志将被添加到 JPanel
然后使用 JLabel
是罚款,但是,如果它被用作背景它不是;因为它会随着添加更多组件而移动,因此我们不应该将背景添加为组件,而不是在组件上绘制背景。
关于你的问题
我的问题是:为什么这段代码不显示图片?
你的代码对我来说是完美的,所以你的图片的位置必须是不正确的。
我做了一个简短的例子,显示如何添加一个图像
到 JPanel
背景,然后将 JPanel
添加到 JFrame
,它还包括调整大小图片的类 ImgUtils
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JFrameWithPicture {
public JFrameWithPicture()throws MalformedURLException,IOException {
initComponents();
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run (){
try {
new JFrameWithPicture();
} catch(Exception ex){
ex.printStackTrace();
}
}
});
}
private void initComponents()throws MalformedURLException,IOException {
JFrame frame = new JFrame(Frame with JPanel and background);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Image background = ImageUtils.scaleImage(300,300,ImageIO.read(new URL(http://images2.layoutsparks.com/1/98191/naruto-14-red-design。 JPG)));
final Dimension jpanelDimensions = new Dimension(new ImageIcon(background).getIconWidth(),new ImageIcon(background).getIconHeight());
frame.add(new JPanel(){
@Override
protected void paintComponent(Graphics grphcs){
super.paintComponent(grphcs);
grpcs.drawImage(background,0,0,this);
}
@Override
public Dimension getPreferredSize(){
return jpanelDimensions;
}
});
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
}
class ImageUtils {
public static BufferedImage scaleImage(int width,int height,String filename){
BufferedImage bi ;
try {
ImageIcon ii = new ImageIcon(filename);
bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d =(Graphics2D)bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(),0,0,width,height,null);
} catch(Exception e){
return null;
}
return bi;
}
static Image scaleImage(int width,int height,BufferedImage filename){
BufferedImage bi;
try {
bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d =(Graphics2D)bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(filename,0,0,width,height,null);
} catch(Exception e){
return null;
}
return bi;
}
}
或者您可能想使用 Image#getScaledInstance( int width,int height,int hints),但这有其危险,主要问题是:
Image.getScaledInstance()
不返回已完成的缩放图像。
当使用图像
像素时,会留下很多缩放功能。
I am coding a gui and I wanted to use embedde some pictures, but before embedding it in my main program I wrote that code to test it:
public class guikopie extends javax.swing.JFrame{
public guikopie() {
a = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
a.setIcon(new javax.swing.ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulpen.jpg"));
add(a);//here i add it to the jlabel
pack();
}
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new guikopie().setVisible(true);
}
});
}
private javax.swing.JLabel a;
}
My question is: Why does this code do not display the picture?
I think people may hate me for repeating this :P:
- Dont extend JFrame class
- Class names begin with captial letter i.e
Guikopie
Also depending on what the background is being used for i.e if its a logo that will be added to a specific location on the JPanel
then using a JLabel
is fine, however, if its being used as a background it is not; because it will moved around as more components are being added, thus we should not add the background as a component rather we paint the background on the component.
As for your question:
My question is: Why does this code do not display the picture?
your code works perfect for me thus the location of your picture must be incorrect.
I did a short example showing how to add a Image
to JPanel
background and then add JPanel
to JFrame
, it also includes class ImgUtils
for resizing picture:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JFrameWithPicture {
public JFrameWithPicture() throws MalformedURLException, IOException {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new JFrameWithPicture();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
private void initComponents() throws MalformedURLException, IOException {
JFrame frame = new JFrame("Frame with JPanel and background");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Image background = ImageUtils.scaleImage(300, 300, ImageIO.read(new URL("http://images2.layoutsparks.com/1/98191/naruto-14-red-design.jpg")));
final Dimension jpanelDimensions = new Dimension(new ImageIcon(background).getIconWidth(), new ImageIcon(background).getIconHeight());
frame.add(new JPanel() {
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(background, 0, 0, this);
}
@Override
public Dimension getPreferredSize() {
return jpanelDimensions;
}
});
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
}
class ImageUtils {
public static BufferedImage scaleImage(int width, int height, String filename) {
BufferedImage bi;
try {
ImageIcon ii = new ImageIcon(filename);
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(), 0, 0, width, height, null);
} catch (Exception e) {
return null;
}
return bi;
}
static Image scaleImage(int width, int height, BufferedImage filename) {
BufferedImage bi;
try {
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(filename, 0, 0, width, height, null);
} catch (Exception e) {
return null;
}
return bi;
}
}
Alternatively you may want to resize using Image#getScaledInstance(int width,int height,int hints), but this has its perils, the main problem being:
Image.getScaledInstance()
does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used.
这篇关于如何在框架中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!