Swing_如何在图片上添加组件而不被覆盖
可能有些同学在做java的图形界面时,总是觉得界面不太好看。
就像下面这样。但是又无奈不知道怎么在图片上添加组件
当我们稍作修改后,摇身一变
核心代码
要在JFrame上添加背景图片,常见做法是加在layeredPane上面,并将contentPane设置成透明的即可。
panel = (JPanel) getContentPane();//将内容面板设置为JPanel面板
panel.setOpaque(false);//将面板设置为透明
label = new JLabel(icon);//将图片封装到JLabel中
getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));//再将JLabel设置为最底层,然后再在JLabel上添加组件
label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());//将label的大小设置为图片的大小
panel.setLayout(null);//使用绝对布局
JLayeredPane面板主要是为Swing容器添加深度,它允许组件在必要的时候相互重叠。就是将组件放入不同的层内,这样可用保证组件能够正确的重叠
这里因为把面板设置为了透明,但组件并没有设置为透明,所以从正面看就像是将组件镶嵌在图片上。
全部代码
package ki;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
public class RegiestFrame extends JFrame {
private JPanel panel;
private JLabel label, username, sex, pwd, repwd, prompt;
private JTextField usernameFid;
private JPasswordField pwdFid, repwdFid;
private JRadioButton mRbtn, wRbtn;
private ButtonGroup buttonGroup;
private ImageIcon icon = new ImageIcon("img/司马懿.png");
private JButton back, confirm;
private Font font = new Font("宋体", Font.BOLD, 13);
private boolean flag;
public RegiestFrame() throws HeadlessException {
setTitle("注册账号");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(690, 439);
setLocationRelativeTo(null);
setResizable(false);
panel = (JPanel) getContentPane();
panel.setOpaque(false);//将面板设置为透明
label = new JLabel(icon);
getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));
label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
panel.setLayout(null);
//用户名
username = new JLabel("用户名:");
username.setBounds(120, 100, 50, 28);
username.setFont(font);
username.setForeground(new Color(196, 255, 14));
panel.add(username);
usernameFid = new JTextField();
usernameFid.setBounds(172, 102, 150, 23);
usernameFid.setHorizontalAlignment(SwingConstants.CENTER);
usernameFid.setFont(new Font("楷体", Font.BOLD, 13));
panel.add(usernameFid);
//性别单选按钮
sex = new JLabel("性 别:");
sex.setBounds(120, 140, 50, 28);
sex.setForeground(new Color(196, 255, 14));
sex.setFont(font);
panel.add(sex);
mRbtn = new JRadioButton("男", true);
mRbtn.setBounds(190, 147, 15, 15);
mRbtn.setBackground(Color.black);
JLabel man = new JLabel("男");
man.setForeground(Color.red);
man.setBounds(205, 147, 15, 15);
panel.add(man);
wRbtn = new JRadioButton("女");
wRbtn.setBounds(258, 147, 15, 15);
wRbtn.setBackground(Color.BLACK);
JLabel woman = new JLabel("女");
woman.setBounds(273, 147, 15, 15);
woman.setForeground(Color.red);
panel.add(woman);
buttonGroup = new ButtonGroup();
buttonGroup.add(mRbtn);
buttonGroup.add(wRbtn);
panel.add(mRbtn);
panel.add(wRbtn);
//密码
pwd = new JLabel("密 码:");
pwd.setBounds(120, 180, 50, 28);
pwd.setForeground(new Color(196, 255, 14));
pwd.setFont(font);
pwdFid = new JPasswordField();
pwdFid.setBounds(172, 182, 150, 23);
panel.add(pwd);
panel.add(pwdFid);
//确认密码
repwd = new JLabel("确认密码:");
repwd.setBounds(116, 228, 60, 28);
repwd.setForeground(new Color(196, 255, 14));
repwd.setFont(font);
repwdFid = new JPasswordField();
repwdFid.setBounds(172, 230, 150, 23);
panel.add(repwd);
panel.add(repwdFid);
//按钮
confirm = new JButton("提交");
confirm.setBackground(Color.BLUE);
confirm.setForeground(Color.YELLOW);
confirm.setFocusPainted(false);
confirm.setFont(font);
confirm.setBounds(153, 278, 75, 32);
panel.add(confirm);
confirm.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
setCursor(new Cursor(12));
}
@Override
public void mouseExited(MouseEvent e) {
setCursor(new Cursor(0));
}
});
back = new JButton("返回");
back.setBackground(Color.BLUE);
back.setFocusPainted(false);
back.setForeground(Color.YELLOW);
back.setFont(font);
back.setBounds(255, 278, 75, 32);
panel.add(back);
back.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
setCursor(new Cursor(12));
}
@Override
public void mouseExited(MouseEvent e) {
setCursor(new Cursor(0));
}
});
setVisible(true);
}
public static void main(String[] args) {
new RegiestFrame();
}
}
提供的案例图片: