Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Colors 如何在同一标签中设置不同的颜色?_Colors_Jframe_Label_Netbeans 7_Foreground - Fatal编程技术网

Colors 如何在同一标签中设置不同的颜色?

Colors 如何在同一标签中设置不同的颜色?,colors,jframe,label,netbeans-7,foreground,Colors,Jframe,Label,Netbeans 7,Foreground,我必须在标签中设置不同的消息,但我无法更改不同消息的颜色。如何在同一标签上设置不同的颜色?我不确定如何回答您的问题,但我有一个想法,您可能会喜欢。。。 您可以尝试制作一个自定义标签(使用paintComponent()或paint()方法。我建议使用paintComponent() 例如: (如果要使用paintComponent(),需要将其放入JPanel类中, 但是,如果选择使用paint()方法,则可以将其保留在JFrame类中。如果选择使用paint()方法,则需要去掉super.p

我必须在标签中设置不同的消息,但我无法更改不同消息的颜色。如何在同一标签上设置不同的颜色?

我不确定如何回答您的问题,但我有一个想法,您可能会喜欢。。。 您可以尝试制作一个自定义标签(使用paintComponent()或paint()方法。我建议使用paintComponent()

例如:


(如果要使用paintComponent(),需要将其放入JPanel类中, 但是,如果选择使用paint()方法,则可以将其保留在JFrame类中。如果选择使用paint()方法,则需要去掉super.paintComponent(g)行。)

现在,假设你知道如何使用这些。你可以继续使用它,让颜色和文字成为你想要的

如果你不懂什么,请尽管问


如果有帮助,请竖起大拇指

当然,这样做需要花费更多的精力和时间。以获得“自定义标签”的外观。
public void paintComponent(Graphics g){
      super.paintComponent(g);

      //first color and first half
      g.setColor(Color.RED);
      g.fillRect(10, 10, 40, 40);

      //second color and second half
      g.setColor(Color.GREEN);
      g.fillRect(50, 10, 40, 40);

      //Obviously sets the text, its color, etc.
      g.setFont(new Font("Arial", Font.BOLD, 20));
      g.setColor(Color.BLACK);
      g.drawString("TEXT", 25, 36);

}