JPanel的边框当然还有方法重载,可以设颜色。Border picBorder = BorderFactory.createTitledBorder("图片展示"); picPanel.setBorder(picBorder);
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); // } }