Java如何为边框添加标题?
本示例说明如何创建带有标题的边框。有一个特殊的边界类TitledBorder可以做到这一点。我们可以定义标题的对齐方式,左对齐,居中对齐或右对齐。为此,我们调用该setTitleJustification()方法。我们还可以设置其他属性,例如字体或标题位置。
package org.nhooo.example.swing;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
public class TitledBorderExample extends JFrame {
public TitledBorderExample() {
initializeUI();
}
private void initializeUI() {
setSize(300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
TitledBorder border = new TitledBorder(" Form Data ");
border.setTitleJustification(TitledBorder.LEFT);
border.setTitlePosition(TitledBorder.TOP);
JPanel panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(1, 2));
JLabel usernameLabel = new JLabel("Username: ");
JTextField usernameField = new JTextField();
panel.add(usernameLabel);
panel.add(usernameField);
setContentPane(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TitledBorderExample().setVisible(true);
}
});
}
}
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便