//不就是在窗口上設(shè)置一個(gè)背景圖片么?怎么那么多不同的聲音?
10年的資源網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整資源建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“資源網(wǎng)站設(shè)計(jì)”,“資源網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
//代碼如下 運(yùn)行就看到結(jié)果了,你準(zhǔn)備好個(gè)圖片 放到當(dāng)前路徑里就可以了
import java.util.*;
import javax.swing.*;
public class Test extends JFrame {
private JPanel pan;
private JLabel labName;
private JTextField tfName;
private JLabel labPass;
private JPasswordField tfPass;
private JButton butConfirm;
private ImageIcon ii;
private JLabel lab;
Test(){
this.setBounds(200, 200, 300, 200);
pan = new JPanel();
labName = new JLabel("用戶名");
tfName = new JTextField(20);
labPass = new JLabel("密 碼");
tfPass = new JPasswordField(20);
butConfirm = new JButton("SAVE");
pan.add(labName);
pan.add(tfName);
pan.add(labPass);
pan.add(tfPass);
pan.add(butConfirm);
ii = new ImageIcon("1.jpg");//你只需要把這里的圖片1.jpg給改了就可以了
lab = new JLabel(ii);
lab.setBounds(0, 0,ii.getIconWidth(), ii.getIconHeight());
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(lab, new Integer(Integer.MIN_VALUE));
this.setContentPane(pan);
pan.setOpaque(false);
this.setVisible(true);
}
public static void main(String args[]){
new Test();
}
}
這是我以前的一個(gè)小代碼你可以看看
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里新建個(gè)文件夾(Folder),然后把你要插入的圖片復(fù)制黏貼到這個(gè)文件夾里面。
例如我那個(gè)引入的圖片代碼:jlabel = new JLabel(new ImageIcon("Image//真三.gif"));
我new一個(gè)folder叫Image,圖片名稱叫"真三.gif"
1 利用標(biāo)簽組件來設(shè)置
標(biāo)簽本來是一種最簡單的組件,為什么可以將它設(shè)置成為一個(gè)窗體的背景呢?首先還是要了解框架JFrame中的層次結(jié)構(gòu)。JFrame中的層次分布及相對(duì)關(guān)系是:最底層是JRootPane,第二層是JlayerPane,最上層就是ContentPane,也正是我們常說的內(nèi)容面板。所以一般我們拖放的控件就是在ContentPane層上。也就是說我們只需將背景圖片放在JFrame的第二層是JlayerPane上,再把內(nèi)容面板ContentPane設(shè)置為透明,則第二層JlayerPane上放置的圖片即成為內(nèi)容面板的背景了。具體代碼如下:
JPanelpnlMain=new JPanel(); //創(chuàng)建面板pnlMain。
getContentPane().add(pnlMain); //將pnlMain設(shè)置為主面板。
Iconi=new ImageIcon("背景.jpg"); /*用源圖片“背景.jpg”構(gòu)造一個(gè)ImageIcon對(duì)象i,需要注意如果圖片的路徑使用的是相對(duì)路徑,則圖片文件必須放在類文件所在文件夾或項(xiàng)目的根文件夾中,否則圖片的路徑必須用絕對(duì)路徑。*/
JLabellblLogo = new JLabel(i); //用指定的圖片構(gòu)造標(biāo)簽對(duì)象lb
this.getLayeredPane().add(lb, new Integer(Integer.MIN_VALUE));
//把標(biāo)簽放在第二層JlayerPane上。
lb.setBounds(0, 0,ii.getIconWidth(),i.getIconHeight());
//設(shè)置標(biāo)簽的尺寸,即背景圖象的大小。
getConentPane().setOpaque(false); /*把內(nèi)容面板設(shè)置為透明,這樣整個(gè)框架的背景就不再是內(nèi)容面板的背景色,而是第二層中標(biāo)簽的圖像。*/
pnlMain.add(lb); //將標(biāo)簽添加到主面板pnlMain中。
用標(biāo)簽組件JLabel來設(shè)置窗體背景,其擴(kuò)展性上比較差,且在標(biāo)簽上不能放置其他組件,比如:在放置一個(gè)JButton,整個(gè)布局背景圖就錯(cuò)亂。導(dǎo)致這種現(xiàn)象是因?yàn)镴ava加載組件是有順序的,作為背景的JLabel的代碼一定要放在全部組件代碼的最后,這樣JLabel的背景圖片才不會(huì)被其他組件遮住,從而使整個(gè)布局背景錯(cuò)亂。所以采用這種方式來設(shè)置窗體背景有很大的局限性。因此最好是采用以下方式來設(shè)置窗體背景。
2 通過JPanel面板來設(shè)置窗體背景
JPanel面板是Java中的容器之一。而Java中的容器是沒有背景圖片的屬性的,它們只有背景顏色,如果需要在JPanel面板上設(shè)置窗體背景,就需要重寫paintComponent(Graphics g)方法,即把所要設(shè)置的背景圖片畫上作為JPanel面板的背景。具體實(shí)現(xiàn)如下:
首先定義一個(gè)JPanel的子類BjPanel,由于JPanel的構(gòu)造方法不能添加圖像,因此在創(chuàng)建此子類的時(shí)候先用getImage載入一幅背景圖片,在重寫paintComponent(Graphics g)方法時(shí),利用drawImage方法將其逐漸繪制到屏幕上,并將該面板添加到框架中,最后將該面板設(shè)置為透明。其代碼如下:
import java.awt.*;
import javax.swing.*;
public class bkground
{
public static void main(String args[])
{
BjFramef=newBjFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class BjFrame extends JFrame
{
public BjFrame()
{
setSize(WTH,HHT);
BjPanel pl=new BjPanel();
Container contentPane=getContentPane();
contentPane.add(pl);
pl. setOpaque(true);
}
public static final int WTH=300;
public static final int HHT=200;
}
class BjPanel extends JPanel
{
Image im;
public BjPanel()
{
im=Toolkit.getDefaultToolkit().getImage("背景.jpg");//需要注意的是如果用相對(duì)路徑載入圖片,則圖片文件必須放在類文件所在文件夾或項(xiàng)目的根文件夾中,否則必須用絕對(duì)路徑。
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int imWidth=image.getWidth(this);
Int imHeight=image.getHeight(this); //定義圖片的寬度、高度
int FWidth=getWidth();
int FHeight=getHeight();//定義窗口的寬度、高度
int x=(FWidth-imWidth)/2;
int y=(FHeight-imHeight)/2;//計(jì)算圖片的坐標(biāo),使圖片顯示在窗口正中間
g.drawImage(image,x,y,null);//繪制圖片
}
}
額,建議直接用Visual Editor來設(shè)計(jì)窗口程序。這樣比你自己寫效率高,還直觀。
當(dāng)然要是想詳細(xì)研究Swing,那就得找書看了
在java swing中需要為容器添加自定義圖片或者背景圖片。提供兩種簡單的解決方案,一種利用JPanel,另一種利用JLabel,代碼如下:
1、JPanel(源代碼)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package oo;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Drawing {
JFrame jframe = new JFrame();
public static JPanel GImage = null;
public Drawing() {
initFrame();
}
// 初始化窗口
public void initFrame() {
// 利用JPanel添加背景圖片
GImage = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jframe.setSize(icon.getIconWidth(), icon.getIconHeight());
}
};
jframe.setTitle("測試背景圖片");
jframe.add(GImage);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Drawing();
}
}
2、JLabel源代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package swing.draw;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/** 利用JLabel來構(gòu)建圖片 */
public class Drawing2 {
JLabel jlpic = new JLabel();
JFrame jframe = new JFrame();
public Drawing2() {
init1Frame();
}
public void init1Frame() {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(), Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setBounds(0, 0, 1366, 768);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jframe.setSize(1366, 768);
jframe.add(jlpic);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
public static void main(String args[]) {
new Drawing2();
}
}
添加控件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
jlpic.setIcon(icon);
Container c = new Container();
JLabel user = new JLabel("用戶:");
JLabel password = new JLabel("密碼:");
JTextField txf_userName = new JTextField();
JPasswordField pwd_password = new JPasswordField()
{
public void paste(){
UIManager.getLookAndFeel().provideErrorFeedback(this);
}
};
user.setBounds(200,25,50,25);
password.setBounds(200,52,50,25);
txf_userName.setBounds(300,25,150,25);
pwd_password.setBounds(300,52,150,25);
c.add(user);
c.add(txf_userName);
c.add(password);
c.add(pwd_password);
c.add(jlpic);
jframe.setSize(1366, 768);
// jframe.add();
jframe.add(c);
新聞標(biāo)題:vb.net怎么用ado,VBNET有什么用
網(wǎng)頁URL:http://vcdvsql.cn/article34/hegpse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、微信公眾號(hào)、軟件開發(fā)、標(biāo)簽優(yōu)化、外貿(mào)建站、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)