java怎么導(dǎo)入圖片代碼 javaee怎么導(dǎo)入圖片

怎么為Java程序添加背景圖片代碼?

僅僅是給窗口添加背景的話是很簡單的,添加上以下語句(自己去添加變量哈):\x0d\x0a\x0d\x0alabel = new JLabel(background); //background為ImageIcon\x0d\x0a// 把標簽的大小位置設(shè)置為圖片剛好填充整個面板 \x0d\x0alabel.setBounds(0, 0, this.getWidth(), this.getHeight());\x0d\x0a//添加圖片到frame的第二層(把背景圖片添加到分層窗格的最底層作為背景)\x0d\x0athis.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));\x0d\x0a//把內(nèi)容窗格轉(zhuǎn)化為JPanel,否則不能用方法setOpaque()來使內(nèi)容窗格透明\x0d\x0ajPanel=(JPanel)this.getContentPane();\x0d\x0a//設(shè)置透明\x0d\x0ajPanel.setOpaque(false);\x0d\x0a\x0d\x0a然后你上面那個JPanel p也設(shè)置成透明就可以了

創(chuàng)新互聯(lián)公司服務(wù)項目包括秭歸網(wǎng)站建設(shè)、秭歸網(wǎng)站制作、秭歸網(wǎng)頁制作以及秭歸網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,秭歸網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到秭歸省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

怎樣在java里用URL引入圖片?

讀取圖片可以有以下兩種方法:\x0d\x0a①:ImageIO.read(new File("這里可以寫目錄,比如您提到的src/images/某張圖片名"));\x0d\x0a②:new ImageIcon("目錄").getImage();\x0d\x0a這兩個方法都返回一個圖片對象??梢杂靡粋€Image對象接收一下。\x0d\x0a\x0d\x0a相對路徑是指您所運行的程序的包 所在的文件夾開始的路徑。\x0d\x0a一般來說,上面兩種讀取方法讀取時,是從項目的目錄下開始找文件的。\x0d\x0a所以,您把圖片放在src下的images包中,正確的讀取方法應(yīng)該是:\x0d\x0aImage img=ImageIO.read(new File("src/images/圖片名"));或者\x0d\x0aImage img=new ImageIcon("src/images/圖片名").getImage();\x0d\x0a得到這樣一個Image對象后,就可以使用了。

如何在java中插入圖片

public Qua_Main_JFrame() {

JPanel jpanel = new JPanel();

this.setContentPane(jpanel);

//

添加標簽組件

GridLayout gird = new GridLayout(3,0);

jpanel.setLayout(gird);

ImageIcon img = new ImageIcon("src/JMXY.JPG");

JLabel imgLabel = new JLabel(img);//

將背景圖放在標簽里。

this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));

imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());

this.getLayeredPane().setLayout(null);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

jpanel.setOpaque(false);

initComponents();

}

其中

Qua_Main_JFrame

為創(chuàng)建的

java

窗體項目名,圖片按路徑存放,注意一點,所有

的代碼都應(yīng)該放在

initComponents();

方法之上,這樣你添加進窗體中的空間才會顯示在

圖片之上,否則看不見控件。

Java程序怎樣導(dǎo)入圖片

圖片名字是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;itoolItem.length;i++){

if(toolItem[i].isSelected()){

str+=toolItem[i].getText()+"\n";

}

}

if(str.length()==0){

textArea.setText("沒有復(fù)選");

}else{

textArea.setText(str);

}

}

};

for(int i=0;ifileItem.length;i++){

fileItem[i].addActionListener(actListener);

fileMenu.add(fileItem[i]);

if(i==2){

fileMenu.addSeparator();

}

}

for(int i=0;iviewItem.length;i++){

viewItem[i].addActionListener(actListener);

viewMenu.add(viewItem[i]);

}

viewMenu.addSeparator();

viewMenu.add(toolMenu);

for(int i=0;itoolItem.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怎么導(dǎo)入圖片代碼 javaee怎么導(dǎo)入圖片
當前網(wǎng)址:http://muchs.cn/article42/doedsec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、定制網(wǎng)站、Google、虛擬主機、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)