有没有一种方法在 JOptionPane.showMessageDialog() 上打印多行模式?

huangapple go评论72阅读模式
英文:

Is there a way to print a multiline pattern on JOptionPane.showMessageDialog()?

问题

Sure, here's the translated code portion:

for (int row = 1; row <= L; row++) {
    for (int col = 1; col <= L + L - 1; col++) {
        if (row == L || row + col == L + 1 || col - row == L - 1)
            System.out.print("*");
        else
            System.out.print(" ");
    }
    System.out.println();
}

Please note that the code you provided is in Java and seems to generate a pattern of a hollow pyramid using asterisks (*). If you have any further questions or need assistance with anything else, feel free to ask.

英文:

So I was wondering if there is a way to output a multiline pattern with stars (*) like a triangle or a pyramid on JOptionPane.showMessageDialog(). I know that this works like that: JOptionPane.showMessageDialog(null,"Hello World!"); but instead of Hello World! I need to print on the new window that pops up a hollow pyramid and I can't figure it out. Can you help me? Thanks in advance. I've added the code that prints the hollow pyramid. The variable L is the height of the pyramid and it is provided by the user whenever the program is executed. So how can I print this pattern with a variable height in a JOptionPane.showMessageDialog()?

for(int row=1;row&lt;=L;row++){
    for(int col=1;col&lt;=L+L-1;col++){
        if(row==L || row+col==L+1 || col-row==L-1) System.out.print(&quot;*&quot;);
        else System.out.print(&quot; &quot;);
    }
    System.out.println();
}

答案1

得分: 2

不要有别的内容,只返回翻译好的部分:

而不是每次将文本逐个字符打印到 System.out,你应该将字符附加到一个字符串变量(最好使用 StringBuilder 以获得更好的性能)。

// 创建一个空字符串,并通过添加字符来生成金字塔
StringBuilder pyramid = new StringBuilder();
for (int row = 1; row <= L; row++) {
    for (int col = 1; col <= L + L - 1; col++) {
        if (row == L || row + col == L + 1 || col - row == L - 1)
            pyramid.append("*");
        else
            pyramid.append(" ");
    }
    pyramid.append("\n");
}
// 输出与原始问题中所有微小的 System.out 写入相同的内容
System.out.println(pyramid);

对于一个较小的学术问题,没有完全必要进行优化;然而,为了以后的参考,当连接大量微小字符串时,应使用 StringBuilder。我提到这一点的原因是,内部实际上,当你“添加”(即连接)两个字符串时,每次都会分配一个新的字符串。当在循环中使用时,这可能会累积产生大量新的字符串分配。

注意,JOptionPane.showMessageDialog(...) 使用的默认字体不是等宽字体,默认情况下字符具有可变长度宽度。尝试有效地显示 ASCII 艺术可能效果不佳。可以通过使用一个不可编辑的透明文本区来解决这个问题:

JTextArea textbox = new JTextArea(pyramid);
textbox.setOpaque(false);
textbox.setEditable(false);
textbox.setFont(new Font("Monospaced", Font.BOLD, 12));
JOptionPane.showMessageDialog(null, textbox, "Pyramid", JOptionPane.PLAIN_MESSAGE);

这会产生以下效果:
有没有一种方法在 JOptionPane.showMessageDialog() 上打印多行模式?

英文:

Instead of printing text a few characters at a time to System.out, you should append the characters to a String variable (or ideally StringBuilder for better performance).

// create an empty string and generate the pyramid by adding characters
String pyramid = &quot;&quot;;
for (int row = 1; row &lt;= L; row++) {
    for (int col = 1; col &lt;= L + L - 1; col++) {
        if (row == L || row + col == L + 1 || col - row == L - 1) pyramid += &quot;*&quot;;
        else pyramid += &quot; &quot;;
    }
    pyramid += &quot;\n&quot;;
}
// outputs the same thing as all the tiny System.out writes in the original question
System.out.println(pyramid);

For a smaller academic problem, it is not totally necessary to optimize this; however, for future reference, you should use a StringBuilder when concatenating a significant amount of tiny strings. The reason I mention this is that internally, when you "add" (i.e. concatenate) two strings together, you are actually allocating another new string each time. This can add up to a lot of new allocations for strings when used in a loop.

<hr>

Note that the default font with JOptionPane.showMessageDialog(...) is not mono-spaced by default, meaning characters have variable length width. Trying to effectively display ASCII art might not work well. This can be circumvented by using a uneditable transparent text area:

JTextArea textbox = new JTextArea(pyramid);
textbox.setOpaque(false);
textbox.setEditable(false);
textbox.setFont(new Font(&quot;Monospaced&quot;, Font.BOLD, 12));
JOptionPane.showMessageDialog(null, textbox, &quot;Pyramid&quot;, JOptionPane.PLAIN_MESSAGE);

This yields:<br>
有没有一种方法在 JOptionPane.showMessageDialog() 上打印多行模式?

huangapple
  • 本文由 发表于 2020年10月5日 16:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64204767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定