0

I have a JFrame in which the user shall enter 2 numbers and the powers of that number should be displayed step-by-step.

This is what I have so far:

enter image description here

This is what it should look like:

enter image description here

The problem is this line:

txt3.setText(output);

because it just writes one line on the JFrame.

How can I add the output on the JFrame like in the image above?

Here's the code:

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);
    }
}

I tried the following, but it didn't work either:

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);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
stepbysteptomathpro
  • 455
  • 1
  • 7
  • 16
  • 1
    A JTextField only displays a single line. Use a JTextArea for multiple lines. – FredK Feb 04 '20 at 22:15
  • @FredK When I store the string output in a textarea it still doesn't display it... – stepbysteptomathpro Feb 04 '20 at 22:16
  • 1
    If you call setText() on a JTextArea, it will REPLACE the text with the new string. Use append() to add to the end. Also, you should not be doing this on the main thread; you need to do it on the EventDispatchThread. See swingUtilities.nvokeLater(). – FredK Feb 04 '20 at 22:40

1 Answers1

1

Change your JTextField to a JTextArea to be able to have multiple lines of text, rather then just one line of text.

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

Then in your loop, change txt3.setText(output) to 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() adds text onto your TextArea rather then replacing the text with setText() and "\n" is adding a new line after you append the text.

Mike
  • 93
  • 6