如何在 Java 中向现有的线框边框添加标题?
这里,我们将学习如何向现有的线框边框添加标题。假设我们有一个标签并且边框将被设置为 -
LineBorder linedBorder = new LineBorder(Color.blue); TitledBorder titledBorder = BorderFactory.createTitledBorder(linedBorder, "Demo Title"); JLabel label = new JLabel(); label.setBorder(titledBorder);
以下是一个用于向现有的线框边框添加标题的示例 -
示例
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.LineBorder; 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); LineBorder linedBorder = new LineBorder(Color.blue); TitledBorder titledBorder = BorderFactory.createTitledBorder(linedBorder, "Demo Title"); JLabel label = new JLabel(); label.setBorder(titledBorder); Container contentPane = frame.getContentPane(); contentPane.add(label, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
输出
广告