我们可以在 Java 中创建嵌套的 TitiledBorder 吗?
是的,我们可以创建嵌套的 TitledBorder。我们首先创建一个组件,我们为其设置边框 -
JLabel label = new JLabel();
现在,我们将创建第 1 个边框 -
TitledBorder border = BorderFactory.createTitledBorder("Top Border"); border.setTitlePosition(TitledBorder.TOP);
下面是我们创建边框 2 的方式。我们在此处设置第 1 个边框 -
TitledBorder border2 = new TitledBorder(border, "Bottom CENTER Border",TitledBorder.CENTER,TitledBorder.BOTTOM);
现在,为标签组件设置它 -
label.setBorder(border2);
以下是使用 Java 创建嵌套 TitledBorder 的示例 -
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.JLabel; import javax.swing.border.TitledBorder; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TitledBorder border = BorderFactory.createTitledBorder("Top Border"); border.setTitlePosition(TitledBorder.TOP); TitledBorder border2 = new TitledBorder(border, "Bottom CENTER Border", TitledBorder.CENTER, TitledBorder.BOTTOM); JLabel label = new JLabel(); label.setBorder(border2); Container contentPane = frame.getContentPane(); contentPane.add(label, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
输出
广告