发布网友 发布时间:2022-04-22 05:07
共1个回答
热心网友 时间:2023-06-26 09:22
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"你放到你创建的项目的文件夹里就行 测试成功