java的边框

发布时间 2023-10-28 20:24:12作者: 翎刿

1.关于边框

是能够在swing组件边缘周围渲染边框的对象的接口,边框就是对组件边界的装饰,可以为组件添加边框的色彩,也可以在边框上添加标题,让组件更加美观好看。

2.部分边框类

BevelBorder : 实现简单的两行斜角边框的类。

参数:
protected int	bevelType 斜面类型。 LOWERED :降低斜面类型。 RAISED:凸起斜面类型。
protected Color	highlightInner	用于斜角内部高光的颜色。
protected Color	highlightOuter	用于斜角外部高光的颜色。
protected Color	shadowInner	用于斜角内阴影的颜色。
protected Color	shadowOuter	用于斜面外阴影的颜色

LineBorder : 实现任意厚度和单色的线边框的类。

参数:
protected Color	lineColor	边框的颜色。
protected boolean	roundedCorners	边框是否有圆角。
protected int	thickness	边框的厚度。

EmptyBorder : 提供空白透明边框的类,占用空间但不绘制。

参数:
protected int	bottom	边界的底部。
protected int	left	边界的左边。
protected int	right	边界的右边。
protected int	top	边界的顶部。

TitledBorder . :一个实现任意边框的类,在指定位置和对齐中添加String标题。

参数:
protected Border	border	边界。
protected String	title	边框应显示的标题。
static int	BOTTOM	将标题置于边框底部的中间位置。
static int	LEFT	将标题文本放在边框线的左侧。
protected Color	titleColor	标题的颜色。
protected Font	titleFont	用于呈现标题的字体。
(还有很多参数,只列举了使用的部分)

MatteBorder : 提供纯色或平铺图标的类似哑光边框的类。

参数:
protected Color	color	为边框渲染的颜色。
protected Icon	tileIcon	用于平铺边框的图标。

CompoundBorder : 一个复合Border类,用于通过将内部Border对象嵌套在外部Border对象的insets中,将两个Border对象组合成单个边框。

参数:
protected Border	insideBorder	内部边界。
protected Border	outsideBorder	外边界。

3.测试代码

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

public class BorderTest {

    JFrame jf=new JFrame("测试边框");

    public void init(){
      //组装视图

        //JFrame的布局修改为GirdLayout
        jf.setLayout(new GridLayout(2,4));
        //往网络中填充不同的JPanel组件,并且设置边框和内容

        //创建BevelBorder           类型: RAISED 和 LOWERED一个是突起另一个是凹下去
        Border  bevelBorder=BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.RED,Color.GREEN,Color.BLUE,Color.gray);
        jf.add(getJPanelWithBorder(bevelBorder,"BevelBorder"));
        //创建LineBorder     只使用了颜色和厚度
      Border lineBorder= BorderFactory.createLineBorder(Color.ORANGE,13);
        jf.add(getJPanelWithBorder(lineBorder,"LineBorder"));
        //创建EmptyBorder
       Border emptyBorder=BorderFactory.createEmptyBorder(10,5,20,10);
        jf.add(getJPanelWithBorder(emptyBorder,"EmptyBorder"));
        //创建TitledBorder
        TitledBorder titledBorder=new TitledBorder(new LineBorder(Color.ORANGE,10),"测试标题",TitledBorder.LEFT,TitledBorder.BOTTOM,new Font("StSong",Font.BOLD,18),Color.RED);
        jf.add(getJPanelWithBorder(titledBorder,"TitledBorder"));
        //创建MatteBorder
        MatteBorder matteBorder=new MatteBorder(10,5,20,10,Color.GREEN);
        jf.add(getJPanelWithBorder(matteBorder,"MatteBorder"));
        //创建CompoundBorder
        CompoundBorder compoundBorder=new CompoundBorder(new LineBorder(Color.RED,10),titledBorder);
        jf.add(getJPanelWithBorder(compoundBorder,"CompoundBorder"));

        //设置窗口最佳大小,设置窗口可见,处理关闭操作
       jf.pack();
       jf.setVisible(true);
       jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

//因为都是创建边框,所以可以使用同一种方法,给panel添加边框在添加到窗口中
   public JPanel getJPanelWithBorder(Border border, String content){
        JPanel jPanel=new JPanel();
        jPanel.add(new JLabel(content));
        //设置边框
       jPanel.setBorder(border);
       return jPanel;
   }
    public static void main(String[] args) {
        new BorderTest().init();
    }

}