最简单的代码在Java中圆角JLabel

时间:2014-09-11 20:42:16

标签: java swing graphics jlabel rounded-corners

我有很多JLabel,并希望在它们周围有光滑的角落。我该怎么做?我已经搜索了SO,但我没有找到任何答案。

有人可以帮助我使用一个简单而准确的代码来制作JLabel的圆角吗?

其他问题是询问一些额外的细节,例如border和其他问题,但我想要用于制作JLabel的圆角的完全和最简单的代码。

3 个答案:

答案 0 :(得分:7)

说真的,最简单的解决方案是在RoundRectangle2D周围画一个JLabel ...

现在,您可以在标签的paintComponentpaintBorder方法中执行此操作,但为什么在您可以创建自己的边框并在可重用的情况下完成工作时,您会感到烦恼方式,例如......

RoundedBorder

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.AbstractBorder;

public class TestRoundedBorder {

    public static void main(String[] args) {
        new TestRoundedBorder();
    }

    public TestRoundedBorder() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBackground(Color.RED);
            setLayout(new GridBagLayout());
            JLabel label = new JLabel("Test");
            label.setBorder(new RoundedBorder(Color.BLACK, 20));
            add(label);
        }

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

    }

    public class RoundedBorder extends AbstractBorder {

        private final Color color;
        private final int gap;

        public RoundedBorder(Color c, int g) {
            color = c;
            gap = g;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(color);
            g2d.draw(new RoundRectangle2D.Double(x, y, width - 1, height - 1, gap, gap));
            g2d.dispose();
        }

        @Override
        public Insets getBorderInsets(Component c) {
            return (getBorderInsets(c, new Insets(gap, gap, gap, gap)));
        }

        @Override
        public Insets getBorderInsets(Component c, Insets insets) {
            insets.left = insets.top = insets.right = insets.bottom = gap / 2;
            return insets;
        }

        @Override
        public boolean isBorderOpaque() {
            return false;
        }
    }

}

现在,如果你想要"顺利"边缘,您必须向RenderingHints上下文提供Graphics,例如......

Smoothed Edges

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setColor(color);
    g2d.draw(new RoundRectangle2D.Double(x + 1, y + 1, width - 2, height - 2, gap, gap));
    g2d.dispose();
}

答案 1 :(得分:2)

您无法在实际的JLabel区域进行圆角处理;它们总是长方形的。但是,一个简单的替代方法是将ImageIcon的{​​{1}}设置为带有圆边的图像,而不是使用边框。设置JLabel

ImageIcon

您的图片应具有yourLabel.setIcon(new ImageIcon(getClass().getResource("/path/to/your/image.png")); // Note: Relative path, starts from root of project 的尺寸。 请注意,如果找不到图像,则会抛出JLabel。确保你找到正确的道路!

创建一个NullPointerException,其大小调整为ImageIcon

的大小
JLabel

修改

这是使用ImageIcon ico = new ImageIcon("/path/to/your/image.png"); Image img = ico.getImage(); BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); g.drawImage(img, 0, 0, yourLabel.getWidth(), yourLabel.getHeight(), null); IconImage newIco = new IconImage(bi); yourLabel.setIcon(newIco); 制作带圆角的边框的最佳方法。 首先,创建一个名为RoundedBorder的新类。将此代码粘贴到其中:

Graphics2D

然后,在您的JFrame类中,要将其设置为import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.geom.Line2D; import javax.swing.border.AbstractBorder; public class RoundedBorder extends AbstractBorder { public RoundedBorder(Color c, int g) { color = c; gap = g; } @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { super.paintBorder(c, g, x, y, width, height); Graphics2D g2d; if (g instanceof Graphics2D) { g2d = (Graphics2D) g; g2d.setColor(color); System.out.println(x + y); g2d.draw(new Line2D.Double((double)x, (double)y + 10, (double)x + 3, (double)y + 3)); g2d.draw(new Line2D.Double((double)x + 3, (double)y + 3, (double)x + 10, (double)y)); g2d.draw(new Line2D.Double((double)x + 10, (double)y, (double)x + 30, (double)y)); g2d.draw(new Line2D.Double((double)x + 30, (double)y, (double)x + 33, (double)y + 2)); g2d.draw(new Line2D.Double((double)x + 33, (double)y + 2, (double)x + 36, (double)y + 8)); g2d.draw(new Line2D.Double((double)x + 36, (double)y + 8, (double)x + 36, (double)y + 28)); g2d.draw(new Line2D.Double((double)x + 36, (double)y + 28, (double)x + 34, (double)y + 31)); g2d.draw(new Line2D.Double((double)x + 34, (double)y + 31, (double)x + 32, (double)y + 33)); g2d.draw(new Line2D.Double((double)x + 32, (double)y + 33, (double)x + 6, (double)y + 33)); g2d.draw(new Line2D.Double((double)x + 6, (double)y + 33, (double)x + 3, (double)y + 31)); g2d.draw(new Line2D.Double((double)x + 3, (double)y + 31, (double)x, (double)y + 27)); g2d.draw(new Line2D.Double((double)x, (double)y + 27, (double)x, (double)y + 10)); } } @Override public Insets getBorderInsets(Component c) { return (getBorderInsets(c, new Insets(gap, gap, gap, gap))); } @Override public Insets getBorderInsets(Component c, Insets insets) { insets.left = insets.top = insets.right = insets.bottom = gap; return insets; } @Override public boolean isBorderOpaque() { return true; } // Variable declarations private final Color color; private final int gap; } 的边框,请执行:

JLabel

正如MadProgrammer所提到的,比画线更有效的方法是使用yourLabel.setBorder(new RoundedBorder(Color.black, 10)); 。要使用此功能,请使用

替换所有RoundRectangle2D
draw

随意修改边框。以下是使用g2d.draw(new RoundRectangle2D.Double(x, y, width - 1, height - 1, gap, gap)); 的语法:

Graphics2D

OR

g2d.draw(new Line2D.Double((double)x1, (double)y1, (double)x2, (double)y2));

我希望这有帮助!

答案 2 :(得分:1)

simplest是使用LineBorder类。

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class RoundedLineBorder extends JPanel {

  public RoundedLineBorder() {
    super(true);

    JLabel label = new JLabel("<html>Stack<br/>Overflow</html>");
    LineBorder line = new LineBorder(Color.blue, 1, true); // color, thickness, rounded
    label.setBorder(line);
    add(label, BorderLayout.CENTER);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("Rounded Line Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 200);
    frame.setContentPane(new RoundedLineBorder());
    frame.setVisible(true);
  }
} 

但结果并不是镇上最好看的圆角; - )