在JPanel

时间:2017-12-01 07:38:27

标签: java image swing jpanel

我使用JPanel嵌入了两张图片。一个是png图像,另一个是jpeg。我必须将png图像覆盖在jpeg上。我试过了。如果,我错了,请修理我。

import java.awt.*;
import java.io.IOException;

import javax.swing.*;

public class Test extends JFrame 
{
  public Test()
  {
    super();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1366,768);

    onInit();

    setVisible(true);
  }
  private void onInit()
  {

    JPanel panel=new JPanel();
    panel.setLayout(null);
    panel.setBounds(0,0,1366,768);
    panel.setBackground(new Color(0,0,0,0));
    JLayeredPane lp = getLayeredPane();
    lp.setBounds(0,0,1366,768);
    JLabel adLabel1=new JLabel();
    adLabel1.setBounds(0,0,1366,768);
    Image img1=new ImageIcon("F:\\wall papers\\Download-High-Res-Crazy-Concrete-Textures.jpg").getImage();  
    ImageIcon ad1=new ImageIcon(img1.getScaledInstance(1366,768,Image.SCALE_SMOOTH));
    adLabel1.setIcon(ad1);
    JLabel adLabel2 = new JLabel();
    adLabel2.setBounds(0, 0, 1366, 768);
    Image img2=new ImageIcon("<path>\\A100004.png").getImage();  
    ImageIcon ad2=new ImageIcon(img2.getScaledInstance(1366,768,Image.SCALE_SMOOTH));
    adLabel2.setIcon(ad2);
    adLabel2.setBackground(new Color(0,0,0,0));
    adLabel2.revalidate();
    lp.add(adLabel1,JLayeredPane.MODAL_LAYER);
    lp.add(adLabel2,JLayeredPane.DRAG_LAYER);
    panel.add(lp);


  }

  public static void main(String args[])
  {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        new Test();
      }
    });
  }
}

如果我不使用JPanel它正在运行,它只显示一个空白面板。但我希望它能在JPanel中完成。

示例:

enter image description here

我尝试了这个png图片:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用多种可能的解决方案,例如尝试:

  • 使用GridBagLayout,虽然这会很快变得非常复杂
  • 创建您自己的LayoutManager,但这可能对问题有点过分
  • 使用OverlayLayout经理,但由于我对此没有任何经验,我不能说它是否能解决您的问题。

或者,你可以......

掌控整个过程并回退到自定义绘画。这使您具有与编写自己的LayoutManager相同的灵活性,但没有复杂性,因为您只是绘制图像,使生活变得更加简单。

如果您需要显示其他组件(如文本或文本字段),您可以使用自定义绘画路线将图像渲染为背景,使用一个或布局管理器来处理其他组件。

Example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                try {
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage background;
        private BufferedImage foreground;

        public TestPane() throws IOException {
            background = ImageIO.read(getClass().getResource("Background.png"));
            foreground = ImageIO.read(getClass().getResource("Foreground.png"));
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = 0;
            int y = 0;
            int width = getWidth();
            if (background != null) {
                x = (getWidth() - background.getWidth()) / 2;
                y = (getHeight() - background.getHeight()) / 2;
                width = background.getWidth();
                g2d.drawImage(background, x, y, this);
            }
            if (foreground != null) {
                x = x + width - foreground.getWidth();
                g2d.drawImage(foreground, x, y, this);
            }
            g2d.dispose();
        }

    }

}