问题
Does anyone know why tab (\t) does not work with JOptionPane.showMessageDialog?
My code is as follows:
String addText = "NAME\t\tADDRESS\t\tTEL.No\tEMAIL\n";
for (int i = 0; i < addressBookSize; i++) {
addText = addText+entry[i].viewAllInfo();
}
System.out.print(addText);
JOptionPane.showMessageDialog(null, addText);
Are there other ways to align text in JOptionPane?
回答1:
Put your tabbed text into JTextArea
String addText = "NAME\t\tADDRESS\t\tTEL.No\tEMAIL\n";
for (int i = 0; i < addressBookSize; i++) {
addText = addText+entry[i].viewAllInfo();
}
System.out.print(addText);
JOptionPane.showMessageDialog(null, new JTextArea(addText));
回答2:
Looking at your data again, I'd probably display it in a JTable, and then if desired, would display this in a JOptionPane or in a GUI. If you need simpler, then display it in a JTextArea whose font has been set to monospaced, and use String.format(...) or something similar to allow your Strings to be displayed in a table.
来源:https://stackoverflow.com/questions/5032831/joptionpane-in-java