【问题标题】:Creating JTextFields in a for loop to calculate pow of a number stepwise在 for 循环中创建 JTextField 以逐步计算数字的 pow
【发布时间】:2020-05-20 19:03:36
【问题描述】:

我有一个 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。
  • @FredK 当我将字符串输出存储在文本区域中时,它仍然不显示它......
  • 如果在 JTextArea 上调用 setText(),它将用新字符串替换文本。使用 append() 添加到末尾。此外,您不应该在主线程上执行此操作;您需要在 EventDispatchThread 上执行此操作。请参阅 swingUtilities.nvokeLater()。

标签: java swing jframe jtextfield pow


【解决方案1】:

将您的 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”是在附加文本后添加新行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多