問題:
JPanelwithGridBagLayout包含兩個JScrollPane組件,頂部一個包含JTextArea,底部一個包含JTable。我希望這種設定可以使組件流暢地填充容器JPanel并在調整大小時均勻地共享它的垂直區域。實際發生的是頂部JScrollPane組件在調整大小時垂直收縮,為底部JScrollPane組件提供更多垂直區域以進行侵入;這發生在某些尺寸上,我不知道它們是否是隨機的。
重要筆記:
我已經申請GridBagConstraints#weighty = 0.5;了這兩個組件,這應該可以完成這項作業。
我注意到問題是由于 的存在JTextArea,因為如果我切換位置;使JScrollPane/JTextArea在底側和JScrollPane/JTable頂側; 底部組件JScrollPane/JTextArea垂直收縮,為頂部組件提供更多垂直區域以JScrollPane/JTable進行侵入。
我知道這個確切的情況可以簡單地使用GridLayout(2,1)而不是GridBagLayout(或者可能是其他解決方案)來解決,但是我很GridBagLayout喜歡并且我想使用其中的大部分。此外,如果我想添加三個組件而不是兩個組件,并且我想讓它們按百分比共享垂直區域,例如 (%, %, 50%) 我可以使用GridBagConstraints#weighty = (0.25, 0.25, 0.5)它很容易適用于GridBagLayout.
那么,在這種情況下,如何使組件在調整大小時均勻地共享JPanel容器的垂直區域GridBagLayout?或者,換句話說;如何強制組件遵守GridBagConstraints#weighty = 0.5;適用于兩者的調整大小設定?
SSCCE格式的代碼:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
public class GridBagFluidFill {
private static JPanel createFormPanel() {
JPanel containerPanel = new JPanel(new GridBagLayout());
containerPanel.setPreferredSize(new Dimension(350, 200));
JScrollPane scrollableTextArea;
JScrollPane scrollableTable;
JTextArea textArea = new JTextArea();
scrollableTextArea = new JScrollPane(textArea);
DefaultTableModel model = new DefaultTableModel(new String[]{"Column1", "Column2", "Column3"}, 0);
JTable table = new JTable(model);
scrollableTable = new JScrollPane(table);
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0;
c.weightx = 1.0;
// I expect this to reserve 50% of the height all time.
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.PAGE_START;
containerPanel.add(scrollableTextArea, c);
c = new GridBagConstraints();
c.gridy = 1;
c.weightx = 1.0;
// I expect this to reserve 50% of the height all time.
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.PAGE_END;
containerPanel.add(scrollableTable, c);
return containerPanel;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
JPanel formPanel = createFormPanel();
JFrame frame = new JFrame("GridBagFluidFill");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(formPanel);
frame.pack();
frame.setVisible(true);
});
}
}
uj5u.com熱心網友回復:
GridBagLayout 很復雜,雖然它的檔案確實描述了它的所有功能,但其中一些很容易被誤解。
我已經申請
GridBagConstraints#weighty = 0.5;了這兩個組件,這應該可以完成這項作業。
這不是 weightx 和 weighty 所做的。
相等的 weightx 和 weighty 值不會強制組件具有相同的寬度或高度。當容器大于每個組件的首選尺寸時,權重值決定了額外空間的分布。
這是一個演示這一點的程式:
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class GridBagEqualWeightsDemo {
static void showWindow() {
// Source: https://www.gutenberg.org/files/98/98-h/98-h.htm
String text =
"It was the best of times, it was the worst of times, it was"
" the age of wisdom, it was the age of foolishness, it was the"
" epoch of belief, it was the epoch of incredulity, it was the"
" season of Light, it was the season of Darkness, it was the"
" spring of hope, it was the winter of despair, we had everything"
" before us, we had nothing before us, we were all going direct"
" to Heaven, we were all going direct the other way—in short,"
" the period was so far like the present period, that some of its"
" noisiest authorities insisted on its being received, for good"
" or for evil, in the superlative degree of comparison only.";
JTextArea topArea = new JTextArea(text, 4, 20);
topArea.setLineWrap(true);
topArea.setWrapStyleWord(true);
JTextArea bottomArea = new JTextArea(text, 8, 20);
bottomArea.setLineWrap(true);
bottomArea.setWrapStyleWord(true);
JScrollPane top = new JScrollPane(topArea);
JScrollPane bottom = new JScrollPane(bottomArea);
top.setMinimumSize(top.getPreferredSize());
bottom.setMinimumSize(bottom.getPreferredSize());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 0.5;
panel.add(top, gbc);
panel.add(bottom, gbc);
JFrame frame = new JFrame("GridBagConstraints Equal Weights");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> showWindow());
}
}
如您所見,這兩個組件以不同的首選高度開始,因此 GridBagLayout 永遠不會為它們提供相同的高度。當您使視窗變高時,由于相對差異較小,組件各自的高度看起來更相似,但它們永遠不會相同。
GridBagLayout 僅使用 weighty 來確定如何分配額外空間——即超過每個組件(不同)首選高度的容器高度。如果面板比首選高度高 50 個像素(根據每個子組件的首選大小計算),那么相等的weighty值意味著每個組件的布局將在其各自的首選高度上增加 25 個像素。
如果您想強制可能具有不同首選大小的兩個組件始終具有相同的寬度和/或高度,則 GridBagLayout 是該作業的錯誤工具。正如您所提到的,GridLayout 非常適合于此。 SpringLayout也可以做到這一點,雖然它的使用很復雜。
您可以自由(并鼓勵!)在彼此內部使用具有不同布局的多個面板。
uj5u.com熱心網友回復:
我已經應用了 GridBagConstraints#weighty = 0.5; 到這兩個組件,這應該可以完成這項作業。
該weighty約束用于告訴 GridBagLayout 如何分配“額外”(顯然更少)的空間。
也就是說,每個組件首先被分配其首選空間。然后,如果有額外(或明顯更少)可用空間,則使用該約束。
如果您添加除錯代碼以顯示每個滾動窗格的首選大小,您將獲得:
java.awt.Dimension[width=223,height=19] // text area
java.awt.Dimension[width=453,height=403] // table
所以問題在于這個宣告:
containerPanel.setPreferredSize(new Dimension(350, 200));
您正在強制組件小于其首選大小。所以看起來 GridBagLayout 為每個分配了 50%。
我想知道為什么 JTextArea 無緣無故地縮小了
隨著框架高度的增加,每個組件被分配 50%,直到框架足夠大以按其首選大小顯示每個組件。這就是為什么文本區域縮小而表格跳躍的原因。
當您從那里增加高度時,額外的空間按預期分配為 50/50。
如果取消注釋 setPreferredSize() 陳述句,您將看到自然的布局,并且跳轉的原因變得清晰。
我想讓它們按百分比共享垂直區域,例如 (%, %, 50%)
如果您想要這樣的分配,請查看為此目的設計的相對布局。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/374381.html
