如何在Java swing中使用GradientPaint实现阴影效果?

我想在我的项目中实现这种类型的文本字段

《如何在Java swing中使用GradientPaint实现阴影效果?》

我写了一个画家类来为我的文本字段设置背景.

这是我的主类,我将Nimbus设置为外观和感觉:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;

class NimbusBaseDemo extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JTextField jtxt, txtDisEnabled;
    int i;

    private UIManager.LookAndFeelInfo[] lafs;

    public NimbusBaseDemo() {
        try {

            // Set nimbus look and feel. nimbusBase works only for it.
            new NimbusBaseUI();

        } catch (Exception e) {
            e.printStackTrace();
        }

        setTitle("Nimbus Base Demo");
        setSize(400, 400);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        jtxt = new JTextField();
        jtxt = new JTextField("Enabled Text Field");
        jtxt.setEnabled(true);
        add(jtxt);
        // Disabled text field
        txtDisEnabled = new JTextField("Disabled Text Field");
        txtDisEnabled.setEnabled(false);
        add(txtDisEnabled);

    }

    public static void main(String args[]) {

        new NimbusBaseDemo();
    }
}

这是我的nimbus主题类,这里我按照我想要的方式扩展NimbusLookAndFell类和setdefaults.

import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
public class NimbusBaseUI extends NimbusLookAndFeel {
    public NimbusBaseUI() {
        super(); // Initialisation and installating
        try {
            new TextFieldTheme(this);
            UIManager.setLookAndFeel(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize() {
        // TODO Auto-generated method stub
        super.initialize();
    }

}

这是我的Text字段主题类,我在NimbusBaseUI类中使用它

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;

import javax.swing.Painter;

public class TextFieldTheme {

    public TextFieldTheme(NimbusBaseUI nimbusUI) {
        nimbusUI.getDefaults().put("TextField.opaque", false);
        nimbusUI.getDefaults().put("TextField.font",
                new Font("Myriad Pro Light", Font.PLAIN, 13));
        nimbusUI.getDefaults().put("TextField[Enabled].textForeground",
                new Color(0, 0, 0));

        nimbusUI.getDefaults().put("TextField.contentMargins",
                new Insets(2, 10, 2, 2));
        nimbusUI.getDefaults().put("TextField[Enabled].contentMargins",
                new Insets(2, 10, 2, 2));
        nimbusUI.getDefaults().put("TextField[Disabled].contentMargins",
                new Insets(2, 10, 2, 2));
        nimbusUI.getDefaults().put(
                "TextField[Enabled].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Focused].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Selected].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[MouseOver].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Enabled+Focused].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Enabled+Selected].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Enabled+MouseOver].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));

        nimbusUI.getDefaults().put("TextField[Disabled].textForeground",
                new Color(0, 0, 0));
        nimbusUI.getDefaults().put(
                "TextField[Disabled].backgroundPainter",
                new TextFeildPaintBorder(new Color(255, 255, 255), new Color(
                        255, 255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Disabled+Focused].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Disabled+Selected].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));
        nimbusUI.getDefaults().put(
                "TextField[Disabled+MouseOver].backgroundPainter",
                new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
                        255, 255)));

    }

    public class TextFeildPaintBorder implements Painter {

        private Color light, dark;
        private GradientPaint gradPaint;
        protected int strokeSize = 1;
        /** Color of shadow */

        /** Color of shadow */
        protected Color shadowColor = new Color(128, 128, 128, 140);

        /** Sets if it drops shadow */
        protected boolean shady = true;
        /** Sets if it has an High Quality view */
        protected boolean highQuality = false;
        /** Double values for Horizontal and Vertical radius of corner arcs */
        protected Dimension arcs = new Dimension(10, 10);
        /** Distance between shadow border and opaque panel border */
        protected int shadowGap = 1;
        /** The offset of shadow. */
        protected int shadowOffset = 1; // width of the shadow
        /** The transparency value of shadow. ( 0 - 255) */
        protected int shadowAlpha = 130;

        public TextFeildPaintBorder(Color light, Color dark) {
            this.light = light;
            this.dark = dark;
        }

        @Override
        public void paint(Graphics2D g, Object object, int w, int h) {

            Color shadowColorA = new Color(shadowColor.getRed(),
                    shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
            if (highQuality) {
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
            }
            if (shady) {
                g.setColor(shadowColorA);
                g.fillRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
                        arcs.height);
            } else {
                shadowGap = 1;
            }
            gradPaint = new GradientPaint((w / 2.0f), 0, new Color(255, 255,
                    255), (w / 2.0f), (h / 2.0f), new Color(255, 255, 255),
                    false);
            g.setPaint(gradPaint);

            g.fillRoundRect(shadowOffset,// X position
                    shadowOffset,// Y position
                    w - strokeSize - shadowOffset, // width
                    h - strokeSize - shadowOffset, // height
                    arcs.width, arcs.height);// arc Dimension
            g.setColor(new Color(188, 188, 187, 130));
            g.setStroke(new BasicStroke(strokeSize));
            g.drawRoundRect(shadowOffset,// X position
                    shadowOffset,// Y position
                    w - strokeSize - shadowOffset, // width
                    h - strokeSize - shadowOffset, // height
                    arcs.width, arcs.height);// arc Dimension
            g.setStroke(new BasicStroke());

        }

    }

}

在比喻和奔跑之后我得到了这个:

《如何在Java swing中使用GradientPaint实现阴影效果?》

最佳答案 您需要使用jtxt.setBorder(BorderFactory.createEmptyBorder(2,10,2,2))而不是d.put(“TextField.contentMargins”,新的Insets(2,10,2,2)):

《如何在Java swing中使用GradientPaint实现阴影效果?》

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NimbusBaseDemo2 {
  public JComponent makeUI() {
    //UIDefaults d = UIManager.getLookAndFeelDefaults();
    UIDefaults d = new UIDefaults();
    //d.put("TextField.opaque", false);
    //d.put("TextField.font", new Font("Myriad Pro Light", Font.PLAIN, 13));
    //d.put("TextField[Enabled].textForeground", new Color(0, 0, 0));
    //d.put("TextField[Disabled].textForeground", new Color(0, 0, 0));

    //Insets ins = new Insets(2, 10, 2, 2);
    //d.put("TextField.contentMargins", ins);
    //d.put("TextField[Enabled].contentMargins", ins);
    //d.put("TextField[Disabled].contentMargins", ins);

    Painter<JComponent> painter = new TextFeildPaintBorder(
      new Color(220, 220, 220), new Color(255, 255, 255));
    d.put("TextField[Enabled].backgroundPainter", painter);
    d.put("TextField[Focused].backgroundPainter", painter);
    d.put("TextField[Selected].backgroundPainter", painter);

    Painter<JComponent> painter2 = new TextFeildPaintBorder(
      new Color(200, 200, 200), new Color(220, 220, 220));
    d.put("TextField[Disabled].backgroundPainter", painter2);
    d.put("TextField[Disabled+Focused].backgroundPainter", painter2);
    d.put("TextField[Disabled+Selected].backgroundPainter", painter2);

    //TEST:
    //Painter<JComponent> painter3 = new Painter<JComponent>() {
    //  @Override public void paint(Graphics2D g, JComponent c, int w, int h) {
    //  }
    //};
    //d.put("TextField[Disabled].borderPainter", painter3);
    //d.put("TextField[Enabled].backgroundPainter", painter3);
    //d.put("TextField[Focused].backgroundPainter", painter3);

    JTextField jtxt = new JTextField("Enabled Text Field");
    jtxt.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));
    jtxt.putClientProperty("Nimbus.Overrides", d);

