简体   繁体   中英

How can I use GroupLayout to organize a 2x2 auto-sized grid

This panel is used to organize everything this method does. It is inside a JFrame and everything works perfectly except that the positioning inside this one JPanel, which has it staggered(from left to right, there is showQuestionsPanel, new line aligned with bottom right of that we have the addQuestionPanel, then aligned with the bottom of the showquestions panel but to the right of the addQuestionsPanel we have the closeEQPanel, and to the right of that but below the addQuestionsPanel is the eqbuttonPanel). The closeEQPanel should be the top right panel, with showQuestionsPanel to its left, and immediately below should be the addQuestionPanel on the left with the eqbuttonPanel to the right, all aligned as a 2x2 grid. What do I have wrong in this layout?

//arrange visual elements/create main panel            
JPanel mainEQPanel = new JPanel();
GroupLayout eqLayout = new GroupLayout(mainEQPanel);
mainEQPanel.setLayout(eqLayout);
eqLayout.setAutoCreateGaps(true);
eqLayout.setAutoCreateContainerGaps(true);
eqLayout.setHorizontalGroup(
    eqLayout.createSequentialGroup()
        .addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.TRAILING))
            .addComponent(showQuestionsPanel)
            .addComponent(addQuestionPanel)
        .addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.CENTER))
            .addComponent(closeEQPanel)   
            .addComponent(eqbuttonPanel)
);      
eqLayout.setVerticalGroup(
        eqLayout.createSequentialGroup()
        .addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.LEADING))
            .addComponent(showQuestionsPanel)
            .addComponent(closeEQPanel)
        .addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.LEADING))
            .addComponent(addQuestionPanel)
            .addComponent(eqbuttonPanel)
);

You are almost there, your problem is that you never assign anything to your ParallelGroup, your bracer/bracket is in the wrong spot:

.addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.TRAILING))//NOTE THE CLOSE BRACKET HERE
.addComponent(showQuestionsPanel)
.addComponent(addQuestionPanel)

But you need:

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
        .addComponent(showQuestionsPanel)
        .addComponent(addQuestionPanel))//NOTE THE CLOSE BRACKET HERE INSTEAD

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