javax.swing.border设置组件边框

丌官向荣
2023-12-01

javax.swing.border;

// Create a border
    EmptyBorder emptyBorder = (EmptyBorder)BorderFactory.createEmptyBorder();// 创建空边框
   
    LineBorder lineBorder = (LineBorder)BorderFactory.createLineBorder(Color.black);// 创建线形边框
   
    EtchedBorder etchedBorder = (EtchedBorder)BorderFactory.createEtchedBorder();// 创建蚀刻式边框
   
    BevelBorder raisedBevelBorder = (BevelBorder)BorderFactory.createRaisedBevelBorder();// 创建浮雕式边框
   
    BevelBorder loweredBevelBorder = (BevelBorder)BorderFactory.createLoweredBevelBorder();// 创建下沉边框
   
    ImageIcon icon = new ImageIcon("image.gif");
    MatteBorder matteBorder = (MatteBorder)BorderFactory.createMatteBorder(-1, -1, -1, -1, icon);// 创建不光滑的边框
   
   
    // Set the border
    component.setBorder(emptyBorder);// 设置组件的边框


向边框添加标题(Adding a Title to a Border)
    // 使用带标题缺省边框(Use default border)
    TitledBorder titledBorder = BorderFactory.createTitledBorder("Title");
   
    // 在已存在的边框基础上创建标题Create the title around existing border
    titledBorder = BorderFactory.createTitledBorder(border, "Title");
   
    // 改变标题所在横向位置(Also available: DEFAULT_JUSTIFICATION, LEFT, RIGHT)
    titledBorder.setTitleJustification(TitledBorder.CENTER);
   
    // 改变标题纵向位置
    // Also available: DEFAULT_POSITION, ABOVE_TOP, TOP,
    // ABOVE_BOTTOM, BOTTOM, BELOW_BOTTOM
    titledBorder.setTitlePosition(TitledBorder.BELOW_TOP);
   
    component.setBorder(titledBorder);// 设置组件的边框


创建复合式边框(Creating a Compound Border)
    // border1 is around border2
    Border newBorder = BorderFactory.createCompoundBorder(border1, border2);
    component.setBorder(newBorder);


      BorderFactory.createCompoundBorder(
    BorderFactory.createTitledBorder(
     BorderFactory.createLineBorder(Color.black),
     " Header Part Here "),
    BorderFactory.createEmptyBorder(5, 5, 5, 5)));

 类似资料: