java 自定义边框_java – Swing自定义边框

您只需要扩展

AbstractBorder并覆盖其paintBorder()方法,这是一个相关的示例:

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.Line2D;

import java.awt.geom.Line2D.Double;

import javax.swing.*;

import javax.swing.border.AbstractBorder;

public class CustomMarginBorder

{

private JPanel contentPane;

private CustomBorder customBorder;

private void displayGUI()

{

JFrame frame = new JFrame("Custom Arrow Border Example");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

customBorder = new CustomBorder(Color.BLUE, 15);

contentPane = new JPanel();

contentPane.setBorder(customBorder);

frame.setContentPane(contentPane);

frame.setSize(300, 300);

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String[] args)

{

Runnable runnable = new Runnable()

{

@Override

public void run()

{

new CustomMarginBorder().displayGUI();

}

};

EventQueue.invokeLater(runnable);

}

}

class CustomBorder extends AbstractBorder

{

private Color borderColour;

private int gap;

public CustomBorder(Color colour, int g)

{

borderColour = colour;

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 = null;

if (g instanceof Graphics2D)

{

g2d = (Graphics2D) g;

g2d.setColor(borderColour);

//Left Border

g2d.draw(new Line2D.Double((double)x + 10, (double)y + 10

, (double)x + 10, (double)y + 20));

g2d.draw(new Line2D.Double( (double)x + 10, (double)y + 10

, (double)x + 20, (double)y + 10));

// Right Border

g2d.draw(new Line2D.Double( (double)width - 10, (double)y + 10

, (double)width - 10, (double)y + 20));

g2d.draw(new Line2D.Double( (double)width - 10, (double)y + 10

, (double)width - 20, (double)y + 10));

// Lower Left Border

g2d.draw(new Line2D.Double( (double)x + 10, (double)height - 10

, (double)x + 20, (double)height - 10));

g2d.draw(new Line2D.Double( (double)x + 10, (double)height - 10

, (double)x + 10, (double)height - 20));

// Lower Right Border

g2d.draw(new Line2D.Double( (double)width - 10, (double)height - 10

, (double)width - 20, (double)height - 10));

g2d.draw(new Line2D.Double( (double)width - 10, (double)height - 10

, (double)width - 10, (double)height - 20));

}

}

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

}

}

对于粗线边框使用此:

class CustomBorder extends AbstractBorder

{

private Color borderColour;

private int gap;

private double rectWidth;

private double rectHeight;

public CustomBorder(Color colour, int g, double w, double h)

{

borderColour = colour;

gap = g;

rectWidth = w;

rectHeight = h;

}

@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 = null;

if (g instanceof Graphics2D)

{

g2d = (Graphics2D) g;

g2d.setColor(borderColour);

//Left Border

g2d.fill(new Rectangle2D.Double(

(double)x + gap

, (double)y + gap

, rectWidth, rectHeight));

g2d.fill(new Rectangle2D.Double(

(double)x + gap

, (double)y + gap + rectHeight

, rectHeight, rectWidth));

// Right Border

g2d.fill(new Rectangle2D.Double(

(double)width - gap - rectWidth

, (double)y + gap

, rectWidth, rectHeight));

g2d.fill(new Rectangle2D.Double(

(double)width - gap - rectHeight

, (double)y + gap + rectHeight

, rectHeight, rectWidth));

// Lower Left Border

g2d.fill(new Rectangle2D.Double(

(double)x + gap

, (double)height - gap - rectWidth

, rectHeight, rectWidth));

g2d.fill(new Rectangle2D.Double(

(double)x + gap

, (double)height - gap

, rectWidth, rectHeight));

// Lower Right Border

g2d.fill(new Rectangle2D.Double(

(double)width - gap - rectHeight

, (double)height - gap - rectWidth

, rectHeight, rectWidth));

g2d.fill(new Rectangle2D.Double(

(double)width - gap - rectWidth

, (double)height - gap

, rectWidth, rectHeight));

}

}

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

}

}

输出:

边框细而粗

对于

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值