在 JLayeredPane 中使用 GridBagLayout

发布于 2024-11-26 10:23:52 字数 235 浏览 2 评论 0原文

我试图在 JPanel 的右上角有一个小的浮动“小部件”。它是一个固定大小的组件 - 想象一下谷歌地图视图中的指南针,如果有帮助的话。

我意识到 JLayeredPane 只对所有层使用一个布局管理器,因此认为使用 GBL 是成功的: - 使右上角的 (1, 0) 框非常小并将小部件放在那里 - 使内容面板的宽度/高度为2

但经过实验,GBL 似乎在重叠时删除了一些组件。

谁能建议一种伪造这种行为的方法?

I am attempting to have a small floating "widget" of sorts in the top right of a JPanel. It's a fixed-size component - think of the compass in a google maps sort of view, if that helps.

I realize that JLayeredPane only uses one layout manager for all the layers and so thought that using GBL with be successful:
- make the top right (1, 0) box very small and put the widget there
- make the content panel be of width/height 2

But after experimenting, it seems that GBL removes some components when they overlap.

Can anyone suggest a way of faking this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-12-03 10:23:52

它是一个分层窗格,因此如果需要,每个层都可以有一个使用自己布局的容器。我根本不会给 JLayeredPane 本身任何布局,而是使用其默认的空布局,然后考虑将小型浮动小部件放入使用任何所需布局的透明(非不透明)JPanel 中,并将透明 JPanel 添加到上部JLayeredPane 的层。

例如,以下代码将指南针的图像放置在非不透明 JPanel 的右上角,该 JPanel 位于显示地形图的 JLabel 之上:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class LayeredExample extends JLayeredPane {
   public static final String MAP_URL = "http://upload.wikimedia.org/" +
        "wikipedia/commons/c/c4/Maps-for-free_Sierra_Nevada.png";
   public static final String COMPASS_URL = "http://upload.wikimedia.org/" +
        "wikipedia/commons/thumb/f/f8/Compass_Rose_English_North.svg/" +
        "200px-Compass_Rose_English_North.svg.png";
   private Dimension imageSize;
   private JLabel defaultLabel = new JLabel();
   private JPanel palettePane = new JPanel();
   private JLabel compassLabel = new JLabel();

   public LayeredExample() {
      try {
         URL mapUrl = new URL(MAP_URL);
         BufferedImage mapImage = ImageIO.read(mapUrl);
         ImageIcon mapIcon = new ImageIcon(mapImage);
         defaultLabel.setIcon(mapIcon);

         URL compassUrl = new URL(COMPASS_URL);
         BufferedImage compassImage = ImageIO.read(compassUrl);
         ImageIcon compassIcon = new ImageIcon(compassImage);
         compassLabel.setIcon(compassIcon);

         imageSize = new Dimension(mapImage.getWidth(), mapImage.getHeight());
         setPreferredSize(imageSize);
         defaultLabel.setSize(imageSize);
         defaultLabel.setLocation(0, 0);
         palettePane.setSize(imageSize);
         palettePane.setLocation(0, 0);


         JPanel northPalettePane = new JPanel(new BorderLayout());
         northPalettePane.setOpaque(false);
         northPalettePane.add(compassLabel, BorderLayout.EAST);
         palettePane.setOpaque(false);
         palettePane.setLayout(new BorderLayout());
         palettePane.add(northPalettePane, BorderLayout.NORTH);

         add(defaultLabel, JLayeredPane.DEFAULT_LAYER);
         add(palettePane, JLayeredPane.PALETTE_LAYER);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("LayeredExample");
      frame.getContentPane().add(new LayeredExample());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

It's a layered pane, and so each layer can have a container that uses its own layout if desired. I wouldn't give the JLayeredPane itself any layout at all but rather use its default null layout and then would consider putting the small floating widget in a transparent (non-opaque) JPanel that uses any layout desired and add the transparent JPanel to an upper layer of the JLayeredPane.

For example this code puts an image of a compass in the upper right corner of a non-opaque JPanel that is layered over a JLabel that shows a relief map:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class LayeredExample extends JLayeredPane {
   public static final String MAP_URL = "http://upload.wikimedia.org/" +
        "wikipedia/commons/c/c4/Maps-for-free_Sierra_Nevada.png";
   public static final String COMPASS_URL = "http://upload.wikimedia.org/" +
        "wikipedia/commons/thumb/f/f8/Compass_Rose_English_North.svg/" +
        "200px-Compass_Rose_English_North.svg.png";
   private Dimension imageSize;
   private JLabel defaultLabel = new JLabel();
   private JPanel palettePane = new JPanel();
   private JLabel compassLabel = new JLabel();

   public LayeredExample() {
      try {
         URL mapUrl = new URL(MAP_URL);
         BufferedImage mapImage = ImageIO.read(mapUrl);
         ImageIcon mapIcon = new ImageIcon(mapImage);
         defaultLabel.setIcon(mapIcon);

         URL compassUrl = new URL(COMPASS_URL);
         BufferedImage compassImage = ImageIO.read(compassUrl);
         ImageIcon compassIcon = new ImageIcon(compassImage);
         compassLabel.setIcon(compassIcon);

         imageSize = new Dimension(mapImage.getWidth(), mapImage.getHeight());
         setPreferredSize(imageSize);
         defaultLabel.setSize(imageSize);
         defaultLabel.setLocation(0, 0);
         palettePane.setSize(imageSize);
         palettePane.setLocation(0, 0);


         JPanel northPalettePane = new JPanel(new BorderLayout());
         northPalettePane.setOpaque(false);
         northPalettePane.add(compassLabel, BorderLayout.EAST);
         palettePane.setOpaque(false);
         palettePane.setLayout(new BorderLayout());
         palettePane.add(northPalettePane, BorderLayout.NORTH);

         add(defaultLabel, JLayeredPane.DEFAULT_LAYER);
         add(palettePane, JLayeredPane.PALETTE_LAYER);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("LayeredExample");
      frame.getContentPane().add(new LayeredExample());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
貪欢 2024-12-03 10:23:52

或者,考虑 javax.swing.OverlayLayout< /代码>。示例可以在此处此处此处显示了说明对齐约束的示例JLabel 保持在面板下半部分的中心:

label.setAlignmentX(0.5f);
label.setAlignmentY(0.75f);

[image

Alternatively, consider javax.swing.OverlayLayout. Examples may be found here and here. An example illustrating alignment constraints is shown here; the JLabel remains centered in the lower half of the panel:

label.setAlignmentX(0.5f);
label.setAlignmentY(0.75f);

[image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文