java - 如何更改 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);
}
解决方案
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 );
推荐阅读
- java - Runtime.exec() 是否仅适用于某些命令?
- javascript - Handlebar 在控制台中返回空数据
- javascript - 将事件处理程序绑定到 React ref
- c# - 如何使用任何 Windows 自动化向 Outlook 文本框添加一些文本?
- excel - 宏在无限循环中运行
- laravel-5 - 预期状态码 200,但收到 302。未能断言 false 为真。在 laravel 5.4 中使用 phpunit 测试
- azure - Azure 异常检测 API 是否适用于视觉数据
- javascript - 如何使用 res.clearCookie() 清除 expressjs 中特定域的所有 cookie
- python - 变量的值(不是元组)在 Tkinter Python 中没有变化
- javascript - Javascript/node:使用管道方法从读取流到http响应对象的问题