本文目錄一覽:
- 1、在窗體中,java顯示圖片怎麼做
- 2、java中怎樣在界面中顯示圖片
- 3、Java圖片顯示不出來,怎麼解決
- 4、java怎麼顯示本地圖片
- 5、為什麼Java里的圖片需要調整窗口大小才能正常顯示?
- 6、java隨機圖片顯示
在窗體中,java顯示圖片怎麼做
下面是一個JAVA顯示圖片的例子,請參考:
package com.tarena.java;
import t.Image;
import .File;
import .IOException;
import ageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
* 加載顯示圖象,需要JDK1.5或以上
*/
public class showtu extends JFrame {
public showtu(String bmpFile) {
Image image = null;
try {
image = ad(new File(bmpFile));
} catch (IOException ex) {
}
JLabel label = new JLabel(new ImageIcon(image));
add(label);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args){
final String fileName = “F:\\456備用\\亮個相.JPG”; //換成你要顯示的圖片
vokeLater(new Runnable(){
public void run(){
new showtu(fileName).setVisible(true);
}
});
}
}
java中怎樣在界面中顯示圖片
方法一:
[java] view plain copy
JLabel helloLabel = new JLabel(“New label”);
helloLabel.setIcon(new ImageIcon(“E:\\javaSE\u4EE3\u7801\\TimeManager\\asset\\hello.gif”));
helloLabel.setBackground(Color.BLACK);
helloLabel.setBounds(0, 0, 105, 50);
contentPane.add(helloLabel);
方法二:
[java] view plain copy
ImageIcon imagetoshow=new ImageIcon(urlofimagetoshow);
JLabel showimagelabel=new JLabel(imagetoshow);
this.getLayeredPane().add(showimagelabel,
new Integer(Integer.MIN_VALUE)); // 設置JLabel在最底層
showimagelabel.setBounds(0, 0, 500,150);
Java圖片顯示不出來,怎麼解決
有兩個問題:
圖片路徑沒有寫對,圖片在 src 下,圖片路徑應是 src/海洋.png,正確的寫法應是 image = new ImageIcon(“src/海洋.png”)
image = new ImageIcon(“src/海洋.png”) 應該放在 label = new JLabel(image); 前面。
如下例:
import javax.swing.*;
class JPanelDemo extends JPanel {
JLabel label;
JTextField text;
JButton button;
ImageIcon image;
public JPanelDemo() {
image = new ImageIcon(“src/test.png”);
label = new JLabel(image);
text = new JTextField(20);
button = new JButton(“確定”);
add(label);
add(text);
add(button);
}
}
public class App extends JFrame {
public App() {
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new JPanelDemo());
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
java怎麼顯示本地圖片
在面板上搞一個和面板一樣大的JLabel
然後,通過JFileChooser獲得路徑,利用這個圖片的路徑,構建一個ImageIcon
最後,根據這個ImageIcon去給JLabel對象setIcon(ImageIcon對象);
具體地:
1.panel.add(label,BorderLayout.CENTER);
2.ImageIcon icon = new ImageIcon(url);
3.label.setIcon(icon);
下面的代碼你把 .JPG改成BMP試試看,O(∩_∩)O~
package com.shlq.sample;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ImagePane extends JPanel
{
JLabel jl = null;
ImageIcon img = null;
public ImagePane()
{
img = new ImageIcon( “E:\\Picture\\1.jpg “);
jl = new JLabel(img);
this.setLayout(new BorderLayout());
this.add(jl, BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame test = new JFrame( “Image Pane “);
test.getContentPane().add(new ImagePane());
test.pack();
test.setVisible(true);
test.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
為什麼Java里的圖片需要調整窗口大小才能正常顯示?
窗口顯示的速度很快。當顯示帶有圖片的窗口時,如果你的圖片的加載不是同步的,可能窗口都顯示完了,你的圖片還沒有加載完,當然不會顯示圖片。而調整窗口的大小會導致重新繪製窗口,如果此時你的圖片加載完了,就會顯示出來,如果還是沒有加載完,也不會顯示。這與圖片的加載方式有關,應採用同步的加載方式,確保窗口顯示前,圖片已加載完。
java隨機圖片顯示
參考代碼. 你可以對照修改
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;
public class Picture extends JFrame {
private JLabel picture;
public Picture() {
ImageIcon[] icons = new ImageIcon[4];//四張圖的icon對象
String photopath = “”;
for (int i = 1; i = 4; i++) {
//這裡的目錄是我的圖片所在的目錄 1.gif~4.gif
photopath = “src/images/” + i + “.gif”;
Image img = Toolkit.getDefaultToolkit().createImage(photopath);
icons[i-1] = new ImageIcon(img);
}
picture = new JLabel();
JPanel jp = new JPanel();
jp.add(picture);
add(jp,BorderLayout.CENTER);
setBounds(500, 200, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
int n = Integer.parseInt(JOptionPane.showInputDialog(“input: “));
//先設置Jlabel應該顯示的圖片
picture.setIcon(icons[n-1]);
//然後才開始顯示窗口
this.setVisible(true);
}
public static void main(String[] args) {
new Picture();
}
}
原創文章,作者:WHSWI,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/330436.html