public void setIcon(Icon icon)
定义此组件将要显示的图标。如果 icon 值为 null,则什么也不显示。
此属性的默认值为 null。
这是一个 JavaBeans 绑定属性。
另请参见:
setVerticalTextPosition(int), setHorizontalTextPosition(int), getIcon()
直接上代码了,这是我以前写的package am_2 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() } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)