Java JOptionPane.showmessagedialog

Java JOptionPane.showmessagedialog,java,swing,joptionpane,Java,Swing,Joptionpane,因此,我必须编写一个DrawKwin.java,它将打印带有小星星*的字母K。 用户给出一个整数参数,如果参数小于4或大于30,程序将终止。 使用该参数,程序将创建与试图打印字母K的参数一样多的行。 例如,如果用户键入数字6,程序将打印6行试图创建字母K。输入将来自带有的输入面板,字母K将打印在带有joptionpane.showmessagedialog的输出面板中 以下是不带输出面板代码的代码: package Askisi_A1; import javax.swing.JOp

因此,我必须编写一个DrawKwin.java,它将打印带有小星星*的字母K。 用户给出一个整数参数,如果参数小于4或大于30,程序将终止。 使用该参数,程序将创建与试图打印字母K的参数一样多的行。 例如,如果用户键入数字6,程序将打印6行试图创建字母K。输入将来自带有的输入面板,字母K将打印在带有joptionpane.showmessagedialog的输出面板中

以下是不带输出面板代码的代码:

       package Askisi_A1;
import javax.swing.JOptionPane;

class DrawKwin {

public static void main(String[] L) {
    int line=Integer.parseInt(L[0]); // make L an  integer.


    if(line <= 4) 
    {
        System.out.println("Program end, wrong argument!");
        System.exit(0);
    }
    else if(line >= 30) 
    {
        System.out.println("Program end, wrong argument!");
        System.exit(0);
    }

    do
    {

        int mid=line/2; // find the middle.
        int gap=0;     // 'gap' is for the gap between the stars .
        for(int i=0;i<line;i++)   //loop for the creation of letter K.
        {
            if(i==0) gap=mid;
            if(i<mid) // if it is before the middle of letter K, start printing stars and gaps but start with gap=middle and the decrease the number of gaps as you change lines.
            {

                System.out.print("*");
                for(int j=gap;j>0;j--)   // placement of gaps between the stars.
                {
                    System.out.print(" ");
                }
                System.out.println("*");
                gap--;
            }
            else if(i==mid && i!=0) // if it is in the middle of letter K, it will print only one star.
            {
                System.out.println("*");
                gap=1;
            }
            else // if it is past the middle section of letter K, it will continue printing gaps but now the gaps start from 0 and keep increasing at each line.
            {
                System.out.print("*");
                for(int j=0;j<gap;j++)  // placement of gaps between the stars.
                {
                    System.out.print(" ");
                }
                System.out.println("*");
                gap++;
            }

        }       


        line = Integer.parseInt(JOptionPane.showInputDialog( "Give me a number ",4)); // input from input panel.
    }while(line>=4 && line<=30);


}
但是我需要在joptionpane.showmessagedialog的帮助下,在输出面板中打印这个。 有人能帮我吗? 对不起,如果我的英语不好。
我的截止日期是星期一。

试试这样:

*  *
* *
*
* *
*  *
do {

            int mid = line / 2; // find the middle.
            int gap = 0; // 'gap' is for the gap between the stars .
            for (int i = 0; i < line; i++) // loop for the creation of letter K.
            {
                if (i == 0)
                    gap = mid;
                if (i < mid) // if it is before the middle of letter K, start
                                // printing stars and gaps but start with
                                // gap=middle and the decrease the number of
                                // gaps as you change lines.
                {

                    output += "*";
                    for (int j = gap; j > 0; j--) // placement of gaps between
                                                    // the stars.
                    {
                        output += " ";
                    }
                    output += "*\n";
                    gap--;
                } else if (i == mid && i != 0) // if it is in the middle of
                                                // letter K, it will print only
                                                // one star.
                {
                    output += "*\n";
                    gap = 1;
                } else // if it is past the middle section of letter K, it will
                        // continue printing gaps but now the gaps start from 0
                        // and keep increasing at each line.
                {
                    output += "*";
                    for (int j = 0; j < gap; j++) // placement of gaps between
                                                    // the stars.
                    {
                        output += " ";
                    }
                    output += "*\n";
                    gap++;
                }

            }
            JOptionPane.showMessageDialog(null, output);

            line = Integer.parseInt(JOptionPane.showInputDialog(
                    "Give me a number ", 4)); // input from input panel.
            output = "";
        } while (line >= 4 && line <= 30);

构建输出字符串,然后用输出字符串显示MessageDialog。

在开头创建一个字符串,例如

字符串kLetter=

每次之后:

系统输出打印*; 添加 克莱特+=*

系统输出; 添加 克莱特+=

System.out.println*; 添加 kLetter+=*\n

然后使用

JOptionPane.showInputDialog(result, null);
若要在对话框中显示结果,请记住在对话框中显示结果后将其设置为空字符串

result = "";

生成字符串\n是新行字符。生成表示K的字符串后,使用joptionpane.showmessagedialog显示生成的字符串。