下面列出了怎么用javax.swing.Spring的API类实例代码及写法,或者点击链接到github查看源代码。
private Spring getBestWidth(Container parent, int rows, int cols) {
Spring x = Spring.constant(0);
Spring width = Spring.constant(0);
for (int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
}
}
return width;
}
private void makeProgressBars(SpringLayout l) {
JComponent prevLabel = null, prevBar = null;
bars = new JProgressBar[SummaryItem.STATES.length];
SpringLayout ls = new SpringLayout();
JPanel appSummary = new JPanel();
appSummary.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "App Summary"));
add(appSummary);
l.putConstraint(SpringLayout.WEST, appSummary, 5, SpringLayout.WEST, this);
l.putConstraint(SpringLayout.EAST, appSummary, -5, SpringLayout.EAST, this);
l.putConstraint(SpringLayout.NORTH, appSummary, 25, SpringLayout.NORTH, this);
appSummary.setLayout(ls);
for (int i = 0; i < SummaryItem.STATES.length; i++) {
JLabel label = new JLabel(SummaryItem.STATES[i] + ":");
appSummary.add(label);
JProgressBar lb = new JProgressBar();
bars[i] = lb;
lb.setString("0");
lb.setStringPainted(true);
appSummary.add(lb);
fixEdges(ls, label, lb, appSummary);
if (prevLabel == null) {
ls.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, appSummary);
}
else {
ls.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.SOUTH, prevLabel);
}
prevLabel = label;
prevBar = lb;
}
JLabel hl = new JLabel("Heap:");
memory = makeProgress(l, hl);
l.putConstraint(SpringLayout.SOUTH, appSummary, -25, SpringLayout.NORTH, hl);
l.putConstraint(SpringLayout.SOUTH, hl, -25, SpringLayout.SOUTH, this);
Spring maxW = Spring.constant(0);
for (int i = 0; i < SummaryItem.STATES.length; i++) {
maxW = Spring.max(maxW, ls.getConstraints(appSummary.getComponent(i * 2)).getWidth());
}
for (int i = 0; i < SummaryItem.STATES.length; i++) {
SpringLayout.Constraints c = ls.getConstraints(appSummary.getComponent(i * 2));
c.setWidth(maxW);
}
}