簡體   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.

我重新編輯了代碼先生。 這是我想出的問題,問題是我重新輸入一個數字后,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();
        }
}

現在,您正在將盒子定向到System.out 因此,您需要將它們定向到您的文本組件。

另外,您不能為此使用JTextField ,因為它不是多行的。 相反,您應該在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");
}

如果您不希望使用滾動窗格,則還可以例如創建具有特定行和列的文本區域( new JTextArea(5, 5) ),使用StringBuilder創建框並使用setText而不是append

附帶說明,您應該在Swing事件線程上創建GUI。 換句話說,您的main應該包裝在對invokeLater的調用中:

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

也可以看看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM