简体   繁体   English

如何在Jtextfield中调用Loop

[英]How to call the Loop in Jtextfield

Im having trouble on calling my Loop that i created to be placed on the JTextField. Im only a beginner on GUI so i don't understand what i am missing or lacking . Please Help me.
the program must print a box of period if the user enters 1 and box of asterisk if the user
enters 2. and if the user enters 2 or more an error message will show up.

I re-editted the code sir. 我重新编辑了代码先生。 this is what i came up, the problem is after i re-enter a number the Jtextarea just keeps stacking the print, it does not refresh i don't know why.Example is if i enter 1 the box of periods will shop then if i enter 2 the box of asterisk appears below the box of periods .and it just keeps on stacking 这是我想出的问题,问题是我重新输入一个数字后,Jtextarea只是不断堆积打印件,它不刷新,我不知道为什么。例如,如果我输入1,则句点框将进行购物,如果我输入2,星号框出现在句点框的下面。它一直在堆积

    import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame{

    private JLabel numL,resultL;
    private JTextField numTF;
    private JTextArea resultTF;
    private JButton printB,exitB;
    private PrintButtonHandler pbHandler;
    private ExitButtonHandler exitHandler;




    public Box(){

        numL=new JLabel("Enter 1 or 2", SwingConstants.CENTER);
        resultL=new JLabel("Result",SwingConstants.CENTER);
        numTF=new JTextField(20);
        //resultTF=new JTextField(20);
        resultTF = new JTextArea(5,5);

        printB=new JButton("Print");
        pbHandler=new PrintButtonHandler();
        printB.addActionListener(pbHandler);


        exitB=new JButton("Exit");
        exitHandler= new ExitButtonHandler();
        exitB.addActionListener(exitHandler);


        setTitle("BOX");

        Container p=getContentPane();
        p.setLayout(new GridLayout(5,1));

        p.add(numL);
        p.add(numTF);
        p.add(resultL);
        p.add(resultTF);
        p.add(printB);
        p.add(exitB);

        setSize(600,600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


        private class PrintButtonHandler implements ActionListener{
            public void actionPerformed(ActionEvent e){
               //Box1 p=new Box1();
                int num,height=5,width=5,numLL;
                  numLL=Integer.parseInt(numTF.getText());

                Font f = resultTF.getFont();
                resultTF.setFont(new Font(Font.MONOSPACED, f.getStyle(), f.getSize()));





            if(numLL==1){
                for(int i = 0; i < 5; i++) {
                 for(int j = 0; j < 5; j++) {
                resultTF.append(".");
                }
                resultTF.append("\n");
                }

            }else if(numLL==2){
                for(int i = 0; i < 5; i++) {
                 for(int j = 0; j < 5; j++) {
                resultTF.append("*");
                }
                resultTF.append("\n");
                }
            }else if(numLL>2){
                resultTF.append("NOT 1 OR 2:");

            }
            }
        }

        private class ExitButtonHandler implements ActionListener{
            public void actionPerformed(ActionEvent e){

                System.exit(0);

            }
        }

        public static void main(String[]args){
            Box p=new Box();
        }
}

Right now you are directing your boxes to System.out . 现在,您正在将盒子定向到System.out So you need to direct them to your text component. 因此,您需要将它们定向到您的文本组件。

Also, you can't use JTextField for this because it is not multiline. 另外,您不能为此使用JTextField ,因为它不是多行的。 Instead, you should use something like a JTextArea inside a JScrollPane . 相反,您应该在JScrollPane使用类似JTextArea东西。

resultTF = new JTextArea();
Font f = resultTF.getFont();
resultTF.setFont(new Font(Font.MONOSPACED, f.getStyle(), f.getSize()));

add(new JScrollPane(resultTF));

.
.
.

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
        resultTF.append(".");
    }
    resultTF.append("\n");
}

If you don't want a scroll pane, you could also, for example, create the text area with specific rows and columns ( new JTextArea(5, 5) ), use a StringBuilder to create your box and use setText instead of append . 如果您不希望使用滚动窗格,则还可以例如创建具有特定行和列的文本区域( new JTextArea(5, 5) ),使用StringBuilder创建框并使用setText而不是append

As a side note, you should be creating your GUI on the Swing event thread. 附带说明,您应该在Swing事件线程上创建GUI。 In other words, your main should be wrapped in a call to invokeLater : 换句话说,您的main应该包装在对invokeLater的调用中:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Box p = new Box();
        }
    });
}

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM