使用 Swing 的 BG 图像,无需重写 PaintComponent

发布于 2024-12-06 05:53:38 字数 837 浏览 0 评论 0原文

我目前正在设计一个带有多个屏幕的菜单,每个屏幕上有多个按钮。要在 jLabel 中的背景图像顶部使用按钮(默认情况下,我不能将按钮放在 jLabel 的顶部),我使用了带有两个面板/菜单屏幕的 GridBagLayout,其中一个面板包含按钮(不透明 = false)和一个带有背景图像或 jLabel 的面板。为了根据用户在菜单中的位置切换当前显示的面板,我用单独的方法而不是类制作了每个菜单屏幕(也称为每 2 个面板)。

现在,我已经到了这样的地步:我正在处理不必要的复杂界面部分,并且我认为 GridBag 无法满足我的目的,所以我想知道是否有不同的方法来绘制我的背景图像,仍然能够使用图像顶部的按钮。

我查找的最流行的方法是重写 PaintComponent 方法,但我不能这样做,因为我已经在单独的方法中而不是类中创建了 JPanel。它们都包含在我原来的 JFrame 中。

帮助将不胜感激,谢谢!

刚刚添加了这段代码,但我的背景由于某种原因仍然是白色的?现在尝试其他建议,谢谢大家!

private void mainPanel() {

    icon = new ImageIcon(getClass().getResource("/phantasma/menuv1.png"));
mainContainer1 = new javax.swing.JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            g.drawImage(icon.getImage(), 0,0, null);
            super.paintComponent(g);
        }
    };

I'm currently designing a menu with several screens with multiple buttons on each screen. To use buttons on top of the background image, which is in a jLabel (by default, I can't put buttons on TOP of the jLabel), I used GridBagLayout with two panels/menu screen, one panel containing the buttons (opaque = false) and one panel with the background image, or jLabel. In order to switch the current panels being displayed, depending on where the user is in the menu, I made each menu screen (aka. every 2 panels) in separate methods, not classes.

Now, I've come to the point where I'm working on parts of the interface that are unnecessarily complicated, and I don't feel GridBag will serve my purposes, so I was wondering if there was a different way to draw my background image, still being able to use my buttons on top of the image.

The most popular way I looked up was overriding the paintComponent method, but I can't do that, since I've made my JPanels in separate methods, not classes. They're all contained in my original JFrame.

Help would be greatly appreciated, thank you!

Just added this code, but my background remains white for some reason? Trying the other suggestion right now, thanks guys!

private void mainPanel() {

    icon = new ImageIcon(getClass().getResource("/phantasma/menuv1.png"));
mainContainer1 = new javax.swing.JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            g.drawImage(icon.getImage(), 0,0, null);
            super.paintComponent(g);
        }
    };

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

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

发布评论

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

评论(2

祁梦 2024-12-13 05:53:38

带有 Swing 的 BG 图像,无需重写 PaintComponent

我不知道为什么所有帖子都建议为此进行自定义绘制。仅当您需要自动缩放背景图像时,您才需要进行自定义绘画。

如果您希望按实际大小绘制图像,请使用 JLabel。

我无法将按钮放在 jLabel 的顶部),

当然可以。只需为 JLabel 设置一个 LayoutManager,然后您就可以向其中添加任何组件,就像向面板添加组件一样。

BG image with Swing without overriding paintComponent

I have no idea why all the postings suggest doing custom painting for this. You would only do custom painting if you need to automatically scale the background image.

If you want the image painted at its real size then use a JLabel.

I can't put buttons on TOP of the jLabel),

Sure you can. Just set a LayoutManager for the JLabel and then you can add any component to it the same way you add components to a panel.

一个人的旅程 2024-12-13 05:53:38

在我上面的评论中我指出:

您始终可以创建一个匿名内部 JPanel 派生类,并在需要时重写其中的 PaintComponent 方法。

作为我的意思的一个例子,您可以在您创建的任何 JPanel 中重写paintComponent,无论它是从独立类派生还是在方法中创建的。例如,

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.*;

public class AnonInnerPanel {

   private static void createAndShowGui() {
      JPanel mainPanel = new JPanel() {
         @Override
         protected void paintComponent(Graphics g) {
            super.paintComponent(g);
         }

         @Override
         public Dimension getPreferredSize() {
            return new Dimension(300, 200);
         }
      };

      JFrame frame = new JFrame("AnonInnerPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

In my comment above I state:

You can always create an anonymous inner JPanel-derived class and override the paintComponent method there if need be.

As an example of what I mean, you can override paintComponent in any JPanel that you create whether it's derived from a stand-alone class or created within a method. For e.g.,

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.*;

public class AnonInnerPanel {

   private static void createAndShowGui() {
      JPanel mainPanel = new JPanel() {
         @Override
         protected void paintComponent(Graphics g) {
            super.paintComponent(g);
         }

         @Override
         public Dimension getPreferredSize() {
            return new Dimension(300, 200);
         }
      };

      JFrame frame = new JFrame("AnonInnerPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文