在 for 循环中创建 JTextFields 以逐步计算数字的 pow

Creating JTextFields in a for loop to calculate pow of a number stepwise

我有一个 JFrame,用户应在其中输入 2 个数字,并且应逐步显示该数字的幂。

这是我目前拥有的:

它应该是这样的:

问题出在这一行:

txt3.setText(output);

因为它只是在JFrame 上写了一行。

如何像上图那样在 JFrame 上添加输出?

代码如下:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PotenzFrame extends JFrame {

    private Toolkit toolkit;

    public PotenzFrame() {
        setSize(620, 500);

        toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation((size.width - getWidth())/2, (size.height -getHeight())/2);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        getContentPane().add(panel);

        panel.setLayout(null);

        JLabel label = new JLabel("Potenzen");
        // label.setPreferredSize(new Dimension(50, 50));
        label.setBounds(80,0,80,80);

        JLabel lbl1 = new JLabel("zahl");
        lbl1.setBounds(30,70,80,30);

        JLabel lbl2 = new JLabel("ende");
        lbl2.setBounds(30,120,110,30);

        JLabel lbl3 = new JLabel("Potenzen:");
        lbl3.setBounds(300,150,80,30);

        final JTextField txt1 = new JTextField(5);
        txt1.setBounds(145,70,50,30);
        txt1.setText("0");
        final JTextField txt2 = new JTextField(5);
        txt2.setBounds(145,120,50,30);
        txt2.setText("0");
        final JTextField txt3 = new JTextField(5);
        txt3.setBounds(300,200,200,200);

        JButton comp = new JButton("Potenzen berechnen");
        comp.setBounds(20, 400, 200, 30);
        comp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                int a1 = Integer.parseInt(txt1.getText());
                int a2 = Integer.parseInt(txt2.getText());
                String output;
                int multiplied;

                for(int i = 1; i <= a2; i++){
                    multiplied = (int) Math.pow(a1, i);
                    output = a1 + " hoch " + i + " ist " + multiplied;
                    txt3.setText(output);
                    System.out.println(output);
                }
            }
        });

        panel.add(label);
        panel.add(lbl1);
        panel.add(lbl2);
        panel.add(lbl3);
        panel.add(txt1);
        panel.add(txt2);
        panel.add(txt3);
        panel.add(comp);
    }

    public static void main(String[] args) {
        PotenzFrame buttons = new PotenzFrame();
        buttons.setVisible(true);
    }
}

我尝试了以下方法,但也没有用:

for(int i = 1; i <= a2; i++){
    multiplied = (int) Math.pow(a1, i);
    output = a1 + " hoch " + i + " ist " + multiplied;
    JTextField txt = new JTextField(i+4);
    txt.setText(output);
    panel.add(txt);
    System.out.println(output);
}

将您的 JTextField 更改为 JTextArea 以便能够包含多行文本,而不是只有一行文本。

final JTextArea txt3 = new JTextArea();
txt3.setBounds(300,200,200,200);

然后在循环中,将 txt3.setText(output) 更改为 txt3.append(output + "\n")。

for(int i = 1; i <= a2; i++){
    multiplied = (int) Math.pow(a1, i);
    output = a1 + " hoch " + i + " ist " + multiplied;
    txt3.append(output + "\n");
    System.out.println(output);
}

append() 将文本添加到您的 TextArea 而不是用 setText() 替换文本,并且“\n”是在您追加文本后添加一个新行。