问题
I'm having trouble with a GridLayout in a GridBagLayout.
I want my GridLayout fit the width of the GridBagLayout.
I made an illustration of what i'm trying to do :
But here is what i obtained :
"Test, version : 1.5, Disponible" is a GridLayout with 1 columns and 3 rows. "Mitramail, version 1.0, Disponible" is another GridLayout with 1 columns and 3 rows too. I'm trying to adding them in another GridLayout with x row and 2 columns. so normally they should be next to each other, but they are one under the other.
Here is the code :
GridBagConstraints c = new GridBagConstraints();
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
c.weightx = 1;
panelDynamique.add(new JLabel("Disponible"), c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panelDynamique.add(new JSeparator(), c);
panelDispo.setLayout(new GridLayout(this.getNbAppFor("Disponible") /*Variable X, replace by 2 for better understanding*/, 2));
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 3;
//c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTH;
c.weighty = 1;
panelDynamique.add(panelDispo, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
panelDynamique.add(new JSeparator(), c);
for(Entry<String, String> entry : this.HMServ().entrySet()){
JPanel tmp = new JPanel();
tmp.setLayout(new GridLayout(3, 1));
tmp.add(new JLabel(entry.getKey())); //1) Test, 2) Mitramail
tmp.add(new JLabel("Version : " + entry.getValue())); //1) Version 1.5, 2) 1.0
tmp.add(new JLabel(this.infoVersion(entry.getKey(), entry.getValue()))); //Disponible
panelDispo.add(tmp);
}
Anybody knows how can i make my "Green" GridLayout fill the Red GridBagLayout width and have 2 columns to add dynamics GridLayout like this schema :
1 | 2
3 | 4
5 | 6
7 | ...
?
Thanks for your attention.
回答1:
It's a little hard to diagnose out of context code snippets, as there might be things going on in your code, which you've not shared, which might be effecting the results
In the future, consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses
Having said that, it seems to work for me...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class MakeItSo {
public static void main(String[] args) {
new MakeItSo();
}
public MakeItSo() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
setLayout(new GridBagLayout());
setBorder(new LineBorder(Color.RED));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JLabel("Disponible:"), gbc);
add(new JSeparator(), gbc);
add(new GridPane());
}
}
public class GridPane extends JPanel {
public GridPane() {
setLayout(new GridLayout(2, 2));
setBorder(new LineBorder(Color.YELLOW));
add(new DetailPane("Mitramail.zip", "Version: 1.0", "Disponible"));
add(new DetailPane("Test", "Version: 1.5", "Disponible"));
add(new DetailPane("Other"));
add(new DetailPane("Other"));
}
}
protected class DetailPane extends JPanel {
public DetailPane(String... values) {
setLayout(new GridBagLayout());
setBorder(new LineBorder(Color.GREEN));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
if (values != null && values.length > 0) {
for (String value : values) {
add(new JLabel(value), gbc);
}
}
}
}
}
来源:https://stackoverflow.com/questions/34731570/java-issue-with-gridbaglayout-gridlayout