`
ruantongsheng
  • 浏览: 21851 次
  • 来自: ...
社区版块
存档分类
最新评论

圆角Border,JLabel,JPanel

 
阅读更多
//=============================Border===================================
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.AbstractBorder;
public class RoundRectBorder extends AbstractBorder {

private static final long serialVersionUID = 1L;
private Color fieldColor = Color.white;
private int fieldThickness = 1;

public RoundRectBorder(Color color, int thickness) { 
fieldColor = color;
fieldThickness  = thickness;
}
public RoundRectBorder(Color color) { 
fieldColor = color;
}
public Insets getBorderInsets(Component component) {
int x = fieldThickness/2;
int y = fieldThickness/2;
int w = fieldThickness/2;
int h = fieldThickness/2;
Insets insets = new Insets(x,y,w,h);
return insets;
}

public boolean isBorderOpaque() {
return false;
}

public void paintBorder(Component component, Graphics g, int x,
int y, int width, int height) {
Color oldColor = g.getColor();
        g.setColor(fieldColor);     
        g.fillRect(x, y, fieldThickness/2, fieldThickness/2);
        g.fillRect(x+width - fieldThickness/2, y, fieldThickness/2, fieldThickness/2);
        g.fillRect(x+width - fieldThickness/2, y + height - fieldThickness/2, fieldThickness/2, fieldThickness/2);
        g.fillRect(x, y + height - fieldThickness/2, fieldThickness/2, fieldThickness/2);
       
        g.setColor(oldColor);
        g.fillArc(x, y, fieldThickness, fieldThickness, 90, 90);
        g.fillArc(x+width - fieldThickness, y, fieldThickness, fieldThickness, 0, 90);
        g.fillArc(x+width - fieldThickness, y + height - fieldThickness, fieldThickness, fieldThickness, 0, -90);
        g.fillArc(x, y + height - fieldThickness, fieldThickness,fieldThickness, -90, -90);
         
        g.setColor(oldColor);
}
}

//=============================Label===================================
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JLabel;
public class RoundRectLabel extends JLabel {
private static final long serialVersionUID = 7049192876836921123L;
private int fieldRoundRect;
private int fieldArc = 1234;

/**
* Create a label with round arc
* @param text: text show in label
* @param roundRect: round arc
* @param arc: should be in {1,2,3,4,12,34,1234}, default is 1234
*/
public RoundRectLabel(String text, int roundRect,int arc) { 
super(text);
fieldArc = arc;
fieldRoundRect = roundRect;
}

public void paintComponent(Graphics g) {
g.setColor(getBackground());
int fieldX = getBounds().x;
int fieldY = getBounds().y;
int fieldWeight = getSize().width;
int fieldHeight = getSize().height;

Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if(fieldArc == 1){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY+fieldRoundRect/2, fieldWeight, fieldHeight);
g2d.fillRect(fieldX+fieldWeight-fieldRoundRect/2, fieldY, fieldRoundRect/2, fieldHeight);
}else if(fieldArc == 2){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY+fieldRoundRect/2, fieldWeight, fieldHeight);
g2d.fillRect(fieldX, fieldY, fieldRoundRect/2, fieldHeight);
}else if(fieldArc == 3){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY, fieldWeight, fieldRoundRect/2);
g2d.fillRect(fieldX, fieldY, fieldRoundRect/2, fieldHeight);
}else if(fieldArc == 4){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY, fieldWeight, fieldRoundRect/2);
g2d.fillRect(fieldX+fieldWeight-fieldRoundRect/2, fieldY, fieldRoundRect/2, fieldHeight);
}else if(fieldArc == 12){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY+fieldRoundRect/2, fieldWeight, fieldHeight);
}else if(fieldArc == 34){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY, fieldWeight, fieldRoundRect/2);
}else if(fieldArc == 1234){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
}

super.paintComponent(g);
}

}

//================================Panel================================
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;
public class RoundRectPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 7049192876836921123L;
private int fieldRoundRect;
private int fieldArc = 1234;

/**
* Create a Panel with round arc
* @param roundRect: round arc
* @param arc: should be in {1,2,3,4,12,34,1234}, default is 1234
*/
public RoundRectPanel(int roundRect,int arc) { 
fieldArc = arc;
fieldRoundRect = roundRect;
setOpaque(true);
}

public void paintComponent(Graphics g) {

int fieldX = 0;
int fieldY = 0;
int fieldWeight = getSize().width;
int fieldHeight = getSize().height;
Graphics2D g2d = (Graphics2D)g;
g.setColor(Color.white);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRect(fieldX, fieldY, fieldWeight, fieldHeight);
g.setColor(getBackground());
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight,fieldRoundRect,fieldRoundRect);

if(fieldArc == 1){
g2d.fillRect(fieldX, fieldY+fieldRoundRect/2, fieldWeight, fieldHeight);
g2d.fillRect(fieldX+fieldWeight-fieldRoundRect/2, fieldY, fieldRoundRect/2, fieldHeight);
}else if(fieldArc == 2){
g2d.fillRect(fieldX, fieldY, fieldWeight-fieldRoundRect/2, fieldHeight);
g2d.fillRect(fieldWeight-fieldRoundRect/2, fieldHeight-fieldRoundRect/2, fieldRoundRect/2, fieldRoundRect/2);
}else if(fieldArc == 3){
g2d.fillRect(fieldX, fieldY, fieldWeight, fieldRoundRect/2);
g2d.fillRect(fieldX, fieldY, fieldRoundRect/2, fieldHeight);
}else if(fieldArc == 4){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY, fieldWeight, fieldRoundRect/2);
g2d.fillRect(fieldX+fieldWeight-fieldRoundRect/2, fieldY, fieldRoundRect/2, fieldHeight);
}else if(fieldArc == 12){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY+fieldRoundRect/2, fieldWeight, fieldHeight);
}else if(fieldArc == 34){
g2d.fillRoundRect(fieldX, fieldY, fieldWeight, fieldHeight, fieldRoundRect, fieldRoundRect);
g2d.fillRect(fieldX, fieldY, fieldWeight, fieldRoundRect/2);
}
}

}


//userage
RoundRectLabel roundRectLabel_1 = new RoundRectLabel("", 10, 2);
roundRectLabel_1.setForeground(Color.white);
roundRectLabel_1.setBackground(new Color(91,124,168));
roundRectLabel_1.setPreferredSize(new Dimension(0, 20));
roundRectLabel_1.setText("");
rightTopPanel1.add(roundRectLabel_1, BorderLayout.NORTH);
//=============
RoundRectPanel roundRectPanel = new RoundRectPanel(0, 0);
roundRectPanel.setBorder(new RoundRectBorder(Color.lightGray,30));
roundRectPanel.setBackground(Color.WHITE);
panel2.add(roundRectPanel, BorderLayout.CENTER);
roundRectPanel.setLayout(new BorderLayout(0, 0));

xx.add(panel)
//==============
roundRectPanel.setBorder(new RoundRectBorder(Color.lightGray,30));

分享到:
评论

相关推荐

    Jlabel实现内容自动换行简单实例

    JLabel实现内容自动换行简单实例 JLabel是一个Java Swing中的组件,用于显示文本或图像。在实际开发中,我们经常需要在JLabel中显示较长的文本,然而JLabel默认情况下不会自动换行,这将导致文本溢出或难以阅读。...

    java JLabel组件的资源

    Java JLabel组件是Java Swing库中的一个核心组件,用于在用户界面上显示文本、图像或两者的组合。在GUI应用程序中,JLabel常被用来呈现提示信息、图标或者简单的静态数据。下面将详细介绍JLabel组件的使用及其相关...

    java JLabel实例

    JLabel可以添加到容器(如JFrame或JPanel)中,以便在窗口上显示: ```java JFrame frame = new JFrame("JLabel演示"); frame.getContentPane().add(label); frame.setSize(300, 200); frame.setVisible(true); ```...

    用JLabel剪接图片

    `JLabel`是其中的一个核心组件,通常用于显示文本、图像或者组合两者。本篇文章将深入探讨如何利用`JLabel`来实现图片的剪接操作,这对于创建复杂的GUI布局或设计具有重要意义。 1. **JLabel的基本介绍** `JLabel`...

    JavaSE JLabel超链接例子

    在JavaSE中,如果我们想要在JLabel中实现超链接效果,即点击文本后能够触发特定的动作,如打开一个网页,可以通过自定义组件或扩展JLabel来实现。 首先,我们来看`JLabelLink.java`这个文件,这很可能是自定义的一...

    java Icon应用到JLabel中

    在Java GUI编程中,`JLabel` 是一个用于显示文本、图像或两者组合的重要组件。在许多情况下,我们可能希望在界面中展示图标(Icon),这时`Icon`接口就派上了用场。`Icon`是Java Swing提供的一种图形对象,可以被`...

    Java编程swing组件JLabel详解以及使用示例

    Java 编程 Swing 组件 JLabel 详解及使用示例 Java 编程中,Swing 组件 JLabel 是一个基本的组件,用于显示文本、图像或同时显示二者。JLabel 对象可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处...

    飞鸽传书源代码

    JPanel panel_1 = new JPanel(new GridLayout(3,1)); JPanel panel = new JPanel(new GridLayout(2,1)); JLabel label_2 = new JLabel(); JLabel label_1 = new JLabel("请选择要保存到的目录:"); JPanel panel...

    动态提取系统当前时间并放到JLabel上

    本文将详细介绍如何通过Java代码实现在程序运行时动态提取系统当前时间,并将其显示在一个`JLabel`组件上,以实现时间的实时更新。 ### 一、基础知识介绍 #### 1.1 `JLabel` 组件简介 `JLabel` 是Swing中的一种轻...

    java编写的日历

    ublic class JCalendar extends JPanel{ //动态表示年月日 private int year=0; private int month=0; private int day=0; private int first = 0; //用于标志日历面板是否显示 private static boolean isShow = ...

    Java用JLabel调用显示windows字体美化窗口.rar

    Java调用windows字体美化窗口,显示多种字体,用JLabel实现,可以很方便 的调用宋体、黑体、Sanf等多种系统字体,轻松改变窗口界面中的字体风格,从而起到美化窗口界面的作用。代码是这样的:  //字体数组:  ...

    Java之JLabel带图片

    `JLabel`是Java Swing库中的一个核心组件,它用于显示文本、图标或者同时显示两者。在Java项目开发中,`JLabel`常常被用来增强界面的可读性和美观性。本篇文章将深入探讨如何在Java中使用`JLabel`来展示图片,并结合...

    Java Swing JPanel面板的使用方法

    - 边框:可以使用 `Border` 接口的实现类来为 `JPanel` 添加边框,增强视觉效果。 - 背景色:`JPanel` 的背景色可以通过 `setBackground(Color color)` 方法改变。 - 绘制:重写 `paintComponent(Graphics g)` ...

    我想在jlabel上实现Key Listener

    在实际开发中,你可能还需要关注焦点管理,确保用户能够正确地将焦点切换到`JLabel`或`JPanel`。同时,`Key Bindings`是另一种处理键盘事件的方式,它允许更灵活的键映射,并且在某些情况下可能是更好的选择。如果你...

    Java JLabel显示多种字体样式.rar

    Java JLabel显示多种字体样式,其实是向大家演示如何在Java的窗口中实现自定义字体,示例中使用了多种字体样式,定义了不同的字体模式,比如字体名称、粗细、是否斜体等,这些都是Java的基础知识范畴吧。

    Java学生管理系统

    jLabel2.setBounds(new Rectangle(214, 8, 63, 28)); jLabel2.setText("选择班号"); jLabel2.setFont(new java.awt.Font("Dialog", 0, 15)); jLabel3.setBounds(new Rectangle(7, 54, 67, 28)); jLabel3....

    JAVA GUI自定义JPanel画板背景

    在Java GUI编程中,自定义JPanel画板背景是一个常见的需求,这允许开发者创建更加美观且功能丰富的用户界面。本文将详细介绍如何实现这一功能,并提供一个具体的实例。 首先,了解GUI(图形用户界面)的基本概念。...

    java 手机小游戏

    JPanel p1=new JPanel(); JPanel p2=new JPanel(); JLabel lab1=new JLabel("黑色"); JLabel lab2=new JLabel("黄色"); JTextField txt1 =new JTextField(); JTextField txt2 =new JTextField(); JButton btn...

    无聊的时候用Java万年历

    JPanel p = new JPanel(); JLabel JL_1 = new JLabel(); JLabel JL_1_1 = new JLabel(); JLabel JL_2 = new JLabel(); JLabel JL_2_1 = new JLabel("年"); JLabel JL_2_2 = new JLabel("月"); JLabel ...

    java万年历代码示例

    package clock; /** *//** * MainFrame.java ... JPanel panel2 = new JPanel(new GridLayout(7, 7)); JPanel panel3 = new JPanel(); JLabel[] label = new JLabel[49]; JLabel y_label = new JLabel("年份");

Global site tag (gtag.js) - Google Analytics