Java Swing中怎样简单方便的设置背景

Java Swing中怎样简单方便的设置背景,第1张

在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来构建图片 */

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)

你的代码是正确闷桥盯的,可能是你图片的消启位置没有放对。我本地试过了,你可以像下面这样蚂和做:

首先修改一行代码:/res改为res

Image image=new ImageIcon("res/bg.jpg").getImage() 

图片放到:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11979746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存