如何在多个 jPanel 上进行 GradientPaint?

发布于 2024-11-05 02:45:58 字数 1001 浏览 4 评论 0原文

我有一个视图对象,它是一个 jPanel,并包含其他 jPanel,而其他 jPanel 又包含 jLabels。我想在对象上绘制渐变叠加层,使其具有漂亮的时尚外观,而不是无聊的朴素外观。

到目前为止我的尝试是:

public class InfoDisplay extends javax.swing.JPanel {


    @Override
     public void paintComponent(Graphics g) {
        UIDefaults uid = UIManager.getDefaults();
        Graphics2D g2d = (Graphics2D)g;

        int w = getWidth();
        int h = getHeight();

        Color lightBlue = new Color(41, 117, 200);
        Color darkBlue = new Color(2, 47, 106);

        if (!isOpaque()) {
            super.paintComponent( g );
            return;
        }

        GradientPaint gp = new GradientPaint(0, 0, lightBlue, 0, h, darkBlue );

        g2d.setPaint(gp);
        g2d.fillRect( 0, 0, w, h );

        setOpaque( false );
        super.paintComponent( g );
        setOpaque( true );
    }
}

这似乎根本没有改变对象背景。我对处理与 Gui 默认值无关的事情还很陌生。

我使用 Netbeans 中的 Gui 构建器来创建该对象,因此 initComponents() 也在类中,但我仅发布了与问题相关的源代码。

也许有人可以指出我正确的方向?

I have a view object that is a jPanel and holds other jPanels which in turn hold jLabels. I'm wanting to paint a gradient overlay on the object to give it a nice sleek look rather than the boring plain look.

My attempt thus far is:

public class InfoDisplay extends javax.swing.JPanel {


    @Override
     public void paintComponent(Graphics g) {
        UIDefaults uid = UIManager.getDefaults();
        Graphics2D g2d = (Graphics2D)g;

        int w = getWidth();
        int h = getHeight();

        Color lightBlue = new Color(41, 117, 200);
        Color darkBlue = new Color(2, 47, 106);

        if (!isOpaque()) {
            super.paintComponent( g );
            return;
        }

        GradientPaint gp = new GradientPaint(0, 0, lightBlue, 0, h, darkBlue );

        g2d.setPaint(gp);
        g2d.fillRect( 0, 0, w, h );

        setOpaque( false );
        super.paintComponent( g );
        setOpaque( true );
    }
}

This doesn't seem to change the objects background at all. I'm fairly new to messing with things that aren't related to the Gui defaults.

I used the Gui builder in Netbeans to create the object, so initComponents() is also in the class, but I posted only the source that is relevant to the question.

Perhaps someone can point me in the right direction?

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

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

发布评论

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

评论(1

丢了幸福的猪 2024-11-12 02:45:58

如果您希望背景 JPanel 使用渐变绘画,那么只需使用它即可。不要在代码中使用 setOpaque 和 super.paintComponent 做那些有趣的事情。例如,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;

@SuppressWarnings("serial")
public class GradientPaintPanel extends JPanel {
   private static final Color LIGHT_BLUE = new Color(41, 117, 200);
   private static final Color DARK_BLUE = new Color(2, 47, 106);

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      GradientPaint gradPaint = new GradientPaint(0, 0, LIGHT_BLUE, 0, getHeight(), DARK_BLUE);
      g2.setPaint(gradPaint);
      g2.fillRect(0, 0, getWidth(), getHeight());
   }

   public GradientPaintPanel() {

   }

   private static void createAndShowUI() {
      GradientPaintPanel gradPaintPanel = new GradientPaintPanel();
      gradPaintPanel.setPreferredSize(new Dimension(400, 300));
      JFrame frame = new JFrame("GradientPaintEg");
      frame.getContentPane().add(gradPaintPanel);
      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();
         }
      });
   }
}

If you want a background JPanel to use a gradient paint, then just use it. Don't do all that funny stuff in your code with setOpaque and super.paintComponent. e.g.,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;

@SuppressWarnings("serial")
public class GradientPaintPanel extends JPanel {
   private static final Color LIGHT_BLUE = new Color(41, 117, 200);
   private static final Color DARK_BLUE = new Color(2, 47, 106);

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      GradientPaint gradPaint = new GradientPaint(0, 0, LIGHT_BLUE, 0, getHeight(), DARK_BLUE);
      g2.setPaint(gradPaint);
      g2.fillRect(0, 0, getWidth(), getHeight());
   }

   public GradientPaintPanel() {

   }

   private static void createAndShowUI() {
      GradientPaintPanel gradPaintPanel = new GradientPaintPanel();
      gradPaintPanel.setPreferredSize(new Dimension(400, 300));
      JFrame frame = new JFrame("GradientPaintEg");
      frame.getContentPane().add(gradPaintPanel);
      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();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文