導航:首頁 > 圖片大全 > 如何引入圖片到Java

如何引入圖片到Java

發布時間:2023-08-23 13:06:51

① Java程序怎樣導入圖片

圖片名字是img1.JPG,你放到本程序的目錄下面就好了。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class TestMenu1 extends JFrame{
private JTextArea textArea=new JTextArea();
private JMenuBar menuBar=new JMenuBar();
private JMenu fileMenu=new JMenu("文件");
private JMenu viewMenu=new JMenu("視圖");
private JMenu toolMenu=new JMenu("工具欄");
private JMenuItem[] fileItem={new JMenuItem("新建"),new JMenuItem("打開"),new JMenuItem("保存"),new JMenuItem("退出")};
private JMenuItem[] viewItem={new JMenuItem("普通"),new JMenuItem("頁面")};
private JCheckBoxMenuItem[] toolItem={new JCheckBoxMenuItem("常用"),new JCheckBoxMenuItem("繪圖"),new JCheckBoxMenuItem("符號欄")};
private JPanel jPanel1;
private JLabel jLabel;
private Image image;
private ImageIcon imageIcon ;

public TestMenu1(String title){
super(title);
jPanel1=new JPanel();
image = Toolkit.getDefaultToolkit().getImage("img1.JPG");
jLabel = new JLabel();
imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
add(jLabel, BorderLayout.NORTH);
add(jPanel1, BorderLayout.SOUTH);
setVisible(true);
setSize(600, 600);

ActionListener actListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(((JMenuItem)e.getSource()).getText()=="退出"){
System.exit(0);
}else{
textArea.setText(((JMenuItem)e.getSource()).getText());
}
}
};
ItemListener itemListener=new ItemListener(){
public void itemStateChanged(ItemEvent e){
String str=new String("");
for(int i=0;i<toolItem.length;i++){
if(toolItem[i].isSelected()){
str+=toolItem[i].getText()+"\n";
}
}
if(str.length()==0){
textArea.setText("沒有復選");
}else{
textArea.setText(str);
}
}
};
for(int i=0;i<fileItem.length;i++){
fileItem[i].addActionListener(actListener);
fileMenu.add(fileItem[i]);
if(i==2){
fileMenu.addSeparator();
}
}
for(int i=0;i<viewItem.length;i++){
viewItem[i].addActionListener(actListener);
viewMenu.add(viewItem[i]);
}
viewMenu.addSeparator();
viewMenu.add(toolMenu);
for(int i=0;i<toolItem.length;i++){
toolItem[i].addItemListener(itemListener);
toolMenu.add(toolItem[i]);
}
menuBar.add(fileMenu);
menuBar.add(viewMenu);
setJMenuBar(menuBar);
add(new JScrollPane(textArea));
}

public static void main(String[] args){
//TestMenu frame=
new TestMenu1("DEMO");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(300,180);
//frame.setVisible(true);
}
}

② java中怎樣導入圖片

不明比你的意思,你是想要將圖片放到java項目中去還是用java寫代碼導入圖片。前者:直接復制粘貼就OK了,銀州後者:你將圖片看成文件就OK了,和文件導入完全相同的,你肆胡可以利用Struts2導裂搏攔入

怎麼在Java里添加背景圖片

//一個小例子
//注意:
//當前目錄下要有一個p.jpg文件
//壓縮這個文件使它的大小盡量小,最好只有幾十KB
//否則無法顯示背景圖片

import javax.swing.*;
import java.awt.*;

