首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >印刷StringBuffer JOptionPane

印刷StringBuffer JOptionPane
EN

Stack Overflow用户
提问于 2014-09-13 12:06:59
回答 1查看 591关注 0票数 1

首先,我要向堆叠式社区和所有回答这些问题的伟大人士表示感谢。很多时候,我通过阅读而不是打字找到我的答案,然而我的时间紧迫,因为这是一项家庭作业,必须在接下来的几天内完成。所以这是我的问题。

编程介绍> ASCII艺术项目>我想把它打印到一个窗口,而不仅仅是无聊的控制台。

到目前为止,这就是我想出来的,但是在JOptionPane.showMessageDialog中,图像看起来并不正确(当同一个字符串生成器被打印到控制台时,它看起来确实是按照预期的那样):

代码语言:javascript
复制
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");


   }
}

有什么建议吗?再次感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-13 12:12:48

您的问题可能是字体问题,因为JOptionPane不会将数据显示为所需的单间距字体。

一种选择是使用JTextArea,将字体设置为单间距:

代码语言:javascript
复制
  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);

理论上您可以通过以下方式设置JOptoinPane的字体

代码语言:javascript
复制
UIManager.put("OptionPane.font", new Font(Font.MONOSPACED, Font.PLAIN, 10));

但我没能让它起作用。

编辑,或者如果您想摆脱JScrollPane (习惯于我使用它)和白色背景:

代码语言:javascript
复制
  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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25823056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档