【Java】边框总结

Table of Contents

  • 1 例子代码与结果
  • 2 javax.swing.border
  • 3 BorderFactory
  • 4 LineBorder
  • 5 MatteBorder
  • 6 TitleBorder
  • 7 BevelBorder
  • 8 SoftBevelBorder
  • 9 EtchedBorder
  • 10 CompoundBorder

1 例子代码与结果

 

package xjtu.vf.swing;

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

import javax.swing.*;
import javax.swing.border.*;

public class Borders extends JFrame {
    static JPanel showBorder(Border b) {
        JPanel jp = new JPanel();
        jp.setLayout(new BorderLayout());
        String mm = b.getClass().toString();
        mm = mm.substring(mm.lastIndexOf('.') + 1);
        jp.add(new JLabel(mm, JLabel.CENTER), BorderLayout.CENTER);
        jp.setBorder(b);
        return jp;
    }

    public Borders() {
        setLayout(new GridLayout(2, 4));
        add(showBorder(new TitledBorder("Title")));
        add(showBorder(new EtchedBorder()));
        add(showBorder(new LineBorder(Color.blue)));
        add(showBorder(new MatteBorder(5, 5, 30, 30, Color.GREEN)));
        add(showBorder(new BevelBorder(BevelBorder.RAISED)));
        add(showBorder(new SoftBevelBorder(BevelBorder.LOWERED)));
        add(showBorder(new CompoundBorder(new EtchedBorder(), new LineBorder(
                Color.RED))));
    }

    public static void main(String[] args) {
        SwingConsole.run(new Borders(), 500, 300);
    }
}

【Java】边框总结_第1张图片

2 javax.swing.border

AbstractBorder所有边框类的父类

 

3 BorderFactory

使用工厂设计模式快速设计边框,隐藏创建细节。

4 LineBorder

LineBorder是围绕组件周围用户义定宽度的单色行边框。他可以具有方角或是圆角。
LineBorder有三个构造函数,两个工厂方法以及两个BorderFactory工厂方法

 

public LineBorder(Color color)
Border lineBorder = new LineBorder (Color.RED);
 
public LineBorder(Color color, int thickness)
Border lineBorder = new LineBorder (Color.RED, 5);
 
public LineBorder (Color color, int thickness, boolean roundedCorners)
Border lineBorder = new LineBorder (Color.RED, 5, true);
 
public static Border createBlackLineBorder()
Border blackLine = LineBorder.createBlackLineBorder();
 
public static Border createGrayLineBorder()
Border grayLine = LineBorder.createGrayLineBorder();
 
public static Border createLineBorder(Color color)
Border lineBorder = BorderFactory.createLineBorder(Color.RED);
 
public static Border createLineBorder(Color color, int thickness)
Border lineBorder = BorderFactory.createLineBorder(Color.RED, 5);

5 MatteBorder

如果我们希望修改不同边的粗细,我们需要使用MatteBorder,

 

MatteBorder(Icon tileIcon)      
Creates a matte border with the specified tile icon.
MatteBorder(Insets borderInsets, Color matteColor)
Creates a matte border with the specified insets and color.
MatteBorder(Insets borderInsets, Icon tileIcon)
Creates a matte border with the specified insets and tile icon.
MatteBorder(int top, int left, int bottom, int right, Color matteColor)
Creates a matte border with the specified insets and color.
MatteBorder(int top, int left, int bottom, int right, Icon tileIcon)
Creates a matte border with the specified insets and tile icon.

6 TitleBorder

 

7 BevelBorder

BevelBorder以三维外观绘制边框,其可以表现为升起或是降低。当边框升起时,在边框的底部与右边会出现阴影效果。当降低时,阴影的位置会相反。
BevelBorder有三个构造函数以及一个工厂方法,同时还有BorderFactory创建BevelBorder对象的五个工厂方法:

 

public BevelBorder(int bevelType)
Border bevelBorder = new BevelBorder(BevelBorder.RAISED);
 
public BevelBorder(int bevelType, Color highlight, Color shadow)
Border bevelBorder = new BevelBorder(BevelBorder.RAISED, Color.PINK, Color.RED);

public BevelBorder(int bevelType, Color highlightOuter, Color highlightInner,
  Color shadowOuter, Color shadowInner)
Border bevelBorder = new BevelBorder(BevelBorder.RAISED, Color.PINK,
  Color.PINK.brighter(), Color.RED, Color.RED.darker());

public static Border createBevelBorder(int bevelType)
Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED);
 
public static Border createLoweredBevelBorder()
Border bevelBorder = BorderFactory.createLoweredBevelBorder();
 
public static Border createRaisedBevelBorder()
Border bevelBorder = BorderFactory.createRaisedBevelBorder();
 
public static Border createBevelBorder(int bevelType, Color highlight, Color shadow)
Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED,
  Color.PINK, Color.RED);
 
public static Border createBevelBorder(int bevelType, Color highlightOuter,
  Color highlightInner, Color shadowOuter, Color shadowInner)
Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED,
  Color.PINK, Color.PINK.brighter(), Color.RED, Color.RED.darker());

8 SoftBevelBorder

oftBevelBorder是BevelBorder的近亲。这个组件会包围四角,所以他们的边并不尖利。
而他使用下边与右边的相应外边颜色只绘制一条线。
SoftBevelBorder有三个构造函数:

 

public SoftBevelBorder(int bevelType)
Border softBevelBorder = new SoftBevelBorder(SoftBevelBorder.RAISED);

public SoftBevelBorder(int bevelType, Color highlight, Color shadow)
Border softBevelBorder = new SoftBevelBorder(SoftBevelBorder.RAISED, Color.RED,
  Color.PINK);

SoftBevelBorder(int bevelType, Color highlightOuterColor, Color highlightInnerColor, Color shadowOuterColor, Color shadowInnerColor)

9 EtchedBorder

建立一个四周有凹痕的边界,也可以指定突边与阴影的颜色.

10 CompoundBorder

建立一个复合边界,并可以指定它的内外边界

 

Author: visaya fan <visayafan[AT]gmail.com>

Date: 2011-11-20 22:02:11

HTML generated by org-mode 6.33x in emacs 23


作者:visayafan
出处:http://www.cnblogs.com/visayafan/
本博客文章欢迎转载,转载时请注意标明出处。

你可能感兴趣的:(java,border,BordeLayout)