public class Demo extends JFrame {
public static void main(String[] args) {
new Demo();
}
public Demo() {

//載入圖片
pic=Toolkit.getDefaultToolkit().getImage("p.jpg");
MediaTracker t=new MediaTracker(this);
t.addImage(pic,1);
try
{
t.waitForID(1);
}
catch(Exception e)
{

}

setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
//覆蓋(重寫)paint方法以在窗口中添加一張圖片
public void paint(Graphics g) {
g.drawImage(pic,0,0,null);
}
Image pic;
}

④ JAVA程序如何插入圖片

圖片名字是img1.JPG,你放到本程序的目錄下面就好了。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class TestMenu1 extends JFrame{
private JTextArea textArea=new JTextArea();
private JMenuBar menuBar=new JMenuBar();
private JMenu fileMenu=new JMenu("文件");
private JMenu viewMenu=new JMenu("視圖");
private JMenu toolMenu=new JMenu("工具欄");
private JMenuItem[] fileItem={new JMenuItem("新建"),new JMenuItem("打開"),new JMenuItem("保存"),new JMenuItem("退出")};
private JMenuItem[] viewItem={new JMenuItem("普通"),new JMenuItem("頁面")};
private JCheckBoxMenuItem[] toolItem={new JCheckBoxMenuItem("常用"),new JCheckBoxMenuItem("繪圖"),new JCheckBoxMenuItem("符號欄")};
private JPanel jPanel1;
private JLabel jLabel;
private Image image;
private ImageIcon imageIcon ;

public TestMenu1(String title){
super(title);
jPanel1=new JPanel();
image = Toolkit.getDefaultToolkit().getImage("img1.JPG");
jLabel = new JLabel();
imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
add(jLabel, BorderLayout.NORTH);
add(jPanel1, BorderLayout.SOUTH);
setVisible(true);
setSize(600, 600);

ActionListener actListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(((JMenuItem)e.getSource()).getText()=="退出"){
System.exit(0);
}else{
textArea.setText(((JMenuItem)e.getSource()).getText());
}
}
};
ItemListener itemListener=new ItemListener(){
public void itemStateChanged(ItemEvent e){
String str=new String("");
for(int i=0;i<toolItem.length;i++){
if(toolItem[i].isSelected()){
str+=toolItem[i].getText()+"\n";
}
}
if(str.length()==0){
textArea.setText("沒有復選");
}else{
textArea.setText(str);
}
}
};
for(int i=0;i<fileItem.length;i++){
fileItem[i].addActionListener(actListener);
fileMenu.add(fileItem[i]);
if(i==2){
fileMenu.addSeparator();
}
}
for(int i=0;i<viewItem.length;i++){
viewItem[i].addActionListener(actListener);
viewMenu.add(viewItem[i]);
}
viewMenu.addSeparator();
viewMenu.add(toolMenu);
for(int i=0;i<toolItem.length;i++){
toolItem[i].addItemListener(itemListener);
toolMenu.add(toolItem[i]);
}
menuBar.add(fileMenu);
menuBar.add(viewMenu);
setJMenuBar(menuBar);
add(new JScrollPane(textArea));
}

public static void main(String[] args){
//TestMenu frame=
new TestMenu1("DEMO");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(300,180);
//frame.setVisible(true);
}
}

⑤ java如何引用本地圖片

引用本地圖片的話,就只能是本機訪問該網站才回顯示出來,即使是你區域網訪問你發布的網站,也是不能夠顯示到圖片的。
建議把圖片放到webRoot文件夾下面,建一個images文件夾,images文件夾存放你的圖片,如1.jpg,在引用的時候,只需要 src='/images/1.jpg'即可。 當然了,src的值可以引用網上的圖片路徑,這樣就避免放到項目中的麻煩

閱讀全文

與如何引入圖片到Java相關的資料

熱點內容
葉玉卿好看電影推薦 瀏覽:492
海洋少女圖片動漫 瀏覽:170
纖纖電影電視2020 瀏覽:908
日漫男頭像圖片高冷高清 瀏覽:216
前蘇聯二戰經典電影大全 瀏覽:58
餵奶場面電影 瀏覽:366
兒童衣服好看圖片 瀏覽:102
端風s3圖片價格 瀏覽:243
韓國有個雙胞胎兄弟電影 瀏覽:211
女生短發婚紗造型圖片 瀏覽:783
ume電影院兒童購票標准 瀏覽:476
下愛電影韓國 瀏覽:859
韓國電影車震被女人嘲笑 瀏覽:740
如何辨別圖片上的字體 瀏覽:356
如何更換主屏圖片 瀏覽:235
抗日神劇電影大全集免費觀看 瀏覽:117
血洗紅花亭王祖賢鏡頭 瀏覽:980
可愛小殼殼圖片大全 瀏覽:860
古裝三極古裝劇 瀏覽:878
喪黑白男生圖片 瀏覽:877