首页 > 解决方案 > 如何更改 GUI 中标题边框的颜色?

问题描述

我已经看过其他关于如何做到这一点的类似帖子,但我一个都不明白。他们中的大多数使用这个名为“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);
}

标签: javaswinguser-interfaceawt

解决方案


question.setBorder(BorderFactory.createTitledBorder("Ask a question"));

你对那个声明有什么不明白的地方?你读过 API 吗?

返回该类的BorderFactory一个实例TitledBorder,因此您将它分配给一个变量,然后您可以调用 TitledBorder 类中的任何方法。

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

推荐阅读