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:
This is what it should look like:
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);
}