如何在 Java 中为组件创建一个复合边框?


复合边框由两个或两个以上边框组成,即边框周围的边框。我们可以使用 createCompoundBorder() 方法在 Java 中为其创建边框。

假设以下是我们组件 −

JLabel label;
label = new JLabel("This has compound border (border around a border)!");

现在,设置复合边框 −

label.setBorder(BorderFactory.createCompoundBorder(BorderFactory
   .createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder()));

以下是为组件创建复合边框的示例 −

示例

package my;
import javax.swing.BorderFactory;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SwingDemo {
   public static void main(String[] args) throws Exception {
      JFrame frame = new JFrame("Demo");
      JLabel label;
      label = new JLabel("This has compound border (border around a border)!");
      label.setFont(new Font("Arial", Font.BOLD, 18));
      label.setVerticalAlignment(JLabel.CENTER);
      label.setBorder(BorderFactory.createCompoundBorder(BorderFactory
      .createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder()));
      frame.add(label); frame.setSize(550,350); frame.setVisible(true);
   }
}

输出

更新于: 30-7-2019

484 次浏览

开启您的事业

完成课程以获得认证

开始
广告