/*
setBorder(new TitledBorder(null, "标题", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.BLUE));
TitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont, Color titleColor)
用指定的边框、标题、标题对齐方式、标题位置、标题字体和标题颜色创建 TitledBorder 实例。
*/
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class ColorTest extends JFrame {
private static final long serialVersionUID = 1L;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
ColorTest frame = new ColorTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public ColorTest() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "标题", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.BLUE));
panel.setLayout(null);
panel.setBounds(103, 45, 274, 174);
getContentPane().add(panel);
final JButton button = new JButton();
button.setBounds(84, 74, 106, 28);
button.setText("New JButton");
panel.add(button);
//
}
}
设置TitledBorder.titleColor即可;
类的文档的相关介绍如下:
If the border, font, or color property values are not
specified in the constuctor or by invoking the appropriate
set methods, the property values will be defined by the current
look and feel, using the following property names in the
Defaults Table:
"TitledBorder.border"
"TitledBorder.font"
"TitledBorder.titleColor"
UIDefaults defaults=UIManager.getLookAndFeelDefaults();
String key = "TitledBorder.font";
Font font = defaults.getFont(key).deriveFont(18.0f);
UIManager.put(key,new javax.swing.plaf.FontUIResource(font));