1. java中的swing设计界面时怎么加上背景图片。而不覆盖什么label等控件
通过以下方式设置下背景就可以了:
background=newImageIcon("images/backImage.png");
backImage=newJLabel(background);
backImage.setBounds(0,0,background.getIconWidth(),
background.getIconHeight());
backPanel=(JPanel)this.getContentPane();
backPanel.setOpaque(false);
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(backImage,newInteger(Integer.MIN_VALUE));
backPanel.setLayout(newBorderLayout());
这样就可以把图片放在最下面了,效果图:
2. myeclipse 用Swing可视化编程,怎么添加背景图片
importjava.awt.*;
importjavax.swing.*;
{
publicstaticvoidmain(String[]args){
TestBackgroundColortbc=newTestBackgroundColor();
tbc.setVisible(true);
}
privateJPanelimagePanel;
privateImageIconbackground;
publicTestBackgroundColor(){
background=newImageIcon("C:\Users\Administrator\Documents\
MyFTPRushDownloads\项目\Photoes\007.jpg");//背景图片
JLabellabel=newJLabel(background);//把背景图片显示在一个标签里面
//把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel=(JPanel)this.getContentPane();
imagePanel.setOpaque(false);
//内容窗格默认的布局管理器为BorderLayout
imagePanel.setLayout(newFlowLayout());
imagePanel.add(newJButton("测试按钮"));
this.getLayeredPane().setLayout(null);
//把背景图片添加到分层窗格的最底层作为背景
this.getLayeredPane().add(label,newInteger(Integer.MIN_VALUE));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(background.getIconWidth(),background.getIconHeight());
this.setVisible(true);
}
}
实现效果:
3. Java 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来构建图片 */
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);
4. Java Swing 怎么自定义界面背景图片
在java swing中需要为容器添加自定义图片或者背景图片。提供两种简单的解决方案,一种利用JPanel,另一种利用JLabel,代码如下:
1、JPanel(源代码)
packageoo;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.io.File;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassDrawing{
JFramejframe=newJFrame();
publicstaticJPanelGImage=null;
publicDrawing(){
initFrame();
}
//初始化窗口
publicvoidinitFrame(){
//利用JPanel添加背景图片
GImage=newJPanel(){
protectedvoidpaintComponent(Graphicsg){
ImageIconicon=newImageIcon("image\benbenla.jpg");
Imageimg=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);
}
publicstaticvoidmain(String[]args){
newDrawing();
}
}
2、JLabel源代码
packageswing.draw;
importjava.awt.Image;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
/**利用JLabel来构建图片*/
publicclassDrawing2{
JLabeljlpic=newJLabel();
JFramejframe=newJFrame();
publicDrawing2(){
init1Frame();
}
publicvoidinit1Frame(){
ImageIconicon=newImageIcon("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);
}
publicstaticvoidmain(Stringargs[]){
newDrawing2();
}
}
添加控件:
jlpic.setIcon(icon);
Containerc=newContainer();
JLabeluser=newJLabel("用户:");
JLabelpassword=newJLabel("密码:");
JTextFieldtxf_userName=newJTextField();
JPasswordFieldpwd_password=newJPasswordField()
{
publicvoidpaste(){
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);
5. java Swing中怎样设置这样的背景图片
指一下方向 ,用起来,不是很难
空白的 整个的背景,覆盖
public void paint(Graphics g)
方法。
如果列表内容也要修改,要重写ListCellRenderer类。
6. Java Swing中怎样简单方便的设置背景图片
可以新建个面板,在面板里放入带图片的JLabel,填满面板即可。
JPanel jp = new JPanel(); //新建面板
jp.setLayout(new FlowLayout()); //设置面板布局
ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"));
JLabel uppicture=new JLabel(ii); //往面板里加入JLabel
this.setVisible(true);
7. java swing里如何设置背景图片
设置背景图片,可以重写paint()方法。
具体步骤如下:
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 static final int WTH=300;
public static final int HHT=200;
public BjFrame() {
setSize(WTH,HHT);
BjPanel pl=new BjPanel();
Container contentPane=getContentPane();
contentPane.add(pl);
pl. setOpaque(true);
}
}
class BjPanel extends JPanel {
Image im;
public BjPanel()
{
im=Toolkit.getDefaultToolkit().getImage("背景.jpg");//需要注意的是如果用相对路径载入图片,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则必须用绝对路径。
}
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;//计算图片的坐标,使图片显示在窗口正中间
g.drawImage(image,x,y,null);//绘制图片
}
}
8. Java swing 窗体如何设置背景图片
background=newImageIcon("images/backImage.png");
backImage=newJLabel(background);
backImage.setBounds(0,0,background.getIconWidth(),
background.getIconHeight());
backPanel=(JPanel)this.getContentPane();
backPanel.setOpaque(false);
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(backImage,newInteger(Integer.MIN_VALUE));
backPanel.setLayout(newBorderLayout());
这个是我程序里面复制出来的,效果就下面图一样。
9. 如何在swing里面设置背景图片
你的代码是正确的,可能是你图片的位置没有放对。我本地试过了,你可以像下面这样做:
首先修改一行代码:/res改为res
Image image=new ImageIcon("res/bg.jpg").getImage();
图片放到: