[英]Java issue with GridBagLayout + GridLayout
我在GridBagLayout中的GridLayout遇到問題。
我希望我的GridLayout適合GridBagLayout的寬度。
我說明了我要做什么:
但是這是我獲得的:
“測試版本:1.5,不負責任”是具有1列和3行的GridLayout。 “ Mitramail,1.0版,Disponible”是另一個具有1列和3行的GridLayout。 我試圖將它們添加到具有x行和2列的另一個GridLayout中。 因此通常它們應該彼此相鄰,但是它們是彼此相鄰的。
這是代碼:
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);
}
任何人都知道如何使我的“綠色” GridLayout填充紅色GridBagLayout寬度,並有2列添加如下所示的動態GridLayout:
7 | ...
?
感謝您的關注。
診斷上下文外代碼段有些困難,因為您的代碼中可能存在某些事情,您尚未共享,這可能會影響結果
將來,請考慮提供一個可運行的示例來演示您的問題。 這不是代碼轉儲,而是您正在執行的操作的一個示例,突出顯示了您所遇到的問題。 這將減少混亂並改善響應
話雖如此,它似乎對我有用...
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);
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.