How to add a JButton to a Panel?

Viewed 33

my Problem is that I want to add a JButton to a JPanel created with IntelliJ. So my Question is how to archive that the Jbutton creates a new JButton right beside (or under) it?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class History_GUI {
    private JPanel Panel;
    public JButton load1Button;
    public JButton backButton;
    public static String loaded[] = new String[20];
    public History_GUI(JFrame frame) {
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
        JButton load = new JButton();
        {
            load.setVisible(true);
            load.setText(Rechner_GUI.answer);
        }
        load1Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Panel.add(load);
                System.out.println(loaded[1]);
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("History");
        frame.setContentPane(new History_GUI(frame).Panel);
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Thank you in advance.

0 Answers
Related