1

My program is a simple game where you are trying to figure out a random 5 letter word by guessing words and being told how many letters are the same (spin off of Jotto). Previous guesses and results are displayed in a JScrollPane. I am trying to get them to line up in columns. When the guess number gets into double digits everything shifts a character.

enter image description here

I am using a mono-space font and String.format() to try and pad the right number spaces between columns but it seems to be making the spaces smaller than other characters still. When printed to the command line it works fine and there the field width is actually 17 characters long instead of 10 or however many are in the picture.

scroll.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
....
guessList.add(0, String.format("%-17s" + result, numGuesses + ":" ));

Here guessList is a JList that is in the JScrollPane. I don't think that it could be causing the problem, but it is part of it.

I am considering switching to a JTable, but I think this should work and if not I would like to know why.

4

2 回答 2

1

在没有看到太多代码的情况下,我假设您正在(在您的代码中JScrollPane命名scroll)上设置字体,这不会渗透到您的JList(在您的代码中命名guessList)。尝试改用这个:

guessList.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
于 2012-08-02T21:16:53.593 回答
1

使用带有 HTML 的 JTextPane:

JTextPane jtp = new JTextPane();
jtp.setContentType("text/html");

并在每次插入中,再次绘制整个表格:

jtp.setText("");
jtp.append("<html><table>");
jtp.append("<tr><td>header1</td><td>header2</td><td>header3</td><td>header4</td></tr>");
for(String line : lines)
    jtp.append("<tr><td> field1 </td><td> field2 </td><td> field3 </td><td> field4 </td></tr>");
jtp.append("</table></html>");
于 2012-08-02T21:27:25.640 回答