【问题标题】:How to call the Loop in Jtextfield如何在 Jtextfield 中调用循环
【发布时间】:2014-11-23 00:57:59
【问题描述】:
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();
        }
}

【问题讨论】:

  • 到底是什么问题?
  • PrintButtonHandler 部分。我无法让程序运行循环。
  • 它对我来说很好用。期待System.out.print 输出到resultTF 还是什么?
  • 是的 .. 星号框或句号框必须在 resultTF 内。

标签: java jframe jtextfield


【解决方案1】:

现在您正在将您的邮箱指向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();
        }
    });
}

另见:

【讨论】:

  • Thankyou Goodsir,但是我有一个新的问题..在我编辑了代码之后,JTextarea 继续统计结果。它不刷新
  • 可以在添加框之前清除文字,如@9​​87654335@
  • actionPerformed 的开头。在附加框之前。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-26
  • 1970-01-01
相关资源
最近更新 更多