java 窗体中添加背景图片的方法
importjava.awt.*;importjava.net.URL;importjavax.swing.*;importjava.awt.Dimension;impo...
import java.awt.*;
import java.net.URL;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import java.awt.event.*;
public class Example1 extends JFrame{
public static void main(String[] args){
JFrame jf=new JFrame("系统登录");
JPanel jpanel = new JPanel();
jf.setContentPane(jpanel);
//设置窗口在屏幕中居中显示
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int x=(int)(width-300)/2;
int y=(int)(height-180)/2;
jf.setBounds(x,y,300,180);
//添加标签组件
GridLayout gird = new GridLayout(3,0);
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JPanel jpanel3=new JPanel();
jpanel.setLayout(gird);
jpanel.add(jpanel1);
jpanel.add(jpanel2);
jpanel.add(jpanel3);
JLabel name=new JLabel("姓 名:");
JTextField nametext=new JTextField(10);
JLabel pwd=new JLabel("密 码:");
JPasswordField pwd1=new JPasswordField(10);
JButton login=new JButton("登录");
JButton exit=new JButton("退出");
jpanel1.add(name);
jpanel1.add(nametext);
jpanel2.add(pwd);
jpanel2.add(pwd1);
jpanel3.add(login);
jpanel3.add(exit);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
不胜感激!!!
jpanel.setOpaque(false);
jpanel1.setOpaque(false);
jpanel2.setOpaque(false);
jpanel3.setOpaque(false);
测试通过! 展开
import java.net.URL;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import java.awt.event.*;
public class Example1 extends JFrame{
public static void main(String[] args){
JFrame jf=new JFrame("系统登录");
JPanel jpanel = new JPanel();
jf.setContentPane(jpanel);
//设置窗口在屏幕中居中显示
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int x=(int)(width-300)/2;
int y=(int)(height-180)/2;
jf.setBounds(x,y,300,180);
//添加标签组件
GridLayout gird = new GridLayout(3,0);
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JPanel jpanel3=new JPanel();
jpanel.setLayout(gird);
jpanel.add(jpanel1);
jpanel.add(jpanel2);
jpanel.add(jpanel3);
JLabel name=new JLabel("姓 名:");
JTextField nametext=new JTextField(10);
JLabel pwd=new JLabel("密 码:");
JPasswordField pwd1=new JPasswordField(10);
JButton login=new JButton("登录");
JButton exit=new JButton("退出");
jpanel1.add(name);
jpanel1.add(nametext);
jpanel2.add(pwd);
jpanel2.add(pwd1);
jpanel3.add(login);
jpanel3.add(exit);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
不胜感激!!!
jpanel.setOpaque(false);
jpanel1.setOpaque(false);
jpanel2.setOpaque(false);
jpanel3.setOpaque(false);
测试通过! 展开
展开全部
import java.awt.*;
import java.net.URL;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import java.awt.event.*;
public class Example1 extends JFrame{
public static void main(String[] args){
JFrame jf=new JFrame("系统登录");
JPanel jpanel = new JPanel();
jf.setContentPane(jpanel);
//设置窗口在屏幕中居中显示
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int x=(int)(width-300)/2;
int y=(int)(height-180)/2;
jf.setBounds(x,y,300,180);
//添加标签组件
GridLayout gird = new GridLayout(3,0);
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JPanel jpanel3=new JPanel();
jpanel.setLayout(gird);
jpanel.add(jpanel1);
jpanel.add(jpanel2);
jpanel.add(jpanel3);
ImageIcon img = new ImageIcon("111.png");
JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
jf.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());
JLabel name=new JLabel("姓 名:");
JTextField nametext=new JTextField(10);
JLabel pwd=new JLabel("密 码:");
JPasswordField pwd1=new JPasswordField(10);
JButton login=new JButton("登录");
JButton exit=new JButton("退出");
jpanel1.add(name);
jpanel1.add(nametext);
jpanel2.add(pwd);
jpanel2.add(pwd1);
jpanel3.add(login);
jpanel3.add(exit);
jf.getLayeredPane().setLayout(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jpanel.setOpaque(false);
jpanel1.setOpaque(false);
jpanel2.setOpaque(false);
jpanel3.setOpaque(false);
jf.setVisible(true);
}
}
那个图片 "111.png"你放到你创建的项目的文件夹里就行 测试成功
import java.net.URL;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import java.awt.event.*;
public class Example1 extends JFrame{
public static void main(String[] args){
JFrame jf=new JFrame("系统登录");
JPanel jpanel = new JPanel();
jf.setContentPane(jpanel);
//设置窗口在屏幕中居中显示
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int x=(int)(width-300)/2;
int y=(int)(height-180)/2;
jf.setBounds(x,y,300,180);
//添加标签组件
GridLayout gird = new GridLayout(3,0);
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JPanel jpanel3=new JPanel();
jpanel.setLayout(gird);
jpanel.add(jpanel1);
jpanel.add(jpanel2);
jpanel.add(jpanel3);
ImageIcon img = new ImageIcon("111.png");
JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
jf.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());
JLabel name=new JLabel("姓 名:");
JTextField nametext=new JTextField(10);
JLabel pwd=new JLabel("密 码:");
JPasswordField pwd1=new JPasswordField(10);
JButton login=new JButton("登录");
JButton exit=new JButton("退出");
jpanel1.add(name);
jpanel1.add(nametext);
jpanel2.add(pwd);
jpanel2.add(pwd1);
jpanel3.add(login);
jpanel3.add(exit);
jf.getLayeredPane().setLayout(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jpanel.setOpaque(false);
jpanel1.setOpaque(false);
jpanel2.setOpaque(false);
jpanel3.setOpaque(false);
jf.setVisible(true);
}
}
那个图片 "111.png"你放到你创建的项目的文件夹里就行 测试成功
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以新建个面板,在面板里放入带图片的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);
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);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询