这是我处理组件的功能,它可以添加到我的面板中。
public void addComp(JPanel thePanel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight, int place, int stretch){
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = compWidth;
gridConstraints.gridheight = compHeight;
gridConstraints.weightx = 1;
//gridConstraints.weighty = 1;
gridConstraints.insets = new Insets(5,5,5,5);
gridConstraints.anchor = place;
gridConstraints.fill = stretch;
thePanel.add(comp, gridConstraints);
}
JPanel contPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel elementBorder = new JPanel(new BorderLayout());
elementBorder.setBorder(new TitledBorder("Element Ansicht"));
JPanel buttonBorder = new JPanel(new BorderLayout());
buttonBorder.setBorder(new TitledBorder("Funktionen"));
JPanel mixedPanel = new JPanel(new GridBagLayout());
JPanel leftPanel = new JPanel(new FlowLayout());
addWinButt = new JButton("hinzuf\u00fcgen");
delWinButt = new JButton("l\u00f6schen");
editWinButt = new JButton("bearbeiten");
listButt = new JButton("Material-Liste")
winViewPanel = new JPanel(new GridBagLayout());
现在我把我想要的组件放到Panel中,它将被提供给JScrollPane
addComp(winViewPanel, new JLabel("PosNummer"), 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
addComp(winViewPanel, new JLabel("Breite"), 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
addComp(winViewPanel, new JLabel("H\u00f6he"), 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
addComp(winViewPanel, new JLabel("Anzahl"), 3, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
winselectedMap = new HashMap<String, JCheckBox>();
for(int i = 0; i < selAssign.fgetArrayList().size();i++){
addComp(winViewPanel, new JLabel(selAssign.fgetArrayList().get(i).getPosNr()), 0, (i+1), 1, 0, GridBagConstraints.NORTH, GridBagConstraints.NONE);
addComp(winViewPanel, new JLabel("" + (int)selAssign.fgetArrayList().get(i).getTotalWidth()), 1, (i+1), 1, 0, GridBagConstraints.NORTH, GridBagConstraints.NONE);
addComp(winViewPanel, new JLabel("" + (int)selAssign.fgetArrayList().get(i).getTotalHeight()), 2, (i+1), 1, 0, GridBagConstraints.NORTH, GridBagConstraints.NONE);
addComp(winViewPanel, new JLabel("" + selAssign.fgetArrayList().get(i).getWindowCount()), 3, (i+1), 1, 0, GridBagConstraints.NORTH, GridBagConstraints.NONE);
JCheckBox box = new JCheckBox();
box.setName("checkselWinBox" + i);
winselectedMap.put(box.getName(), box);
addComp(winViewPanel, box, 4, (i+1), 1, 1, GridBagConstraints.NORTH, GridBagConstraints.NONE);
}
JScrollPane scrollPane = new JScrollPane(winViewPanel);
scrollPane.setBorder(null);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize( new Dimension(500, 600) );
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
buttonBorder.add(leftPanel);
elementBorder.add(scrollPane, BorderLayout.NORTH);
addComp(mixedPanel, buttonBorder, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
addComp(mixedPanel, elementBorder, 0, 1, 1, 0, GridBagConstraints.NORTH, GridBagConstraints.NONE);
contPanel.add(mixedPanel);
我试图在JScrollPane中设置我的组件,因为某些原因它将每个组件放入SCrollPane的中心,而不是从顶部开始。