简体   繁体   中英

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

    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 . So you need to direct them to your text component.

Also, you can't use JTextField for this because it is not multiline. Instead, you should use something like a JTextArea inside a JScrollPane .

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 .

As a side note, you should be creating your GUI on the Swing event thread. In other words, your main should be wrapped in a call to invokeLater :

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

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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