歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java JLayeredPane來實現窗口背景圖片

用JLayeredPane實現在不影響窗口組件布局的情況下,添加一個背景圖片:

調整窗口大小後:

  1. package com.han;
  2. import java.awt.BorderLayout;
  3. import java.awt.Container;
  4. import java.awt.Dimension;
  5. import java.awt.Image;
  6. import java.awt.Point;
  7. import java.awt.Rectangle;
  8. import java.awt.Toolkit;
  9. import java.awt.event.ComponentAdapter;
  10. import java.awt.event.ComponentEvent;
  11. import javax.swing.BorderFactory;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JLayeredPane;
  17. import javax.swing.JPanel;
  18. /**
  19. * The JLayeredPane has no layout manager by default, so you have to use
  20. * setBounds() for each component in it. However, you can still assign a layout
  21. * manager to the layered pane.
  22. * <p>
  23. * Many programs use intermediate containers (such as panels) and their layout
  24. * managers to lay out components on the same layer, but use absolute
  25. * positioning to lay out components on different layers.
  26. * <p>
  27. * We can use frame.pack() or frame.setSize() whatever its layout method is
  28. * (use a layout manager or not).
  29. * <p>
  30. * In fact, if we hesitate to use component.setPreferredSize() or
  31. * component.setSize(), we first see the layout method employed to layout this
  32. * component. If it has a layout manager, we use component.setPreferredSize();
  33. * else (no layout manager), we use component.setBounds().
  34. * <p>
  35. * Concerning the JLayeredPane, the <i>depth</i> is higher, the component is on
  36. * the top (of course, there are some depth conventions, like DEFAULT_LAYER,
  37. * PALETTE_LAYER, ...). Within its depth, the position number is 0, top-most;
  38. * -1, bottom-most; 0, 1, 2, 3, ... from top to bottom.
  39. *
  40. * @author HAN
  41. *
  42. */
  43. @SuppressWarnings("serial")
  44. publicclass JLayeredPane_1 extends JFrame {
  45. public JLayeredPane_1() {
  46. // TODO Auto-generated constructor stub
  47. Container container = getContentPane();
  48. final JLayeredPane layeredPane = new JLayeredPane();
  49. // layeredPane.setBorder(BorderFactory.createTitledBorder(null,
  50. // "layeredPane", TitledBorder.LEADING, TitledBorder.TOP,
  51. // new Font(null, Font.BOLD + Font.ITALIC, 14), Color.RED));
  52. layeredPane.setBorder(BorderFactory.createTitledBorder("<html><b><i>" +
  53. "<u><font face='SansSerif' size='5' color='red'>layeredPane" +
  54. "</font></u></i></b></html>"));
  55. Dimension dimension = new Dimension(300, 310);
  56. layeredPane.setPreferredSize(dimension);
  57. final JPanel panelBg = new JPanel();
  58. final Image imageBg = Toolkit.getDefaultToolkit()
  59. .getImage(this.getClass().getResource("/images/Lighthouse.jpg"));
  60. ImageIcon imageIcon = new ImageIcon(imageBg.getScaledInstance
  61. (dimension.width, dimension.height, Image.SCALE_FAST));
  62. final JLabel bg = new JLabel(imageIcon);
  63. final Point origin = new Point(10, 30);
  64. final Rectangle rectangle = new Rectangle(origin, dimension);
  65. panelBg.setBounds(rectangle);
  66. panelBg.add(bg);
  67. final JPanel panelContent = new JPanel();
  68. JButton button = new JButton("button 1");
  69. JButton button2 = new JButton("button 2");
  70. JButton button3 = new JButton("button 3");
  71. panelContent.setBounds(rectangle);
  72. panelContent.setOpaque(false); // 設置為透明
  73. panelContent.add(button);
  74. panelContent.add(button2);
  75. panelContent.add(button3);
  76. layeredPane.add(panelBg, new Integer(0)); // the same to layeredPane.add(panelBg);
  77. layeredPane.add(panelContent, new Integer(1));
  78. container.add(layeredPane, BorderLayout.CENTER);
  79. addComponentListener(new ComponentAdapter() {
  80. publicvoid componentResized(ComponentEvent e) {
  81. // System.out.println(layeredPane.getSize()); //此處必須用getSize()而不是用getPreferredSize()
  82. // System.out.println(layeredPane.getPreferredSize());
  83. Dimension dimensionNew = layeredPane.getSize();
  84. Rectangle rectangleNew = new Rectangle(origin, dimensionNew);
  85. panelBg.setBounds(rectangleNew);
  86. panelContent.setBounds(rectangleNew);
  87. bg.setIcon(new ImageIcon(imageBg.getScaledInstance
  88. (dimensionNew.width, dimensionNew.height, Image.SCALE_FAST)));
  89. }
  90. });
  91. }
  92. /**
  93. * @param args
  94. * @throws Exception
  95. */
  96. publicstaticvoid main(String[] args) {
  97. // TODO Auto-generated method stub
  98. JLayeredPane_1 frame = new JLayeredPane_1();
  99. frame.pack();
  100. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  101. frame.setVisible(true);
  102. }
  103. }
Copyright © Linux教程網 All Rights Reserved