    JTextField txtDisEnabled = new JTextField("Disabled Text Field");
    txtDisEnabled.setEnabled(false);

    JTextField txtDisEnabled2 = new JTextField("Disabled Text Field");
    txtDisEnabled2.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));
    txtDisEnabled2.putClientProperty("Nimbus.Overrides", d);
    txtDisEnabled2.setEnabled(false);

    Box box = Box.createVerticalBox();
    box.add(new JSeparator());
    box.add(Box.createVerticalStrut(5));
    box.add(new JTextField("Enabled Text Field"));
    box.add(Box.createVerticalStrut(5));
    box.add(jtxt);
    box.add(Box.createVerticalStrut(5));
    box.add(new JSeparator());
    box.add(Box.createVerticalStrut(5));
    box.add(txtDisEnabled);
    box.add(Box.createVerticalStrut(5));
    box.add(txtDisEnabled2);
    box.add(Box.createVerticalStrut(5));
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    p.add(box, BorderLayout.NORTH);
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      try {
        for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(laf.getName())) {
            UIManager.setLookAndFeel(laf.getClassName());
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new NimbusBaseDemo2().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

class TextFeildPaintBorder implements Painter<JComponent> {
  private Color light, dark;
  private GradientPaint gradPaint;
  protected int strokeSize = 1;
  /** Color of shadow */
  protected Color shadowColor = new Color(128, 128, 128, 140);

  /** Sets if it drops shadow */
  protected boolean shady = true;
  /** Sets if it has an High Quality view */
  protected boolean highQuality = false;
  /** Double values for Horizontal and Vertical radius of corner arcs */
  protected Dimension arcs = new Dimension(10, 10);
  /** Distance between shadow border and opaque panel border */
  protected int shadowGap = 1;
  /** The offset of shadow. */
  protected int shadowOffset = 1; // width of the shadow
  /** The transparency value of shadow. ( 0 - 255) */
  protected int shadowAlpha = 130;

  public TextFeildPaintBorder(Color light, Color dark) {
    this.light = light;
    this.dark = dark;
  }

  @Override
  public void paint(Graphics2D g, JComponent c, int w, int h) {
    Color shadowColorA = new Color(
      shadowColor.getRed(),
      shadowColor.getGreen(),
      shadowColor.getBlue(),
      shadowAlpha);
    if (highQuality) {
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);
    }
    if (shady) {
      g.setColor(shadowColorA);
      g.fillRoundRect(
        0, 0, w - shadowGap, h - shadowGap, arcs.width, arcs.height);
    } else {
      shadowGap = 1;
    }
    gradPaint = new GradientPaint(0, 0, light, 0, h * .5f, dark, false);
    g.setPaint(gradPaint);

    g.fillRoundRect(shadowOffset,// X position
                    shadowOffset,// Y position
                    w - strokeSize - shadowOffset, // width
                    h - strokeSize - shadowOffset, // height
                    arcs.width, arcs.height);// arc Dimension
    g.setColor(new Color(188, 188, 187, 130));
    g.setStroke(new BasicStroke(strokeSize));
    g.drawRoundRect(shadowOffset,// X position
                    shadowOffset,// Y position
                    w - strokeSize - shadowOffset, // width
                    h - strokeSize - shadowOffset, // height
                    arcs.width, arcs.height);// arc Dimension
    g.setStroke(new BasicStroke());
  }
}

