将两个JPanel添加到JLayeredPane

时间:2014-07-15 20:07:18

标签: java swing jpanel jlayeredpane

我正在尝试向JPanel添加JLayeredPane,只取得了部分成功。我能够分别看到两个面板,但是当我将它们添加到一起时却看不到。当我把它们放在一起时,只显示DEFAULT_LAYER。任何想法都会很棒。

import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.JLayeredPane;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Container;
import java.awt.Component;
import java.awt.Insets;
import java.util.ArrayList;
import java.awt.LayoutManager;

public class ImageEditorView 
{
   public static int dimensionWidth  = 1280;
   public static int dimensionHeight = 640;
   private Color currentColor = Color.WHITE;
   private JLayeredPane layeredPane;

   private JPanel createEditPanel()
   {
      final CirclePanel editPanel = new CirclePanel();
      //editPanel.setLayout(new CircleLayout(true));

      editPanel.setBounds(50, 0, 150, 150);
      editPanel.setOpaque(true);

      return editPanel;
   }

   private JPanel createImageView()
   {
      JPanel imageView = new JPanel(new GridBagLayout());
      imageView.setPreferredSize(new Dimension(dimensionWidth, dimensionHeight));
      imageView.setBounds(0, 0, dimensionWidth, dimensionHeight);
      imageView.setBackground(currentColor);
      return imageView;
   }

   private void addToLayeredPane(JComponent component, int level)
   {
      //This prints out what is expected. values of 1 and 100 respectfully
      System.out.println("The level has a value of " + level);
      layeredPane.add(component, level);
   }

   public void createAndShowGUI()
   {
      JFrame f = new JFrame("ImageEditor");
      f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      f.setLocationByPlatform(true);

      layeredPane = new JLayeredPane();
      //Both createImageView and createEditPanel return with the type JPanel
      addToLayeredPane(createImageView(), JLayeredPane.DEFAULT_LAYER);
      addToLayeredPane(createEditPanel(), JLayeredPane.PALETTE_LAYER);

      f.setSize(dimensionWidth, dimensionHeight);
      f.setContentPane(layeredPane);
      f.setMinimumSize(f.getSize());
      f.setVisible(true);
   }
}

class CirclePanel extends JPanel
{
   @Override
   protected void paintComponent(Graphics g)
   {
      g.drawOval(0, 0, g.getClipBounds().width, g.getClipBounds().height);
   }
}

1 个答案:

答案 0 :(得分:2)

默认情况下,JPanel是不透明的。因此,您只会看到已添加到顶层的面板。尝试:

panel.setOpaque(false);
顶部面板上的

如果您需要更多帮助,请发布展示问题的正确SSCCE。您发布的代码无法编译,因此无法执行。

编辑:

问题在于您的addToLayeredPane(...)方法。您正在使用图层参数“int”。它必须是Integer

由于您使用int值添加组件,因此组件会正常添加到面板中并在其ZOrder中绘制,这基本上意味着添加的最后一个组件首先被绘制,因此您的“imageview”被绘制在“编辑视图”。

另外,您仍然没有像我建议的那样在“editpanel”上使用setOpaque(false)。代码只会起作用,因为您没有正确地进行自定义绘制。您应该始终在绘画方法的开头调用super.paintComponent(g)。这将自动绘制背景,这就是您需要使面板透明的原因。