getLayeredPane与getContentPane的区别

本文详细介绍了如何在JFrame中通过分层面板与透明内容面板结合,成功地在应用界面中展示背景图片,同时确保其他组件如按钮和标签等正常显示于其上。重点讲解了如何通过代码实现内容面板的透明属性,以及关键代码片段的作用,提供了一种在界面设计中灵活运用图像与UI元素的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JFrame里,由根面板,玻璃面板,分层面板结合而成。我们将图片放在分层面板中,因为内容面板在分层面板的上面,所以必须要将内容面板设置透明。

JPanel p1 = (JPanel)this.getContentPane();

这句语句的作用是,首先初始化一个内容面板——this.getContentPane(),这是使用JFrame添加组件的正确方法,并且将这个初始化好的内容面板的类型转换为JPanel类型的,这样才可以使得这个内容面板设置透明。

p1.setOpaque(false);

  这句话即设置透明。p1好像放在桌子上面的一块玻璃,玻璃下的东西可以透过玻璃让我们看见,只有玻璃上面的东西才会挡住玻璃下面的东西。p1下面正是我们要添加的图片,其余的组件,比如按钮,标签都放在p1上。如果参数是true,则全部覆盖下面的组件。


JPanel p1 = (JPanel)this.getContentPane();

p1.setOpaque(false);

p1.setLayout(null);

this.getLayeredPane().add(background,new Integer(Integer.MIN_VALUE));

this.setBounds(300,100,icon.getIconWidth(),icon.getIconHeight());

以上的代码就是添加背景图片的关键代码,现在可以往p1组件上添加其余的组件了


