发布网友 发布时间:2022-04-29 01:35
共1个回答
热心网友 时间:2022-06-27 17:57
直接上代码了,这是我以前写的import java.awt.*;
import javax.swing.*;
public class JLayeredPane_1 extends JFrame {
public JLayeredPane_1() {
this.setSize(300, 400);
JLayeredPane layeredPane = this.getLayeredPane();
layeredPane.add(new BackgroundPanel(), new Integer(0)); // the same to
// layeredPane.add(panelBg);
layeredPane.add(new PanelContent(), new Integer(1));
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class BackgroundPanel extends JPanel {
public BackgroundPanel() {
this.add(new JLabel(getIcon()));
this.setBounds(0, 0, 300, 400);
}
public ImageIcon getIcon() {
final Image imageBg = Toolkit.getDefaultToolkit().getImage(
this.getClass().getResource("/img/0.jpg"));
ImageIcon imageIcon = new ImageIcon(imageBg);
return imageIcon;
}
@Override
public void paint(Graphics g) {
Image imageBg = Toolkit.getDefaultToolkit().getImage(
this.getClass().getResource("/img/0.jpg"));
g.drawImage(imageBg, 0, 0, 300, 400, null);
}
}
public class PanelContent extends JPanel {
public PanelContent() {
JButton button = new JButton("测试按钮 1");
JButton button2 = new JButton("测试按钮 2");
JButton button3 = new JButton("测试按钮 3");
this.setBounds(100, 100, 100, 100);
this.setOpaque(false); // 设置为透明
this.add(button);
this.add(button2);
this.add(button3);
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JLayeredPane_1 frame = new JLayeredPane_1();
}
}