英文:
Creating a multi-colored JLabel in java with the help of html
问题
我试图将JLabel的一部分颜色更改为特定颜色。我试图使"Blue Wins: 0"中的"Blue Wins"变为蓝色,我知道我应该使用HTML函数,函数应该类似于这样的新的JLabel("<html>Blue Wins: 0 <font color='blue'>blue</font></html>"));
。但我不确定它是否应该是这个样子,更重要的是我应该将这个函数放在代码的哪个部分(GUI部分而不是逻辑部分)。
这两段代码与JLabel和颜色、字体的GUI部分相关联。
JLabel infoLabel;
JLabel XWinCounter = new JLabel("X Wins: 0");
JLabel OWinCounter = new JLabel("O Wins: 0");
GameLogic gameLogic;
infoLabel.setHorizontalAlignment(JLabel.CENTER);
panel.setBorder(new LineBorder(Color.black,1));
XWinCounter.setForeground(Color.BLUE);
XWinCounter.setOpaque(true);
infoPanel.add(XWinCounter);
infoPanel.add(OWinCounter);
infoPanel.add(infoLabel);
getContentPane().add(infoPanel, BorderLayout.NORTH);
getContentPane().add(panel,BorderLayout.CENTER);
这是当前带有JLabel的GUI外观,整个前景色都是蓝色的,我只想让"Blue Wins:"部分变为蓝色,":"和"0"应保持当前的颜色。
英文:
I am trying to change the color of a part of my JLabel to a specific color.I am trying to make it so that "Blue Wins" in Blue Wins: 0 becomes the color blue, I know that I should use html function and that the function should look something like this new JLabel("<html>Blue Wins: 0 <font color='Blue Wins'>blue</font></html>"));
. But I am not sure if that's how it should look and more importantly where I should place this function in my code ( the GUI section and not the logic section)
These Two snippets of code are the ones connected to the Jlabel and the Gui part of the colors and fonts of it.
JLabel infoLabel;
JLabel XWinCounter = new JLabel("X Wins: 0");
JLabel OWinCounter = new JLabel("O Wins: 0");
GameLogic gameLogic;
infoLabel.setHorizontalAlignment(JLabel.CENTER);
panel.setBorder(new LineBorder(Color.black,1));
XWinCounter.setForeground(Color.BLUE);
XWinCounter.setOpaque(true);
infoPanel.add(XWinCounter);
infoPanel.add(OWinCounter);
infoPanel.add(infoLabel);
getContentPane().add(infoPanel, BorderLayout.NORTH);
getContentPane().add(panel,BorderLayout.CENTER);
This is how the GUI with the Jlabel currently looks, The whole foreground color is blue, I only want the Blue Wins: Part to be blue the : and 0 should remain the same color it is currently.
答案1
得分: 0
你需要在JLabel的构造函数中添加HTML,就像这样:
public class Answers {
public static JFrame f = new JFrame("Colors");
public static void main(String[] args) {
JLabel XWinCounter = new JLabel("<html>X Wins: <font color='blue'>0</font></html>");
JLabel OWinCounter = new JLabel("<html>Y Wins: <font color='red'>0</font></html>");
f.add(OWinCounter, BorderLayout.NORTH);
f.add(XWinCounter, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
英文:
Your'e need to add the HTML on the construcror of the JLabel, like this:
public class Answers {
public static JFrame f = new JFrame("Colors");
public static void main(String[] args) {
JLabel XWinCounter = new JLabel("<html>X Wins: <font color='blue'>0</font></html>");
JLabel OWinCounter = new JLabel("<html>Y Wins: <font color='red'>0</font></html>");
f.add(OWinCounter, BorderLayout.NORTH);
f.add(XWinCounter, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论