2014-09-13 66 views
1

我想首先感謝stackoverflow社區和所有回答這些問題的優秀人士。更常見的是,我通過閱讀而不是打字來找到自己的答案,但是我時間緊迫,因爲這是一項必須在接下來的幾天內完成的家庭作業。所以這是我的問題。打印StringBuffer JOptionPane

編程介紹> ASCII藝術項目>我想將它打印到一個窗口,而不僅僅是無聊的控制檯。

這是我想出了這麼遠,但(當相同的StringBuilder打印到控制檯它看起來是意指)的圖像不看在JOptionPane.showMessageDialog正確:

import javax.swing.*; 

public class R2D2 
{ 
    public static void main(String[] args) 
    {  
    boolean test = false;//Create and initialize boolean variable; later used to break out of the Do While loop. 

    do 
    { 
    String name = JOptionPane.showInputDialog("Who is everyone's favorite Droid?");//GUI asking for 'favorite droid' input. 

    if (name.equalsIgnoreCase("R2D2") || name.equalsIgnoreCase("R2"))//Sets boolean test to true if the user inputs the correct answer ie 'R2D2' or 'R2', not case sensitive. 
    { 
     test = true; 
    } 

    else 
    { 
     JOptionPane.showMessageDialog(null, "Nope! Try Again...Dummy.");//Chides the user. 
    } 
    } 
    while (test == false);//Continues back to the top if the user doesn't answer correctly. 


    String l0 = new String("    _____________     ");//In lines 14,15,16 the escape used to place the backslash has no real 
    String l1 = new String("    /_/_/_/_/_/_/_\\    ");//effect because there isn't any ASCII art to the right of the escape (oh no, I just 
    String l2 = new String("    /_/_/_/_/_/_/_/_\\    ");//realized you probably won't like how I am spanning lines with the single line commentor). 
    String l3 = new String("   / _______ \\    "); 
    String l4 = new String("   / /(\"\") \\ \\   ");//The first double quote is shifted one character to the right, the second double quote is shifted two characters to the right, the second backslash (of the first backslash grouping) is shifted three characters to the right, and the second backslash (of the second backslash grouping) is shifted four characters to the right. It's kinda lonely out here. 
    String l5 = new String("   / /_________\\ \\   ");//1 escape in R2's body results in his right edge being shifted to the right one character. 
    String l6 = new String("  /      \\   "); 
    String l7 = new String("  /   0 (O) \\   ");//Again no real effect seen from the escapes on lines 19, 20, 21. 
    String l8 = new String("  /       \\   "); 
    String l9 = new String(" ____(  ---------------  )____  "); 
    String l10 = new String(" | |  ---------------  | | "); 
    String l11 = new String(" | |        | | "); 
    String l12 = new String(" | |        | | "); 
    String l13 = new String(" | |  ---------------  | | "); 
    String l14 = new String(" | |  ===============  | | "); 
    String l15 = new String(" | |        | | "); 
    String l16 = new String(" | |        | | "); 
    String l17 = new String(" | |  ---------------  | | "); 
    String l18 = new String(" | |  ===============  | | "); 
    String l19 = new String(" | |        | | "); 
    String l20 = new String(" | |        | | "); 
    String l21 = new String(" | |        | | "); 
    String l22 = new String(" | |        | | "); 
    String l23 = new String(" ) |_____________________________| ( "); 
    String l24 = new String("/ \\       / \\ "); 
    String l25 = new String("(_______)       (_______) "); 


    String[] lineholder = {l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18,l19,l20,l21,l22,l23,l24,l25}; 


    StringBuilder sb = new StringBuilder(); 

    for(int i=0 ; i < lineholder.length; i++) 
    { 
    sb.append(lineholder[i] + "\n"); 
    } 

    JOptionPane.showMessageDialog(null, sb.toString()); 

    System.out.println(sb.toString()); 


    System.out.println("\n\n\n//In Your Best Robot Voice");//Intentionally left comment break visible for effect 
    System.out.println("End Of Program"); 


    } 
} 

有什麼建議嗎?再次感謝。

+0

你能分享畸形輸出的截圖嗎? – Mureinik 2014-09-13 12:10:12

回答

0

您的問題可能是字體之一,因爲JOptionPane不會將數據顯示爲需要的等寬字體。

一種選擇是你設置的字體等寬使用一個JTextArea:

JTextArea tArea = new JTextArea(lineholder.length, 50); 
    tArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10)); 
    tArea.setText(sb.toString()); 
    JScrollPane scrollPane = new JScrollPane(tArea); 
    //!! JOptionPane.showMessageDialog(null, sb.toString()); 
    JOptionPane.showMessageDialog(null, scrollPane); 

理論上你可以通過

UIManager.put("OptionPane.font", new Font(Font.MONOSPACED, Font.PLAIN, 10)); 

設置JOptoinPane的字體,但我無法得到這工作。


編輯,或者如果你想擺脫JScrollPane的的(習慣,我使用它),白色背景:,

JTextArea tArea = new JTextArea(lineholder.length, 50); 
    tArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10)); 
    tArea.setText(sb.toString()); 
    tArea.setOpaque(false); 
    JOptionPane.showMessageDialog(null, tArea); 

一如往常如果有什麼在這個答案讓你感到困惑,或者如果它不能完全回答你的問題,那麼請發表評論,詢問這個問題的澄清。

+0

這就是它的氣墊船,謝謝!如果我可以回顧一下,以便進一步理解這裏完成的工作: – 2014-09-13 20:04:23

+0

控制檯以等寬字體打印,JOptionPane默認使用非等寬字體,這導致了我的ASCII藝術的改變。理論上,我們可以將JOptionPane字體更改爲等寬字體,但出於某種原因,這對您不起作用。相反,我們創建了一個帶有(行,列)構造函數的JTextArea。然後,我們通過傳入一個由(font facename MONOSPACE,style PLAIN,size 10)構造的新字體對象來設置JTextArea的字體。然後,我們將填充的等寬不透明的JTextArea傳遞給JOptionPane的showMessageDialog。那是對的嗎? – 2014-09-13 20:19:43