鴻蒙java代碼設(shè)置圖片 鴻蒙java代碼設(shè)置圖片在哪
JAVA 按鈕上設(shè)置圖片的問題
按鈕只能是個矩形,按你是意思,應(yīng)該是不顯示按鈕的邊框,只顯示圖標是吧。
你可以設(shè)置按鈕背景為透明,設(shè)置邊框為null
but.setBackground(new Color(255,255,255)); //but是按鈕名稱
but.setBorder(null); //but是按鈕名稱
我剛寫的一個點擊按鈕交替變換圖標的程序,代碼如下:
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
//變換按鈕圖標
public class Button_Icon extends JFrame implements ActionListener{
private Container con;
private JButton but;
private Icon ic;
public Button_Icon() {
this.setTitle("歡迎");
this.setBounds(200, 200, 200, 234); //滑漏洞標搜歷題欄高34
con=this.getContentPane();
con.setLayout(null);
Cursor cs=new Cursor(Cursor.HAND_CURSOR);
ic=new ImageIcon("j:\\Screenshot.png");
but=new JButton(ic);
but.setBounds(60, 70, 80, 60);
but.addActionListener(this);
but.setCursor(cs);
but.setBackground(new Color(255,255,255));
but.setBorder(null);
con.add(but);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Icon ic2=but.getIcon();
if(ic2==null){but.setIcon(ic);}
else {but.setIcon(null);}
}
public static void main(String[] args) {
new Button_Icon();
}
}
你可信枯以看下效果,看是不是你想要的。
JAVA界面加了背景圖片但是顯示不出來,代碼在下面
你的背景圖片的Java程序,我?guī)湍愀耐炅?你看看吧(只是改了JM();構(gòu)造函數(shù)中的內(nèi)容,去掉了setOpaque函數(shù),其它的地方?jīng)]動)
import?javax.swing.*;
public?class?JM?extends?JFrame?{
?JLabel?yh?=?new?JLabel("用戶名");
?JLabel?mm?=?new?JLabel("密碼");
?JTextField?yh1?=?new?JTextField(15);
?JPasswordField?mm1?=new?JPasswordField(15);
?嘩散寬JRadioButton?jmm?=?new?JRadioButton("記住密碼");
?JButton?dl?=new?JButton("登陸");
?JButton?qx?=?new?JButton("取消");
?JButton?zc?=?掘慧new?JButton("注冊");
?public?JM?(){
??this.setTitle("登陸界面");
??this.setSize(300,?270);
??JPanel?c?=?(JPanel)this.getContentPane();
??c.setLayout(null);
??//Container?c?=?this.getContentPane();
??yh.setBounds(20,?0,?100,?100);
??yh1.setBounds(70,37,?200,?27);
??mm.setBounds(20,?30,?100,?100);
??mm1.setBounds(70,?67,?200,27);
??jmm.setBounds(70,?95,?80,?30);
??dl.setBounds(70,?135,?60,?35);
??qx.setBounds(135,?135,?60,?35);
??zc.setBounds(200,?135,?60,?35);
??c.add(yh);
??c.add(yh1);
??c.add(mm);
??c.add(mm1);
??亂亮c.add(jmm);
??c.add(dl);
??c.add(qx);
??c.add(zc);
??JLabel?bg?=?new?JLabel(new?ImageIcon("006Cx3u5ly1fbz7xwdx1sj30i40dydg4.jpg"));
??bg.setBounds(0,?0,?300,?270);
??c.add(bg);
??c.setOpaque(false);
??this.setDefaultCloseOperation(EXIT_ON_CLOSE);
??this.setVisible(true);
?}
?//private?void?setOpaque(boolean?b)?{
?//?TODO?Auto-generated?method?stub
?//}
?public?static?void?main(String[]?args)?{
??//?TODO?Auto-generated?method?stub
??new?JM();
?}
}
運行結(jié)果
Java怎么添加背景圖片?
這是我以前的一個小代碼你可以看看
public class demo_9 extends JFrame {
JSplitPane jsp = null;
JList jlist;
JLabel jlabel;
public static void main(String[] args) {
demo_9 a = new demo_9();
}
public demo_9(){
String []words ={"boy","girl"};
JList jlist = new JList(words);
jlabel = new JLabel(new ImageIcon("Image//真頌吵三.gif")); //這里就是引入圖片了
//拆分窗格
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jlist,jlabel);
jsp.setDividerLocation(70);
//設(shè)置可以伸縮
jsp.setOneTouchExpandable(true);
this.add(jsp);
this.setTitle("test");
this.setSize(400,300);
this.setLocation(400,200);
this.setVisible(true);
}
}
步驟:首先先在project里新建個文件夾(Folder),然謹臘后把你要插入的圖片復(fù)制黏貼到這個文件祥櫻滑夾里面。
例如我那個引入的圖片代碼:jlabel = new JLabel(new ImageIcon("Image//真三.gif"));
我new一個folder叫Image,圖片名稱叫"真三.gif"
如何用java代碼實現(xiàn)手動點擊圖片更換想要換的圖片替換
一個圖片時你知道,現(xiàn)在只是切悔御換的問碧配巖題?
給按鈕添加一個 ActionListener,在它賣攜的 actionPerformed 方法中做你的事情,把 jpanel.setIcon(..) 換張圖片。
欄 目:Java編程
下一篇:沒有了
本文標題:鴻蒙java代碼設(shè)置圖片 鴻蒙java代碼設(shè)置圖片在哪
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/17413.html
您可能感興趣的文章
- 04-13關(guān)于java視頻通話代碼的信息
- 04-13未讀消息顯示java代碼 未讀消息顯示java代碼什么意思
- 04-13java字符串轉(zhuǎn)代碼6的簡單介紹
- 04-13Java結(jié)束進程樹代碼 java怎么結(jié)束運行
- 04-13java重置按鈕的代碼 java重置按鈕代碼jsp
- 04-13java圖片上傳方法代碼 java上傳圖片到tomcat
- 04-13點擊按鈕的java代碼 點擊按鈕的java代碼是什么
- 04-12包含局域網(wǎng)聊天java代碼6的詞條
- 04-12java代碼塊嵌套幾層 java代碼塊嵌套不超過幾層
- 04-12java程序代碼成績表 用java編寫成績


閱讀排行
本欄相關(guān)
隨機閱讀
- 01-10C#后臺調(diào)用前臺JS函數(shù)方法
- 08-05織夢安裝后打開首頁亂的或者不是截
- 08-05dedecms修改附件按月保存避免文件夾越
- 08-05dedecms織夢如何調(diào)用指定欄目名稱和鏈
- 08-05織夢CMS管理員帳號密碼忘記了怎么辦
- 01-11如何使用wordpress鉤子函數(shù)在發(fā)表文章
- 01-10C++實現(xiàn)類似延時停頓的打字效果
- 01-10C++使struct對象擁有可變大小的數(shù)組(詳
- 01-10C語言實現(xiàn)三子棋小游戲
- 01-10C#基礎(chǔ)知識之new關(guān)鍵字介紹