Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

How to add a java variable into HTML text in a JButton

Probably a stupid question but I can't figure it out for the life of me. I have this code :

JFrame frame = new JFrame("Game Window");
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
JPanel Panel = new JPanel();
frame.add(Panel);
Panel.add(new JLabel("<html>This is turn  </html>"));

However, it prints turnCounter in italics and not the value of the variable. I've also tried %d but not too sure of the syntax. It makes the whole GUI turn blank so I'm guessing something is wrong. Would really appreciate some help !

question from:https://stackoverflow.com/questions/65837694/how-to-add-a-java-variable-into-html-text-in-a-jbutton

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not tested:

turnCounterPanel.add(new JLabel( String.format( 
  "<html>This is turn %d turnCounter %d </html>", var, var ) ));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...