最简单的代码在Java中围绕JLabel的角落 [英] Simplest Code to round corners of JLabel in java

查看:150
本文介绍了最简单的代码在Java中围绕JLabel的角落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多 JLabel 的并且想要在它们周围平滑转弯。我怎样才能做到这一点?我已经搜索过,但我没有找到任何答案。

有人可以帮我用一个简单而准确的代码来制作其他的问题是要求一些额外的细节,比如边框和其他的东西,但是我想只需要精确和简单的代码来制作圆角 JLabel 的。

解决方案

code> JLabel 区域;它们总是矩形的。但是,一个简单的替代方法是将 JLabel ImageIcon 设置为具有圆角边缘的图像,而不是使用边框。要设置 ImageIcon

  yourLabel.setIcon(new ImageIcon(getClass ().getResource(/ path / to / your / image.png)); 
//注意:相对路径,从项目的根目录开始

您的图片的维度应该是 JLabel
请注意,这会导致 NullPointerException 如果没有找到图像,请确保你找到了正确的路径!



创建一个 ImageIcon ,其大小调整为<​​code> 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);

编辑:



以下是使用 Graphics2D 。
首先,创建一个名为RoundedBorder的新类。将此代码粘贴到它中:

  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;

$ b @Override
public void paintBorder(Component c,Graphics g,int x,int y,int width,int height){
super.paintBorder( c,g,x,y,宽度,高度);
Graphics2D g2d;
if(g instanceof Graphics2D){
g2d =(Graphics2D)g;
g2d.setColor(color);
System.out.println(x + y); (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
g2d.draw(new Line2D.Double((double)x + 10,(double)y,(double)x + 30,(double)y)); (double)x + 30,(double)y,(double)x + 33,(double)y + 2));
g2d.draw(new Line2D.Double
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)); (double)x + 32,(double)y + 33,(double)x + 6,(double)y + 33));
g2d.draw(new Line2D.Double
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)));

$ b @Override
public Insets getBorderInsets(Component c,Insets insets){
insets.left = insets.top = insets.right = insets.bottom =间隙;
返回插入;
}

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

//变量声明
private final颜色的颜色;
私人最终诠释差距;

$ / code>

然后,在你的JFrame类中,将它设置为 JLabel ,do:

  yourLabel.setBorder(new RoundedBorder(Color.black ,10)); 

正如MadProgrammer提到的,比绘制线条更有效的方法是使用的RoundRectangle2D 。要使用它,请用

  g2d替换所有绘制行。绘制(新的RoundRectangle2D.Double(x,y,width  -  1,height  -  1,gap,gap)); 

随意根据需要修改边框。下面是使用 Graphics2D 的语法:

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

OR

  g2d.draw(new Line2D.Double(Point2D p1,Point2D p2)); 

我希望这有助于!


I have many JLabel's and want smooth around corners on them. How can I make this? I already searched on SO but I didn't find any answer.

Could someone help me with a simple and exact code for making round corners for JLabel's?

Other questions are asking some extra details like border and others but I want just exact and simplest code for making round corners for JLabel's.

解决方案

You cannot round corners on the actual JLabel area; they are always rectangular. However, a simple alternative is to set the ImageIcon of the JLabel to an image with rounded edges and not use a border. To set an ImageIcon:

yourLabel.setIcon(new ImageIcon(getClass().getResource("/path/to/your/image.png"));
// Note: Relative path, starts from root of project

Your image should have the dimensions of your JLabel. Note that this will throw a NullPointerException if the image is not found. Make sure you get the right path!

To create an ImageIcon that resizes to the size of the 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);

EDIT:

Here is the best way to make a border with rounded corners, using Graphics2D. First, make a new class called RoundedBorder. Paste this code into it:

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;
}

Then, in your JFrame class, to set this as the border of a JLabel, do:

yourLabel.setBorder(new RoundedBorder(Color.black, 10));

As MadProgrammer mentioned, a more efficient way than drawing lines is to use a RoundRectangle2D. To use this, replace all of the draw lines with

g2d.draw(new RoundRectangle2D.Double(x, y, width - 1, height - 1, gap, gap));

Feel free to modify the border as you wish. Here is the syntax for using Graphics2D:

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

OR

g2d.draw(new Line2D.Double(Point2D p1, Point2D p2));

I hope this helped!

这篇关于最简单的代码在Java中围绕JLabel的角落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