[英]GridBagLayout content not aligning to the right of JPanel
我有一個JFrame(BorderLayout)在南部位置持有一個JPanel(GridBagLayout)。 JPanel邊框水平填滿屏幕(如我所願),其中的內容也填滿(我不想要)。
可視化要容易得多,所以我在Photoshop中做了用Java無法理解的工作...
我在photoshop中進行了此演示,以演示我想要發生的事情: 這是我的代碼產生的:
這是我正在使用的代碼:
private void loadTags(String filePath)
{
Map<String, ArrayList<String>> fileTags;
try
{
fileTags = PowerPointManipulator.getTagsFromFile(filePath);
}
catch (IOException e)
{
System.err.println("Could not open Powerpoint File");
e.printStackTrace();
return;
}
tag_listener = new TagButtonListener();
pnl_tags = new JPanel();
pnl_tags.setBackground(new Color(0, 0, 0, 0));
pnl_tags.setLayout(new GridBagLayout());
pnl_tags.setAlignmentX(LEFT_ALIGNMENT);
brd_tags = new TitledBorder("Tags");
brd_tags.setTitleColor(Color.WHITE);
brd_tags.setBorder(new LineBorder(Color.WHITE));
pnl_tags.setBorder(brd_tags);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(0, 0, 2, 15);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
int col = 0;
for (String key : fileTags.keySet())
{
ArrayList<String> vals = fileTags.get(key);
gbc.gridwidth = 2;
gbc.gridx = col;
gbc.gridy = 0;
JToggleButton tempButton = new JToggleButton(key);
tempButton.setOpaque(false);
tempButton.addActionListener(tag_listener);
tempButton.setFocusable(false);
pnl_tags.add(tempButton, gbc);
int row = 1;
for (String val : vals)
{
tempButton = new JToggleButton(val);
tempButton.setOpaque(false);
tempButton.addActionListener(tag_listener);
tempButton.setFocusable(false);
gbc.gridwidth = 1;
gbc.gridy = row;
pnl_tags.add(tempButton, gbc);
row++;
}
col += 2;
}
contentPane.add(pnl_tags, BorderLayout.PAGE_END);
}
如果刪除“權重”選項,那么我將獲得正確的布局,除了按鈕在JPanel中居中。
我覺得我已經很近了,但是我無法獲得正確的正確設置! 任何幫助深表感謝!
GridBagConstraints#weightx
和GridBagConstraints#weighty
將導致組件占據所有其他組件的布局后剩余的所有剩余空間, GridBagConstraints#fill
將使組件填充其所駐留的單元格的可用空間您提供的價值,
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
完全按照您的要求進行。
您可以嘗試類似...
List<String> tags = new ArrayList<>(25);
tags.add("example");
tags.add("objective");
tags.add("motivation");
tags.add("summary");
tags.add("c");
tags.add("*");
tags.add("*");
tags.add("*");
tags.add("cs");
JPanel tagPane = new JPanel(new GridBagLayout());
tagPane.setBorder(new TitledBorder("Tags"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
for (String tag : tags) {
tagPane.add(new JToggleButton(tag), gbc);
gbc.gridx--;
if (gbc.gridx < 0) {
gbc.gridx = 3;
gbc.gridy++;
}
}
導致類似...
好吧,這樣會更好一些,但是它們被分組在中間!
好吧,您可以設置它,以便每個右側列的weightx
為1
,例如...
gbc.gridx--;
if (gbc.gridx < 0) {
gbc.gridx = 3;
gbc.gridy++;
gbc.weightx = 1;
} else {
gbc.weightx = 0;
}
或在所有其他組件的右側添加“填充器”組件。
for (String tag : tags) {
tagPane.add(new JToggleButton(tag), gbc);
gbc.gridx--;
if (gbc.gridx < 0) {
gbc.gridx = 3;
gbc.gridy++;
}
}
JLabel filler = new JLabel();
gbc.gridx = 4;
gbc.gridy = 0;
gbc.weightx = 1;
tagPane.add(filler, gbc);
無論哪種方式,您最終都會得到類似...的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.