简体   繁体   中英

How can i use multiple panels with different layouts for my gui?

i have problems with nesting 2 different button panels into my main panel. It worked for me with only one 1 panel. Now with my actual nesting i see the last added panel over my main panel and my first button panel. Additionally the "editButton" i added, is not showing up. I got no errors while compiling. It seems to be that i just made a mistake in the arrangement. Would be very nice if there are any suggestions. Here is my code snippet: Thanks in advance!

private final String name;
private JLabel catLabel;
private JButton cat1Button;
private JButton cat2Button;
private JButton cat3Button;
private JButton cat4Button;
private JButton editButton;
private JButton deleteButton;
private JButton insertButton;

public JPanel panelMain;
public JPanel panel;
public JPanel panel1;


public ReadwriteQuiz readwrite;

public GUIEdit(String name){

    this.name = name;
    this.setTitle(name);

    this.setLocationRelativeTo(null);
    this.setLayout((null));
    this.setSize(400,300);
    this.setLocation(400,150);
    this.setResizable(false);

    panelMain = new JPanel();
    panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));

    panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.setBackground(Color.lightGray);

    panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
    panel1.setBackground(Color.ORANGE);

    panelMain.add(panel);
    panelMain.add(panel1);

    catLabel= new JLabel("Willkommen zum Quizzeln ! \n Wählen Sie Ihre Kategorie");
    catLabel.setBounds(90,10,260,40);
    panel.add(catLabel);

    cat1Button = new JButton("Kategorie 1");
    cat1Button.setBounds(52,90,120,40);
    panel.add(cat1Button);
    cat1Button.addActionListener((e) -> {
        try {
            readwrite.readFile("kategorie1.txt");
        } catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        }
    });

    cat2Button = new JButton("Kategorie 2");
    cat2Button.setBounds(220,90,120,40);
    panel.add(cat2Button);

    cat3Button = new JButton("Kategorie 3");
    cat3Button.setBounds(52,160,120,40);
    panel.add(cat3Button);

    cat4Button = new JButton("Kategorie 4");
    cat4Button.setBounds(220,160,120,40);
    panel.add(cat4Button);

    editButton = new JButton("Frage editieren");
    editButton.setBounds(52,400,120,40);
    panel1.add(editButton);

    this.add(panelMain);
    this.add(panel);
    this.add(panel1);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {

}

You have one main panel and you have already added the other nested panels to the main panel so you only add that main panel to the parent panel but you're adding the main panel ( including the other 2 ) and again adding the 1st panel and again adding the 2nd panel

so

this.add(panelMain);

should be enough


Full class

public class app {
    private JTextField textField1;
    private JPanel panel;
    private JLabel label;
    private JLabel catLabel;
    private JButton cat1Button;
    private JButton cat2Button;
    private JButton cat3Button;
    private JButton cat4Button;
    private JButton editButton;
    private JButton deleteButton;
    private JButton insertButton;

    public JPanel panelMain;
    public JPanel panel2;
    public JPanel panel1;

    public app(JFrame frame) {

        panelMain = new JPanel();
        panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));

        panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());
        panel2.setBackground(Color.lightGray);

        panel1 = new JPanel();
        panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
        panel1.setBackground(Color.ORANGE);

        panelMain.add(panel2);
        panelMain.add(panel1);

        catLabel= new JLabel("Willkommen zum Quizzeln ! \n Wählen Sie Ihre Kategorie");
        catLabel.setBounds(90,10,260,40);
        panel2.add(catLabel);

        cat1Button = new JButton("Kategorie 1");
        cat1Button.setBounds(52,90,120,40);
        panel2.add(cat1Button);

        cat2Button = new JButton("Kategorie 2");
        cat2Button.setBounds(220,90,120,40);
        panel2.add(cat2Button);

        cat3Button = new JButton("Kategorie 3");
        cat3Button.setBounds(52,160,120,40);
        panel2.add(cat3Button);

        cat4Button = new JButton("Kategorie 4");
        cat4Button.setBounds(220,160,120,40);
        panel2.add(cat4Button);

        editButton = new JButton("Frage editieren");
        editButton.setBounds(52,400,120,40);
        panel1.add(editButton);
        frame.add(panelMain);
    }

    public static void main(String[] args) {
        JFrame fram = new JFrame("app");
        new app(fram);
        fram.pack();
        fram.setVisible(true);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM