本文目錄一覽:
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圖片顯示一半
你看一下圖片文件大小,如果圖片文件變小了,說明上傳的時候圖片文件沒有完整地被上傳,這個時候需要你找一個網路好的地方重新上傳。如果圖片文件大小一樣,把文件下載回本地,用圖片工具打開看看圖片是不是能完整顯示,如果能完整顯示,那就是用戶那邊的網路不夠順暢造成的,如果不能完整顯示,則需要你找一個網路好的地方重新上傳原圖。總之,就是要想辦法確定是伺服器上圖片本身有問題還是用戶網路有問題。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237062.html