Java如何在框架里面显示图片,如何在框架中显示图像?

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:

22ru4.png

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;

}

}

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,可以使用Java Swing框架来创建窗体以及在窗体显示文本。您可以使用以下方法来排版窗体的文本: 1. 使用JLabel组件:JLabel是一个用于显示文本或图像的Swing组件。您可以使用JLabel的setHorizontalAlignment()和setVerticalAlignment()方法来设置文本在组件的水平和垂直对齐方式。例如: ```java JLabel label = new JLabel("Hello World"); label.setHorizontalAlignment(SwingConstants.CENTER); // 设置为居对齐 label.setVerticalAlignment(SwingConstants.TOP); // 设置为顶部对齐 ``` 在此示例,我们创建了一个JLabel组件,并将其文本设置为“Hello World”。然后,我们使用setHorizontalAlignment()方法将文本设置为居对齐,使用setVerticalAlignment()方法将文本设置为顶部对齐。 2. 使用JTextArea组件:JTextArea是一个多行文本输入框,您可以使用它来显示带格式的文本。您可以使用JTextArea的setAlignmentX()和setAlignmentY()方法来设置文本在组件的水平和垂直对齐方式。例如: ```java JTextArea textArea = new JTextArea("Hello World"); textArea.setAlignmentX(Component.CENTER_ALIGNMENT); // 设置为居对齐 textArea.setAlignmentY(Component.TOP_ALIGNMENT); // 设置为顶部对齐 ``` 在此示例,我们创建了一个JTextArea组件,并将其文本设置为“Hello World”。然后,我们使用setAlignmentX()方法将文本设置为居对齐,使用setAlignmentY()方法将文本设置为顶部对齐。 希望这可以帮助您排版窗体的文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值