繁体   English   中英

如何为我的 gui 使用具有不同布局的多个面板?

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

我在将 2 个不同的按钮面板嵌套到我的主面板时遇到问题。 它对我来说只有一个 1 面板。 现在通过我的实际嵌套,我在主面板和第一个按钮面板上看到了最后添加的面板。 此外,我添加的“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) {

}

您有一个主面板,并且您已经将其他嵌套面板添加到主面板,因此您只需将该主面板添加到父面板,但您正在添加主面板(包括其他 2 个)并再次添加第一个面板和再次添加第二个面板

所以

this.add(panelMain);

应该够了


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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