-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBorderTest.java
55 lines (53 loc) · 2.47 KB
/
BorderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;
public class BorderTest {
private JFrame jf=new JFrame("测试边框");
public void init(){
jf.setLayout(new GridLayout(2,4));
//使用静态工厂方法创建BevelBorder
Border bb=BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.RED,Color.GREEN,Color.BLUE,Color.GRAY);
jf.add(getPanelWithBorder(bb,"BevelBorder"));
//使用静态工厂方法创建LineBorder
Border lb=BorderFactory.createLineBorder(Color.ORANGE,10);
jf.add(getPanelWithBorder(lb,"LineBorder"));
//使用静态工厂方法创建EmptyBorder,EmptyBorder就是在组件四周留空
Border eb=BorderFactory.createEmptyBorder(20,5,10,30);
jf.add(getPanelWithBorder(eb,"EmptyBorder"));
//使用静态工厂方法 创建EtchedBorder
Border etb=BorderFactory.createEtchedBorder(EtchedBorder.RAISED,Color.RED,Color.GREEN);
jf.add(getPanelWithBorder(etb,"EtchedBorder"));
//直接创建TitleBorder,TitleBorder就是为原有的边框增加标题
TitledBorder tb=new TitledBorder(lb,"测试标题",TitledBorder.LEFT,TitledBorder.BOTTOM,new Font("StSong",Font.BOLD,18),Color.BLUE);
jf.add(getPanelWithBorder(tb,"TitledBorder"));
//直接创建MatteBorder,MatteBorder是EmptyBorder的子类,它可以指定留空区域的颜色或背景,此处是指定颜色
MatteBorder mb=new MatteBorder(20,5,10,30,Color.GREEN);
jf.add(getPanelWithBorder(mb,"MatteBorder"));
//直接创建CompoundBorder,CompoundBorder将两个边框组合成新边框
CompoundBorder cb=new CompoundBorder(new LineBorder(Color.RED,8),tb);
jf.add(getPanelWithBorder(cb,"CompoundBorder"));
jf.pack();
jf.setVisible(true);
}
public static void main(String args []){
new BorderTest().init();
}
public JPanel getPanelWithBorder(Border b,String BorderName){
JPanel p=new JPanel();
p.add(new JLabel(BorderName));
//为Panel组件设置边框
p.setBorder(b);
return p;
}
}