java程序设计出来的图形用户界面上标题栏上怎样添加图片呢?

java程序设计出来的图形用户界面上标题栏上怎样添加图片呢?,第1张

setIcon

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()  } }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11673063.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存