【问题标题】:How can I change the color of the titled border in my GUI?如何更改 GUI 中标题边框的颜色?
【发布时间】:2021-02-16 10:21:21
【问题描述】:

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

【问题讨论】:

    标签: java swing user-interface awt


    【解决方案1】:
    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 );
    

    【讨论】:

    • 哦,我没有阅读实际的 API,我唯一依赖的是我的老师给我的信息。我现在会尝试,如果它有效,请告诉您,谢谢。
    • 我试过了,它有效,谢谢。但是,无论如何我可以改变边框本身的颜色吗?
    • @Anonymous 我没有阅读实际的 API - 作为程序员,你需要做的第一件事就是学习阅读 API。 无论如何我可以更改边框本身的颜色吗? - 如果您在 API 中看不到支持该颜色的方法,那么如果没有自定义编码就无法完成。在这种情况下,您需要实现自己的自定义边框类并自己实现自定义绘画逻辑。可以随时下载JDK源码,尝试修改已有的TitledBorder绘画代码。
    猜你喜欢
    • 2015-04-13
    • 2021-11-30
    • 2016-12-19
    • 2010-09-09
    • 1970-01-01
    • 2020-01-12
    • 2013-02-18
    • 1970-01-01
    • 2017-08-17
    相关资源
    最近更新 更多