引言:GUI在显示基本功能情况后,要将图形界面设计得尽量好看。这就需要对界面的边框进行一定的设计
第一、边框的分类:
边框分为空白边框(EmptyBorder)、颜色线边框(LineBorder)、蚀刻边框(EtchedBorder)、
斜面边凸(RaisedBevelBorder)、斜面边框凹(loweredSoftBevelBorder)、标题边框(TitledBorder)、
花边边框(MatteBorder)、复合边框(CompoundBorder)
第二、实例代码:
package swingui_border;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("swing的b边框演示");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0,2,5,10));
//绘制一个空边框,但是这里指定了线的宽度
panel.setBorder(BorderFactory.createEmptyBorder(5,
5,
5,
5));
//绘制一个具有颜色的线边框
JPanel p1 = new JPanel();
p1.setBorder(BorderFactory.createLineBorder(Color.red,3));
p1.add(new JLabel("颜色线边框"));
panel.add(p1);
//绘制一个蚀刻边框,背景用于高亮和阴影显示
JPanel p2 = new JPanel();
p2.setBorder(BorderFactory.createEtchedBorder());
p2.add(new JLabel("蚀刻边框"));
panel.add(p2);
//绘制一个斜面边框(凸) 当前背景颜色中较亮部分高亮显示,较暗部分用于阴影显示
JPanel p3 = new JPanel();
p3.setBorder(BorderFactory.createRaisedBevelBorder());
p3.add(new JLabel("斜面边框(凸)"));
panel .add(p3);
//绘制一个斜面边框(凹) 与上同
JPanel p4 = new JPanel();
p4.setBorder(BorderFactory.createLoweredSoftBevelBorder());
p4.add(new JLabel("斜面边框(凹)"));
panel.add(p4);
//标题边框 向现有边框添加一个标题
JPanel p5 = new JPanel();
p5.setBorder(BorderFactory.createTitledBorder("标题"));
p5.add(new JLabel("标题边框"));
panel .add(p5);
//标题边框可以指定标题的位置
JPanel p6 = new JPanel();
TitledBorder tb = BorderFactory.createTitledBorder("标题");
tb.setTitleJustification(TitledBorder.RIGHT);
p6.setBorder(tb);
p6.add(new JLabel("标题边框(右边)"));
panel .add(p6);
//花色边框
JPanel p7 = new JPanel();
p7.setBorder(BorderFactory.createMatteBorder(1, 5, 1,1,Color.blue));
p7.add(new JLabel("花色边框"));
panel.add(p7);
//组合边框
JPanel p8 = new JPanel();
Border b1 = BorderFactory.createLineBorder(Color.RED,2);
Border b2 = BorderFactory.createEtchedBorder();
p8.setBorder(BorderFactory.createCompoundBorder(b1, b2));
p8.add(new JLabel("复合边框"));
panel.add(p8);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(300,300));
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
第三、使用解析
边框理论上能够所有的JComponet子类。