zoukankan      html  css  js  c++  java
  • 给面板添加背景图片

    给JPanel添加背景图片:

    代码入下:

     1 package test;
     2 
     3 import java.awt.Graphics;
     4 import java.awt.Image;
     5 
     6 import javax.swing.ImageIcon;
     7 import javax.swing.JPanel;
     8 
     9 public class BackgroundPanel extends JPanel {
    10     private int width;
    11     private int height;
    12     private Image img;
    13     
    14     public BackgroundPanel(String imgPath, int width, int height) {
    15         this(new ImageIcon(imgPath).getImage(), width, height);
    16     }
    17     
    18     public BackgroundPanel(Image img, int width, int height) {
    19         this.width = width;
    20         this.height = height;
    21         this.img = img;
    22     }
    23     
    24     public void paint(Graphics g) {
    25         g.drawImage(img, 0, 0, width, height, null);
    26     }
    27     
    28 }
    View Code

    测试代码:

     1 package test;
     2 
     3 import javax.swing.JFrame;
     4 import javax.swing.JPanel;
     5 
     6 public class Test {
     7     public static void main(String[] args) {
     8         final int WIDTH = 600;
     9         final int HEIGHT = 600;
    10         JFrame frame = new JFrame("测试背景面板");
    11         JPanel panel = new BackgroundPanel("imgPath", WIDTH, HEIGHT);
    12         frame.add(panel);
    13         frame.setSize(WIDTH, HEIGHT);
    14         frame.setVisible(true);
    15         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    16     }
    17 }
    View Code
  • 相关阅读:
    STL中set求交集、并集、差集的方法
    Vijos 1308 埃及分数(迭代加深搜索)
    POJ 1161 Walls(Floyd , 建图)
    UVa 1601 万圣节后的早晨
    dp之完全背包
    dp之取数字问题
    dp之最长公共子序列
    枚举排列
    poj 3187 暴力枚举
    poj 2431 优先队列,贪心
  • 原文地址:https://www.cnblogs.com/wss-is-knight/p/3671784.html
Copyright © 2011-2022 走看看