2013-02-10 38 views
1

我有一個字符串,我想在JOptionPane中顯示。在JOptionPane中對齊字符串

String info = "Name:" + _name + "\n" + 
       "Phone:" + _phone; 

我試圖添加\t但它沒有工作。

我也試圖

int choose = JOptionPane.showConfirmDialog(this,new JTextArea(info),"XXX",0); 

但它並不好看。

有沒有其他方法可以做到這一點? (如果你知道一個解決方案,我可以使用類似\t這將是對我非常有用)

*在這個具體的例子我可以手動對齊,但是我正在尋找一個一般解決方案

回答

4

HTML格式可以幫助:

String info = "Name:" + _name + "<br>" + 
       "Phone:" + _phone + "<br>"; 

int choose = 
     JOptionPane.showConfirmDialog(this, "<html>" + info + "</html>", "XXX", 0); 

另一種選擇是在JOptionPane對話框使用JTable如本solution

+0

我試圖這樣做,仍然不起作用。 (我有更多的字段,不僅'_name'和'_phone',我想對齊它,所以'_name','_phone'和其他字段將從同一列開始)。 – Maroun 2013-02-10 12:49:35

+1

你可以使用'HTML'表格或使用'CSS'做對齊 – Reimeus 2013-02-10 12:51:10

+0

我會試試。非常感謝。 – Maroun 2013-02-10 12:52:18

1

我有類似的東西,它的工作:

String st =  "<table border = \"0\">" + 
        "<tr><td>VALUE1: </td><td>" + _value2 + "</td></tr>"+ 
        "<tr><td>VALUE2: </td><td>" + _value4 + "</td></tr>" + 
        //..... 
        "</table>";