用Swing包下的ImageIcon類就可以實現(xiàn),比如在一個按鈕中添加一張圖片,就可以用以下代碼實現(xiàn):ImageIcon imageicon =new ImageIcon(String s);JButton b=new JButton(imageicon); 其中參數(shù)s是所要添加圖片的路徑(絕對路徑或相對路徑)和名字。如想添加D盤下的圖片1.jpg,就可以將上面改成:ImageIcon imageicon =new ImageIcon("D:\1.jpg");
公司主營業(yè)務(wù):成都做網(wǎng)站、網(wǎng)站設(shè)計、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出子洲免費做網(wǎng)站回饋大家。
圖片名字是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);
}
}
僅僅是給窗口添加背景的話是很簡單的,添加上以下語句(自己去添加變量哈):\x0d\x0a\x0d\x0alabel = new JLabel(background); //background為ImageIcon\x0d\x0a// 把標(biāo)簽的大小位置設(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è)置成透明就可以了
分享標(biāo)題:java中加入圖片的代碼 java中加入圖片的代碼是什么
本文URL:http://chinadenli.net/article18/dodepdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站營銷、網(wǎng)站設(shè)計公司、自適應(yīng)網(wǎng)站、軟件開發(fā)、小程序開發(fā)
聲明:本網(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)