下面列出了javax.swing.JComponent#doLayout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Image getComponentImage(JComponent component) {
// Initial component sizing & layout
component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
component.doLayout(); // layout component
// Correct component sizing & layout
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// One more iteration because of nested JTextAreas
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// Paint component into BufferedImage
BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
BufferedImage.TYPE_INT_RGB);
component.printAll(componentImage.getGraphics());
return componentImage;
}
private static void checkVisibility(JComponent comp) {
if (comp == null) return;
comp.invalidate();
comp.revalidate();
comp.doLayout();
comp.repaint();
for (Component c : comp.getComponents())
if (c.isVisible()) {
comp.setVisible(true);
return;
}
comp.setVisible(false);
}
private Image getComponentImage(JComponent component) {
// Initial component sizing & layout
component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
component.doLayout(); // layout component
// Correct component sizing & layout
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// One more iteration because of nested JTextAreas
component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
component.doLayout(); // layout component
// Paint component into BufferedImage
BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
BufferedImage.TYPE_INT_RGB);
component.printAll(componentImage.getGraphics());
return componentImage;
}
public static void setZoomingEnabled(JComponent chartUI, boolean enabled) {
SimpleXYChart chart = (SimpleXYChart)chartUI.getClientProperty("chart"); // NOI18N
if (chart.isZoomingEnabled() == enabled) return;
else chart.setZoomingEnabled(enabled);
JPanel sidePanel = (JPanel)chartUI.getClientProperty("sidePanel"); // NOI18N
JPanel scrollerPanel = (JPanel)chartUI.getClientProperty("scrollerPanel"); // NOI18N
if (enabled) {
TransparentToolBar toolbar = new TransparentToolBar(false);
for (Action action : chart.getActions()) toolbar.addItem(action);
sidePanel.add(toolbar);
JScrollBar scroller = chart.getScroller();
scroller.setSize(scroller.getPreferredSize());
scrollerPanel.add(scroller);
chart.putClientProperty("scroller", scroller); // NOI18N
} else {
sidePanel.removeAll();
scrollerPanel.removeAll();
chart.putClientProperty("scroller", null); // NOI18N
}
sidePanel.setVisible(enabled);
chartUI.doLayout();
chartUI.repaint();
}
public static void setLegendVisible(JComponent chartUI, boolean visible) {
JPanel legendPanel = (JPanel)chartUI.getClientProperty("legendPanel"); // NOI18N
legendPanel.setVisible(visible);
chartUI.doLayout();
chartUI.repaint();
}
/**
* Creates a MockComponent that resembles the argument component.
* <P>
* Note this method will traverse c and its subcomponents and may
* temporarily change properties of inner components: such as the focused
* state, the visibility, etc.
* <P>
* The goal is of this component is not to mirror the exact state of a
* component, but rather to provide a sample image of this component in its
* plain, unmodified, unused state.
*
* @param c
*/
public MockComponent(JComponent c) {
Dimension preferredSize = c.getPreferredSize();
Dimension currentSize = c.getSize();
Dimension d = new Dimension(Math.max(preferredSize.width,
currentSize.width), Math.max(preferredSize.height,
currentSize.height));
if (currentSize.width == 0 || currentSize.height == 0) {
// if the component isn't visible yet
c.setSize(d);
c.doLayout();
}
storeState(c);
image = new BufferedImage(d.width, d.height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, d.width, d.height);
g.setComposite(AlphaComposite.SrcOver);
c.paint(g);
g.dispose();
setPreferredSize(d);
setMinimumSize(d);
setMaximumSize(d);
setOpaque(c.isOpaque());
setName(c.getName());
setToolTipText(c.getToolTipText());
restoreState(c);
}
final void resizeContainer() {
JComponent c = ( JComponent ) container.getParent();
c.invalidate();
c.revalidate();
c.doLayout();
}