如何更改 GUI 中标题边框的颜色?

How can I change the color of the titled border in my GUI?

我已经看过关于如何执行此操作的其他类似帖子,但我不明白其中任何一个。他们中的大多数人使用这个object 称为“TitledBorder”,但我只是使用方法“.setBorder()”。我想更改边框的颜色和标题的颜色。请帮帮我,谢谢!

private void layoutView()
{

//The JPanel that holds the JTextField. This is the first
//JPanel that I want to change the color of the titled border
JPanel question = new JPanel();
question.add(this.question);
question.setBorder(BorderFactory.createTitledBorder("Ask a question"));

//The JPanel that holds the JLabel. 
//This is the second JPanel that I want to change the colour of.
JPanel questionAnswerPanel = new JPanel();
questionAnswerPanel.add (this.questionAnswer);
questionAnswerPanel.setBorder(BorderFactory.createTitledBorder("Prediction"));

//The JPanel that holds the question JPanel so it can be centered
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
center.add(question, BorderLayout.CENTER);

//The complete layout
this.setLayout(new BorderLayout());
this.add(center, BorderLayout.CENTER);
this.add(questionAnswerPanel, BorderLayout.SOUTH);
}
question.setBorder(BorderFactory.createTitledBorder("Ask a question"));

你对这句话有什么不理解的?你读过 API 了吗?

BorderFactory returns TitledBorder class 的一个实例,所以你把它赋值给一个变量,然后你就可以从 TitledBorder class.

//question.setBorder(BorderFactory.createTitledBorder("Ask a question"));
TitledBorder border = BorderFactory.createTitledBorder("Ask a question");
border.setTitleColor( Color.RED );
question.setBorder( border );