Как я могу использовать несколько панелей с разными макетами для моего gui? - PullRequest
0 голосов
/ 06 августа 2020

У меня проблемы с вложением двух разных кнопочных панелей в мою главную панель. У меня это сработало только с одной панелью. Теперь с моим фактическим вложением я вижу последнюю добавленную панель над моей основной панелью и моей первой панелью кнопок. Кроме того, добавленная мной кнопка "editButton" не отображается. При компиляции ошибок не было. Кажется, я просто ошибся в аранжировке. Было бы очень приятно, если есть предложения. Вот мой фрагмент кода: Заранее спасибо!

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) {

}

1 Ответ

0 голосов
/ 06 августа 2020

У вас есть одна основная панель, и вы уже добавили другие вложенные панели к основной панели, поэтому вы добавляете только эту основную панель к родительской панели, но вы добавляете основную панель (включая другие 2) и снова добавляете 1-я панель и снова добавление 2-й панели

, поэтому

this.add(panelMain);

должно быть достаточно

Полный класс

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