1464521427 发表于 2021-12-24 23:56:10

contentPane添加背景图片

用哪个eclipse可视化界面如何给contentPane添加背景图片呢?

412046761 发表于 2021-12-26 12:35:25


    public Test() {
      JLayeredPane pane = new JLayeredPane();
      JLabel label;
      JPanel panel1 = new JPanel();
      JTextField field1 = new JTextField();
      ImageIcon image;
      // 读取图片注意文件路径和文件后缀
      image = new ImageIcon("0.jpeg");
      label = new JLabel(image);//把图片添加到标签里
      panel1.setBounds(0, 0, image.getIconWidth(), image.getIconHeight()); //把标签设置为和图片等高等宽
      panel1 = (JPanel)this.getContentPane(); //把我的面板设置为内容面板
      panel1.add(label);
      pane.add(panel1,JLayeredPane.DEFAULT_LAYER);
      this.setTitle("派大星");
      this.setBounds(100,100,image.getIconWidth(), image.getIconHeight());
      this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      this.setLayeredPane(pane);
      this.setVisible(true);

    }

    public static void main(String[] args) {
      new Test();
    }

不会起名字的我 发表于 2021-12-30 18:10:03

@Override
public void paintComponent(Graphics g) {
        backimage = ImageIcon("图片路径地址").getImage();
        Graphics2D g2 = (Graphics2D)g;
        int width = getWidth();// 定义桌面面板的宽度
        int height = getHeight();// 定义桌面面板的高度
        g2.drawImage(backimage, 0, 0, width, height, this);// 绘制背景图片
}
页: [1]
查看完整版本: contentPane添加背景图片