本文目錄一覽:
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怎麼顯示本地圖片
在面板上搞一個和面板一樣大的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 在屏幕上顯示圖形
import java.awt.Graphics;
import javax.swing.JFrame;
public class Test456 extends JFrame{
public static void main(String[] str){
//重寫了JFrame中的paint()方法(該方法實際從JComponent中繼承)。
JFrame frame = new JFrame(“Frame”){
public void paint(Graphics e) {
e.drawString(“畫一條直線、矩形”, 30, 60);
e.drawLine(30, 90, 100, 90);
e.drawRect(30, 120, 100, 50);
}
};
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JAVA怎麼在指定位置顯示圖片
圖片是保存在你的java工程中的。至於調取圖片的話,如果是bs架構的web方式直接用相對路徑 用img標籤就可以了。
如果是cs架構的話,調用圖片以及顯示的方法可以在網上查一下。都是api裡面的。
主要是把圖片放置在你的java工程的一個固定目錄中,然後根據需要去調取顯示。
原創文章,作者:XWXC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/150280.html