JOptionPane in Java

筅森魡賤 提交于 2019-12-20 03:42:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!