Swing添加边框
2021-10-24 本文已影响0人
开心的小哈
package com.main;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class Dtext {
JFrame jFrame= new JFrame("测试边框");
public void init(){
//组装试图
jFrame.setLayout(new GridLayout(2,4));
//网表格中添加不同的jpanel组件并设置边框和内容
//创建bevelBorder
Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.GREEN, Color.BLACK, Color.GREEN);
jFrame.add(getJPanelWithBord(bevelBorder,"BevelBorder"));
//创建LinBorder
Border lineBorder = BorderFactory.createLineBorder(Color.ORANGE, 10);
jFrame.add(getJPanelWithBord(lineBorder,"LineBorder"));
//创建EmptyBorder
Border emptyBorder = BorderFactory.createEmptyBorder(10, 5, 20, 10);
jFrame.add(getJPanelWithBord(emptyBorder,"emptyBorder"));
//创建EchteBorder
Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,Color.red,Color.GREEN);
jFrame.add(getJPanelWithBord(etchedBorder,"etchedBorder"));
//titleborder
TitledBorder titledBorder = new TitledBorder(new LineBorder(Color.ORANGE, 10),"测试标题",TitledBorder.LEFT,TitledBorder.BOTTOM,new Font("StSong",Font.BOLD,18),Color.BLUE);
jFrame.add(getJPanelWithBord(titledBorder,"titledBorder"));
//chuangjain MtteBorder
MatteBorder matteBorder = new MatteBorder(10, 5, 20, 10, Color.GREEN);
jFrame.add(getJPanelWithBord(matteBorder,"matteBorder"));
//CompondBorder()
CompoundBorder compoundBorder = new CompoundBorder(new LineBorder(Color.RED, 10),titledBorder);
jFrame.add(getJPanelWithBord(compoundBorder,"compoundBorder"));
//设置窗口大小
jFrame.pack();
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public JPanel getJPanelWithBord(Border border,String ss){
JPanel jPanel = new JPanel();
jPanel.add(new JLabel(ss));
jPanel.setBorder(border);
return jPanel;
}
public static void main(String[] args) {
new Dtext().init();
}
}