java图片显示不了package cn.game.ui; import javax.swing.*; import javax.swing.border.BevelBorder; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; import java.util.ArrayList; import java.awt.event.ActionEvent; public class LoginJFrame extends JFrame { static Random rand = new Random(); //准备账号的储存 static ArrayList<database> list = new ArrayList<database>(); static{ list.add(new database("zhangsan","1234","123456789112345678","12345678912")); } //验证码 static String CAPTCHA = getcaptcha(); //获取输入的信息 String USERNAME; String PASSWORD; String IDCARDNUMBER; //游戏登陆界面 public LoginJFrame() { this.setSize(603,630); //设置界面的标题 this.setTitle("登陆"); //设置界面置顶 this.setAlwaysOnTop(true); //设置界面居中 this.setLocationRelativeTo(null); this.setVisible(true); //设置关闭模式,即关闭游戏程序结束 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //取消默认居中放置,取消了才可以按XY轴方式放置组件 this.setLayout(null); //加载基本信息,用户名密码验证码 JLabel username = new JLabel("用户名"); JLabel password = new JLabel("密码"); JLabel captcha= new JLabel("验证码"); username.setBounds(160,250,100,30); username.setFont(new Font("用户名", Font.PLAIN, 24)); // 调整字体大小 this.getContentPane().add(username); password.setBounds(160,300,100,30); password.setFont(new Font("<UNK>", Font.PLAIN, 24)); this.getContentPane().add(password); captcha.setBounds(160,350,100,30); captcha.setFont(new Font("<UNK>", Font.PLAIN, 24)); this.getContentPane().add(captcha); //测试数据 // database data = new database(); // data.setUsername("123456789"); // data.setPassword("12342"); // // JLabel testlabel = new JLabel(CAPTCHA); // testlabel.setBounds(160,200,100,30); // this.getContentPane().add(testlabel); //加载白框为了输入 JTextField usernametext = new JTextField(); usernametext.setBounds(250,252,180,30); this.getContentPane().add(usernametext); JPasswordField passwordtext = new JPasswordField(); passwordtext.setBounds(250,302,180,30); this.getContentPane().add(passwordtext); JTextField captchatext = new JTextField(); captchatext.setBounds(250,352,130,30); this.getContentPane().add(captchatext); //生成验证码 JLabel captchalabel = new JLabel(getcaptcha()); captchalabel.setBounds(390,350,180,30); captchalabel.setFont(new Font(getcaptcha(), Font.PLAIN, 24)); this.getContentPane().add(captchalabel); //添加登陆和注册按钮 // JButton login = new JButton(); // login.setIcon(new ImageIcon("D:\\java\\IDEA\\code\\game\\login\\login.png")); // login.setBorderPainted(false); // login.setContentAreaFilled(false); // login.setBounds(250,400,100,30); // this.getContentPane().add(login); //加载背景图 JLabel label = new JLabel(new ImageIcon("D:\\java\\IDEA\\code\\game\\animal1\\background.png")); label.setBounds(40,10,503,560); this.getContentPane().add(label); //添加登陆和注册按钮 JButton login = new JButton(); login.setIcon(new ImageIcon("D:\\java\\IDEA\\code\\game\\animal1\\login.png")); login.setBorderPainted(false); login.setContentAreaFilled(false); login.setBounds(250,400,130,50); this.getContentPane().add(login); //获取白框的输入值 // String USERNAME = usernametext.getText(); // String PASSWORD = passwordtext.getText(); // String CAPTCHA = captchalabel.getText(); } //生产验证码 public static String getcaptcha() { String captcha = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; String number= "0123456789"; StringBuilder sb = new StringBuilder(5); for(int i = 0;i < 4;i++) { int num = rand.nextInt(captcha.length()); sb.append(captcha.charAt(num)); } int num = rand.nextInt(number.length()); int index = rand.nextInt(4); sb.insert(index,number.charAt(num)); return sb.toString(); } //判断账号是否可用 public static boolean isusername(String username) { if(username.length()<3 || username.length() > 17) { return false; } for(int i = 0;i < username.length();i++) { if(!Character.isDigit(username.charAt(i))) { return false; } } return true; } //判断身份证是否可用 public static boolean isidcardnumber(String idcardnumber) { if(idcardnumber.length()!= 18) { return false; } for(int i = 0;i < idcardnumber.length();i++) { if(!Character.isDigit(idcardnumber.charAt(i))) { return false; } } return Character.isDigit(idcardnumber.charAt(17)) && idcardnumber.charAt(17) !='x' && idcardnumber.charAt(17) != 'X'; } //判断手机号是否可用 public static boolean isphonenumber(String phonenumber) { if(phonenumber.length()!= 11) { return false; } for(int i = 0;i < phonenumber.length();i++) { if(!Character.isDigit(phonenumber.charAt(i))) { return false; } } return true; } //判断账号是否正确 public static boolean istrueusername(String username) { for(int i = 0;i < list.size();i++) { if(username.equals(list.get(i).getUsername())) { return true; } } return false; } //判断密码是否正确 public static boolean istruepassword(String password) { for(int i = 0 ;i < list.size();i++) { if(password.equals(list.get(i).getPassword())) { return true; } } return false; } //判断身份证是否正确 public static boolean istrueidcardnumber(String idcardnumber) { for(int i = 0;i < list.size();i++) { if(idcardnumber.equals(list.get(i).getIdcardnumber())) { return true; } } return false; } //判断验证码是否正确 public static boolean istruecaptcha(String captcha) { if(captcha.equals(CAPTCHA)) { return true; } return false; } //判断手机号是否正确 public static boolean istruephonenum(String phonenumber) { for(int i = 0;i < list.size();i++) { if(phonenumber.equals(list.get(i).getPhonenumber())) { return true; } } return false; } //判断是可以登陆 public static boolean islogin(String username, String password, String captcha) { if(istrueusername(username) && istruepassword(password) && istruecaptcha(captcha)) { return true; } return false; } //展示弹框 public static void showJDialog(String content) { JDialog jdialog = new JDialog(); jdialog.setSize(200,200); jdialog.setAlwaysOnTop(true); jdialog.setLocationRelativeTo(null); jdialog.setModal(true); JLabel label = new JLabel(); label.setBounds(0,0,200,150); jdialog.getContentPane().add(label); jdialog.setVisible(true); } } 为啥我的背景图和按钮不会显示出来
最新发布
06-04
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值