gpt4 book ai didi

java - 带有垂直滚动条的 GridBagLayout

转载 作者:行者123 更新时间:2023-11-29 05:40:43 24 4
gpt4 key购买 nike

我尝试使用 GridBagLayout 制作图表,我承认这非常困难,主要是因为它似乎不是实现该目的的最佳方式...

现在我尝试有一个垂直滚动条,这样我基本上可以在这个 GridBagLayout 中有任意多的行,但我找不到实现它的方法。我尝试了最直接(也可能是幼稚)的方式,但它看起来不起作用:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestCode2_Scrollbar {

public static void main(String[] args) {

JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 200);
window.setMinimumSize(new Dimension(350, 150));

JPanel panelMain = new JPanel(new BorderLayout());
window.add(panelMain);

//Comment the next 2 lines when using without scroll bar
JScrollPane scrollPanel = new JScrollPane();
panelMain.add(scrollPanel, BorderLayout.CENTER);

GridBagLayout innerLayout = new GridBagLayout();
GridBagConstraints innerConstraints = new GridBagConstraints();
JPanel innerPanel = new JPanel(innerLayout);
innerPanel.setBackground(new Color(0, 220, 250));

//Comment one of the 2 following lines:
scrollPanel.add(innerPanel);
//panelMain.add(innerPanel, BorderLayout.CENTER);

innerConstraints.weightx = 0.0;
innerConstraints.weighty = 0.0;
innerConstraints.gridy = 0;

innerConstraints.gridx = 0;
JLabel lblTitle = new JLabel("Title / ");
innerLayout.setConstraints(lblTitle, innerConstraints);
innerPanel.add(lblTitle);

innerConstraints.gridx = 1;
JLabel lblRating = new JLabel("Rating / ");
innerLayout.setConstraints(lblRating, innerConstraints);
innerPanel.add(lblRating);

innerConstraints.gridx = 2;
JLabel lblCategoryCode = new JLabel("Category code / ");
innerLayout.setConstraints(lblCategoryCode, innerConstraints);
innerPanel.add(lblCategoryCode);

innerConstraints.gridx = 3;
innerConstraints.weightx = 1.0;
innerConstraints.fill = GridBagConstraints.HORIZONTAL;
JLabel lblDescription = new JLabel("Description");
innerLayout.setConstraints(lblDescription, innerConstraints);
innerPanel.add(lblDescription);

int max = 4;

for(int i = 0; i < max; i++) {

innerConstraints.weightx = 0.0;
innerConstraints.fill = GridBagConstraints.NONE;
innerConstraints.anchor = GridBagConstraints.NORTHWEST;
innerConstraints.gridy = i + 1;

innerConstraints.gridx = 0;
lblTitle = new JLabel("" + i);
innerLayout.setConstraints(lblTitle, innerConstraints);
innerPanel.add(lblTitle);

innerConstraints.gridx = 1;
lblRating = new JLabel("" + i);
innerLayout.setConstraints(lblRating, innerConstraints);
innerPanel.add(lblRating);

innerConstraints.gridx = 2;
lblCategoryCode = new JLabel("" + i);
innerLayout.setConstraints(lblCategoryCode, innerConstraints);
innerPanel.add(lblCategoryCode);

innerConstraints.gridx = 3;
innerConstraints.weightx = 1.0;
innerConstraints.fill = GridBagConstraints.HORIZONTAL;
lblDescription = new JLabel("" + i);
innerLayout.setConstraints(lblDescription, innerConstraints);
innerPanel.add(lblDescription);

}

innerConstraints.gridy = max + 1;

JPanel innerVoidPanel = new JPanel();
innerVoidPanel.setBackground(new Color(0, 250, 250));
innerConstraints.weighty = 1.0;
innerConstraints.fill = GridBagConstraints.VERTICAL;
innerLayout.setConstraints(innerVoidPanel, innerConstraints);
innerPanel.add(innerVoidPanel);

//...

scrollPanel.add(innerPanel);
window.setVisible(true);
}

}

最佳答案

删除将 innerPanel 添加到滚动 Pane 的所有行。相反,在 main 方法的末尾添加这两行:

JScrollPane scrollPanel = new JScrollPane(innerPanel);
panelMain.add(scrollPanel, BorderLayout.CENTER);

完整代码:

public class TestCode2_Scrollbar {

public static void main(String[] args) {

JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 200);
window.setMinimumSize(new Dimension(350, 150));

JPanel panelMain = new JPanel(new BorderLayout());
window.add(panelMain);

//Comment the next 2 lines when using without scroll bar

GridBagLayout innerLayout = new GridBagLayout();
GridBagConstraints innerConstraints = new GridBagConstraints();
JPanel innerPanel = new JPanel(innerLayout);
innerPanel.setBackground(new Color(0, 220, 250));

//Comment one of the 2 following lines:
//panelMain.add(innerPanel, BorderLayout.CENTER);

innerConstraints.weightx = 0.0;
innerConstraints.weighty = 0.0;
innerConstraints.gridy = 0;

innerConstraints.gridx = 0;
JLabel lblTitle = new JLabel("Title / ");
innerLayout.setConstraints(lblTitle, innerConstraints);
innerPanel.add(lblTitle);

innerConstraints.gridx = 1;
JLabel lblRating = new JLabel("Rating / ");
innerLayout.setConstraints(lblRating, innerConstraints);
innerPanel.add(lblRating);

innerConstraints.gridx = 2;
JLabel lblCategoryCode = new JLabel("Category code / ");
innerLayout.setConstraints(lblCategoryCode, innerConstraints);
innerPanel.add(lblCategoryCode);

innerConstraints.gridx = 3;
innerConstraints.weightx = 1.0;
innerConstraints.fill = GridBagConstraints.HORIZONTAL;
JLabel lblDescription = new JLabel("Description");
innerLayout.setConstraints(lblDescription, innerConstraints);
innerPanel.add(lblDescription);

int max = 4;

for(int i = 0; i < max; i++) {

innerConstraints.weightx = 0.0;
innerConstraints.fill = GridBagConstraints.NONE;
innerConstraints.anchor = GridBagConstraints.NORTHWEST;
innerConstraints.gridy = i + 1;

innerConstraints.gridx = 0;
lblTitle = new JLabel("" + i);
innerLayout.setConstraints(lblTitle, innerConstraints);
innerPanel.add(lblTitle);

innerConstraints.gridx = 1;
lblRating = new JLabel("" + i);
innerLayout.setConstraints(lblRating, innerConstraints);
innerPanel.add(lblRating);

innerConstraints.gridx = 2;
lblCategoryCode = new JLabel("" + i);
innerLayout.setConstraints(lblCategoryCode, innerConstraints);
innerPanel.add(lblCategoryCode);

innerConstraints.gridx = 3;
innerConstraints.weightx = 1.0;
innerConstraints.fill = GridBagConstraints.HORIZONTAL;
lblDescription = new JLabel("" + i);
innerLayout.setConstraints(lblDescription, innerConstraints);
innerPanel.add(lblDescription);

}

innerConstraints.gridy = max + 1;

JPanel innerVoidPanel = new JPanel();
innerVoidPanel.setBackground(new Color(0, 250, 250));
innerConstraints.weighty = 1.0;
innerConstraints.fill = GridBagConstraints.VERTICAL;
innerLayout.setConstraints(innerVoidPanel, innerConstraints);
innerPanel.add(innerVoidPanel);

//...

JScrollPane scrollPanel = new JScrollPane(innerPanel);
panelMain.add(scrollPanel, BorderLayout.CENTER);

window.setVisible(true);
}

}

关于java - 带有垂直滚动条的 GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17695734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com