GridBaglayout in Java Column Exers
-
12-11-2019 - |
Domanda
Sto cercando di usare GridBaglayout Layout Manager per raggiungere questo obiettivo:
Tuttavia, quello che sto ottenendo attualmente è questo:
Il problema è che i pannelli arancione e marrone/grigio dovrebbero occupare la seconda colonna, ma sembrano voler occupare il terzo solo quando si tratta di eseguire il codice.
Il codice che sto usando per il layout:
Container contentPane = form.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel pnlGame = new JPanel();
pnlGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.85;
c.weighty = 0.65;
contentPane.add(pnlGame, c);
JPanel pnlBuy = new JPanel();
c.gridx = 2;
pnlBuy.setBackground(Color.blue); //temp
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.15;
c.weighty = 0.46;
contentPane.add(pnlBuy, c);
JPanel pnlUpgrade = new JPanel();
pnlUpgrade.setBackground(Color.yellow); //temp
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.15;
c.weighty = 0.19;
contentPane.add(pnlUpgrade, c);
JPanel pnlStats = new JPanel();
pnlStats.setBackground(Color.red); //temp
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.61;
c.weighty = 0.35;
contentPane.add(pnlStats, c);
JPanel pnlSpeed = new JPanel();
pnlSpeed.setBackground(Color.orange); //temp
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.04;
contentPane.add(pnlSpeed, c);
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
Allora, cosa sto facendo di sbagliato? Scusa se il mio inglese è un po 'di merda e/o l'errore che sto commettendo è accecantemente ovvio ... sono 20-5 del mattino e ho avuto una lunga giornata. Probabilmente dovrebbe colpire il fieno, abbastanza a breve.
AGGIORNARE:
Sembra che se cambio la larghezza della griglia del pannello marrone/grigio, tutto sembra allineare correttamente, ma finisco con un brutto spazio nel mio layout. Qui:
i.imgur.com/6jux2.png
E il codice per il panel (incluso l'emendamento suggerito da Kevin S):
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
Quindi, c'è qualcosa che sto sbagliando, o questo è solo uno strano comportamento della gridbaglayout con cui dovrò vivere?
Sfortunatamente, grazie al montaggio a me, ho perso tutti gli incorporati che Bala r ha gentilmente messo lì. Quindi, siamo tornati ai link per le immagini, temo. E ora sembra che non posso pubblicare più di due collegamenti ipertestuali, quindi il link è stato ucciso nell'ultimo, devi copiarlo e incollarlo.
Grazie, Sam
Soluzione
Tutti i componenti nella colonna centrale sono almeno in un'altra colonna. Quindi GridBaglayout calcola la larghezza preferita della colonna centrale come 0, e questo è l'effetto che stai vedendo.
Se vuoi assicurarti che la tua colonna centrale abbia più larghezza, metti qualche componente lì che è solo in questa colonna.
Altri suggerimenti
Quindi sembra che tu debba sistemare i pnlrounds jpanel:
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1; // NEEDS TO BE 1 (NOT 2)
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
Sembra che il tuo problema sia che hai avuto c.gridx = 2
nel tuo codice quando c.gridx = 1
è ciò che dovrebbe essere davvero lì.
Inoltre, proprio come nota a margine, i pesi dei pannelli inferiori non si aggiungono a 1,00, aggiungono a .99. Ho solo pensato che dovresti sapere.
Non esattamente asoluzione per te ma più simile a una soluzione temporanea. Se aggiungi questo nuovo pannello
JPanel pnlTemp = new JPanel();
pnlTemp.setBackground(Color.pink); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlTemp, c);
Dopo il pannello grigio, corregge il layout. Non sono sicuro del perché, ma forse questo ti aiuterà fino a quando non sarai (o qualcun altro) in grado di capire la soluzione corretta. Questo nuovo pannello rosa non è visibile, ma aiuta solo a correggere il layout in qualche modo. Ecco come appare il layout dopo
Vuoi solo che il layout funzioni?
Ho fatto delle modifiche.
Container contentPane = form.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel pnlGame = new JPanel();
pnlGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 1; // one column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlGame, c);
// Added two new components
// pnlgGame and pnlggGame
// Covers up 2nd column and rows 0 and 1
JPanel pnlgGame = new JPanel();
pnlgGame.setBackground(Color.green); //temp
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 2; // two column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlgGame, c);
// Covers 2nd row and column 1
JPanel pnlggGame = new JPanel();
pnlggGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.20;
contentPane.add(pnlggGame, c);
JPanel pnlBuy = new JPanel();
pnlBuy.setBackground(Color.blue); //temp
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlBuy, c);
JPanel pnlUpgrade = new JPanel();
pnlUpgrade.setBackground(Color.yellow); //temp
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.20;
contentPane.add(pnlUpgrade, c);
JPanel pnlSpeed = new JPanel();
pnlSpeed.setBackground(Color.BLUE); //temp
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.05;
contentPane.add(pnlSpeed, c);
JPanel pnlStats = new JPanel();
pnlStats.setBackground(Color.red); //temp
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlStats, c);
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.35;
contentPane.add(pnlRounds, c);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setVisible(true);