如何将 BorderFactory 类中的 EtchedBorder 设置为 Java 中的组件?


从 BorderFactory 类设置 EtchedBorder −

EtchedBorder etchedBorder = (EtchedBorder)BorderFactory.createEtchedBorder();

现在,为组件设置 -

JButton button = new JButton("Etched Border");
button.setBorder(etchedBorder);

以下是一个示例,从 BorderFactory 类中设置组件的 EtchedBorder -

示例

package my;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);
      EtchedBorder etchedBorder = (EtchedBorder)BorderFactory.createEtchedBorder();
      JButton raisedButton = new JButton("Raised Border");
      raisedButton.setBorder(raisedBorder);
      JButton button = new JButton("Etched Border");
      button.setBorder(etchedBorder);
      Container contentPane = frame.getContentPane();
      contentPane.add(raisedButton,BorderLayout.NORTH);
      contentPane.add(button,BorderLayout.EAST);
      frame.setSize(600, 300);
      frame.setVisible(true);
   }
}

输出


更新于: 2019 年 7 月 30 日

169 次浏览

开启你的职业

完成课程以获得认证

入门
广告