加:

I need to apply same text field theme all text fields in my project.

另一种选择是使用TextField [Focused] .borderPainter和TextField.contentMargins:

d.put("TextField.contentMargins", insets);
d.put("TextField[Focused].borderPainter", emptyPainter);

NimbusBaseDemo3

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NimbusBaseDemo3 {
  public JComponent makeUI() {
    JTextField jtxt = new JTextField("Enabled Text Field");

    JTextField txtDisEnabled = new JTextField("Disabled Text Field");
    txtDisEnabled.setEnabled(false);

    JTextField txtDisEnabled2 = new JTextField("Disabled Text Field");
    txtDisEnabled2.setEnabled(false);

    Box box = Box.createVerticalBox();
    box.add(new JSeparator());
    box.add(Box.createVerticalStrut(5));
    box.add(new JTextField("Enabled Text Field"));
    box.add(Box.createVerticalStrut(5));
    box.add(jtxt);
    box.add(Box.createVerticalStrut(5));
    box.add(new JSeparator());
    box.add(Box.createVerticalStrut(5));
    box.add(txtDisEnabled);
    box.add(Box.createVerticalStrut(5));
    box.add(txtDisEnabled2);
    box.add(Box.createVerticalStrut(5));
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    p.add(box, BorderLayout.NORTH);
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      try {
        for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(laf.getName())) {
            UIManager.setLookAndFeel(laf.getClassName());

            UIDefaults d = UIManager.getLookAndFeelDefaults();

            Insets ins = new Insets(2, 10, 2, 2);
            d.put("TextField.contentMargins", ins);
            d.put("TextField[Enabled].contentMargins", ins);
            d.put("TextField[Disabled].contentMargins", ins);

            Painter<JComponent> painter = new TextFeildPaintBorder(
              new Color(220, 220, 220), new Color(255, 255, 255));
            d.put("TextField[Enabled].backgroundPainter", painter);
            d.put("TextField[Focused].backgroundPainter", painter);
            d.put("TextField[Selected].backgroundPainter", painter);

            Painter<JComponent> painter2 = new TextFeildPaintBorder(
              new Color(200, 200, 200), new Color(220, 220, 220));
            d.put("TextField[Disabled].backgroundPainter", painter2);

            Painter<JComponent> painter3 = new Painter<JComponent>() {
              @Override public void paint(Graphics2D g, JComponent c, int w, int h) {}
            };
            d.put("TextField[Disabled].borderPainter", painter3);
            d.put("TextField[Enabled].borderPainter", painter3);
            d.put("TextField[Focused].borderPainter", painter3);
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new NimbusBaseDemo3().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

class TextFeildPaintBorder implements Painter<JComponent> {
  private Color light, dark;
  private GradientPaint gradPaint;
  protected int strokeSize = 1;
  /** Color of shadow */
  protected Color shadowColor = new Color(128, 128, 128, 140);

  /** Sets if it drops shadow */
  protected boolean shady = true;
  /** Sets if it has an High Quality view */
  protected boolean highQuality = false;
  /** Double values for Horizontal and Vertical radius of corner arcs */
  protected Dimension arcs = new Dimension(10, 10);
  /** Distance between shadow border and opaque panel border */
  protected int shadowGap = 1;
  /** The offset of shadow. */
  protected int shadowOffset = 1; // width of the shadow
  /** The transparency value of shadow. ( 0 - 255) */
  protected int shadowAlpha = 130;

  public TextFeildPaintBorder(Color light, Color dark) {
    this.light = light;
    this.dark = dark;
  }

  @Override
  public void paint(Graphics2D g, JComponent c, int w, int h) {
    Color shadowColorA = new Color(
      shadowColor.getRed(),
      shadowColor.getGreen(),
      shadowColor.getBlue(),
      shadowAlpha);
    if (highQuality) {
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);
    }
    if (shady) {
      g.setColor(shadowColorA);
      g.fillRoundRect(
        0, 0, w - shadowGap, h - shadowGap, arcs.width, arcs.height);
    } else {
      shadowGap = 1;
    }
    gradPaint = new GradientPaint(0, 0, light, 0, h * .5f, dark, false);
    g.setPaint(gradPaint);

    g.fillRoundRect(shadowOffset,// X position
                    shadowOffset,// Y position
                    w - strokeSize - shadowOffset, // width
                    h - strokeSize - shadowOffset, // height
                    arcs.width, arcs.height);// arc Dimension
    g.setColor(new Color(188, 188, 187, 130));
    g.setStroke(new BasicStroke(strokeSize));
    g.drawRoundRect(shadowOffset,// X position
                    shadowOffset,// Y position
                    w - strokeSize - shadowOffset, // width
                    h - strokeSize - shadowOffset, // height
                    arcs.width, arcs.height);// arc Dimension
    g.setStroke(new BasicStroke());
  }
}
点赞