下面列出了javax.swing.JComponent#setBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void paintComponent(Graphics g) {
//Hack for issue 38132 - Beans are set via TTVBridge as a package
//private property on the parent PropertyPanel (if there is one).
//FindBeans() will locate the beans either in the model or as a
//property of the PropertyPanel (if an esoteric undocumented client property
//is set on the PropertyPanel). RendererFactory will set the env
//value (if there is an env) to the value of ReusablePropertyEnv.NODE
//(a performance hack to avoid creating 1 property env for each property
//painted each time we paint). Cool, huh?
reusableEnv.setNode(EditorPropertyDisplayer.findBeans(this));
JComponent comp = getRenderer(this);
prepareRenderer(comp);
comp.setBounds(0, 0, getWidth(), getHeight());
if (comp instanceof InplaceEditor) {
Component inner = findInnermostRenderer(comp);
SwingUtilities.paintComponent(g, comp, this, 0, 0, getWidth(), getHeight());
removeAll();
return;
}
comp.paint(g);
}
@Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
boolean hitBoundary = (f.getWidth() != newWidth || f.getHeight() != newHeight);
if (!inBounds((JInternalFrame) f, newX, newY, newWidth, newHeight)) {
Container parent = f.getParent();
Dimension parentSize = parent.getSize();
// Limit the unit window or tool windows to stay inside and never go outside of
// the desktop
// or always show up fully (never show up less than the full window)
int boundedX = (int) Math.min(Math.max(0, newX), parentSize.getWidth() - newWidth);
int boundedY = (int) Math.min(Math.max(0, newY), parentSize.getHeight() - 40);// newHeight);
if (f != null)
f.setBounds(boundedX, boundedY, newWidth, newHeight);
} else {
if (f != null)
f.setBounds(newX, newY, newWidth, newHeight);
}
if (hitBoundary) {
if (f != null)
f.validate();
}
}
protected void install() {
midanimation.acquireUninterruptibly();
try {
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
int width = getWidth() - insets.right - insets.left;
for (int a = 0; a < components.size(); a++) {
JComponent jc = components.get(a);
if (visibleComponents.contains(jc)) {
int h = heightMap.get(jc);
jc.setBounds(x, y, width, h);
jc.validate();
y += h;
jc.setVisible(true);
} else {
jc.setBounds(0, 0, 0, 0);
jc.setVisible(false);
}
}
} finally {
midanimation.release();
}
}
@Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
if (!(f instanceof JInternalFrame)) {
return;
}
boolean didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight);
if (!inBounds((JInternalFrame) f, newX, newY, newWidth, newHeight)) {
Container parent = f.getParent();
Dimension parentSize = parent.getSize();
int boundedX = (int) Math.min(Math.max(0, newX), parentSize.getWidth() - newWidth);
int boundedY = (int) Math.min(Math.max(0, newY), parentSize.getHeight() - newHeight);
f.setBounds(boundedX, boundedY, newWidth, newHeight);
} else {
f.setBounds(newX, newY, newWidth, newHeight);
}
if (didResize) {
f.validate();
}
}
public void dragExit(DropTargetEvent dte) {
if (draggingDefaults) {
return;
}
updateContents(getContents(new Point(-1000, -1000)));
if (draggingComponent != null) {
JComponent theComponent = getComponent(draggingComponent);
Rectangle r = getLayout().getDestinationMap(
CustomizedToolbar.this).get(theComponent);
if (r != null) {
theComponent.setBounds(r);
}
if (hideActiveComponents)
theComponent.setVisible(true);
}
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
protected void paintComponent(Graphics g) {
JTableHeader header = getHeader();
setupHeader(header);
TableCellRenderer renderer = header.getDefaultRenderer();
JComponent component = (JComponent)renderer.getTableCellRendererComponent(
getTable(), "", isSelected && isPressed, isFocusOwner(), -1, 0); // NOI18N
int height = header.getPreferredSize().height;
component.setBounds(0, 0, getWidth(), height);
component.setOpaque(false);
getPainter().paintComponent(g, component, null, 0, 0, getWidth(), height, false);
}
/**
* JComponent methods delegating to the hexmap layers
*/
public void setBounds(int x, int y, int width, int height) {
for (JComponent l : layers) {
l.setBounds(x, y, width, height);
}
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
/**
* Nudge a component towards the destination.
*
* @param c
* the component to nudge.
* @param dest
* the target bounds for the component.
* @return true when the component is at the desired location
*/
protected static boolean nudge(JComponent c, Rectangle dest) {
Rectangle bounds = c.getBounds();
double lastDX = getDouble(c, PROPERTY_LAST_DX, 0);
double lastDY = getDouble(c, PROPERTY_LAST_DY, 0);
double lastDW = getDouble(c, PROPERTY_LAST_DW, 0);
double lastDH = getDouble(c, PROPERTY_LAST_DH, 0);
double dx = dest.x - bounds.x;
double dy = dest.y - bounds.y;
double dw = dest.width - bounds.width;
double dh = dest.height - bounds.height;
dx = limit(.5 * sign(dx) * Math.pow(Math.abs(dx), .7) + .5 * lastDX, dx);
dy = limit(.5 * sign(dy) * Math.pow(Math.abs(dy), .7) + .5 * lastDY, dy);
dw = limit(.5 * sign(dw) * Math.pow(Math.abs(dw), .7) + .5 * lastDW, dw);
dh = limit(.5 * sign(dh) * Math.pow(Math.abs(dh), .7) + .5 * lastDH, dh);
c.putClientProperty(PROPERTY_LAST_DX, new Double(dx));
c.putClientProperty(PROPERTY_LAST_DY, new Double(dy));
c.putClientProperty(PROPERTY_LAST_DW, new Double(dw));
c.putClientProperty(PROPERTY_LAST_DH, new Double(dh));
if (Math.abs(dx) < 1.2 && Math.abs(dy) < 1.2 && Math.abs(dw) < 1.2
&& Math.abs(dh) < 1.2) {
c.setBounds(dest);
return true;
}
bounds.x += (int) (dx + .5);
bounds.y += (int) (dy + .5);
bounds.width += (int) (dw + .5);
bounds.height += (int) (dh + .5);
c.setBounds(bounds);
return false;
}
protected void paintComponent(Graphics g) {
JTableHeader header = getHeader();
setupHeader(header);
TableCellRenderer renderer = header.getDefaultRenderer();
JComponent component = (JComponent)renderer.getTableCellRendererComponent(
getTable(), "", isSelected && isPressed, isFocusOwner(), -1, 0); // NOI18N
int height = header.getPreferredSize().height;
component.setBounds(0, 0, getWidth(), height);
component.setOpaque(false);
getPainter().paintComponent(g, component, null, 0, 0, getWidth(), height, false);
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int labelWidth = 0;
for (Component comp : labels) {
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
}
int yPos = insets.top;
Iterator<Component> fieldIter = fields.listIterator();
Iterator<Component> labelIter = labels.listIterator();
while (labelIter.hasNext() && fieldIter.hasNext()) {
JComponent label = (JComponent) labelIter.next();
JComponent field = (JComponent) fieldIter.next();
int height = Math.max(label.getPreferredSize().height, field.
getPreferredSize().height);
label.setBounds(insets.left, yPos, labelWidth, height);
field.setBounds(insets.left + labelWidth + xGap,
yPos,
c.getSize().width - (labelWidth + xGap + insets.left
+ insets.right),
height);
yPos += (height + yGap);
}
}
/**
* Instructs the layout manager to perform the layout for the specified
* container.
*
* @param parent the parent
*/
@SuppressWarnings("deprecation")
public void layoutContainer(Container parent)
{
JRootPane root = (JRootPane) parent;
Rectangle b = root.getBounds();
Insets i = root.getInsets();
int nextY = 0;
int w = b.width - i.right - i.left;
int h = b.height - i.top - i.bottom;
if(root.getLayeredPane() != null)
{
root.getLayeredPane().setBounds(i.left, i.top, w, h);
}
if(root.getGlassPane() != null)
{
root.getGlassPane().setBounds(i.left, i.top, w, h);
}
// Note: This is laying out the children in the layeredPane,
// technically, these are not our children.
if (root.getWindowDecorationStyle() != JRootPane.NONE &&
(root.getUI() instanceof BERootPaneUI))
{
JComponent titlePane = ((BERootPaneUI)root.getUI()).
getTitlePane();
if (titlePane != null)
{
Dimension tpd = titlePane.getPreferredSize();
if (tpd != null)
{
int tpHeight = tpd.height;
titlePane.setBounds(0, 0, w, tpHeight);
nextY += tpHeight;
}
}
}
if(root.getMenuBar() != null
//* 该 行代码由Jack Jiang于2012-10-20增加:目的是为解决当
//* MebuBar被设置不可见时任然被错误地当作可视组件占据布局空间,这
//* 在BE LNF中的表现就是当menuBar不可见,它占据的那块空间将会是全透明
//* 的空白区。这个问题在Metal主题中仍然存在(就是设置JFrame.setDefaultLookAndFeelDecorated(true);
//* JDialog.setDefaultLookAndFeelDecorated(true);后的Metal主题状态),
//* 可能官方不认为这是个bug吧。
//* 为什么无论什么外观当在使用系统窗口边框类型时不会出现这样的情况呢?它
//* 可能是由于窗口外观的实现原理决定的吧(按理说是同一原理),有待深究!!!
&& root.getMenuBar().isVisible()
)
{
Dimension mbd = root.getMenuBar().getPreferredSize();
root.getMenuBar().setBounds(0, nextY, w, mbd.height);
nextY += mbd.height;
}
if(root.getContentPane() != null
//* 该 行代码由Jack Jiang于2012-10-20增加:目的是为解决与menubar在设置可见性时遇难到的一样的问题
&& root.getContentPane().isVisible()
)
{
Dimension cpd = root.getContentPane().getPreferredSize();
root.getContentPane().setBounds(0, nextY, w,
h < nextY ? 0 : h - nextY);
}
}
@Override
public final void dragGestureRecognized(DragGestureEvent dge) {
TreePath path = tree.getSelectionPath();
if (path != null) {
draggedNode = path.getLastPathComponent();
if (drawImage) {
Rectangle pathBounds = tree.getPathBounds(path); // getpathbounds
// of
// selectionpath
JComponent lbl = (JComponent) tree.getCellRenderer().getTreeCellRendererComponent(tree, draggedNode,
false, tree.isExpanded(path), tree.getModel().isLeaf(path.getLastPathComponent()), 0,
false);// returning the label
lbl.setBounds(pathBounds);// setting bounds to lbl
image = new BufferedImage(lbl.getWidth(), lbl.getHeight(),
java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE);// buffered
// image
// reference
// passing
// the
// label's
// ht
// and
// width
Graphics2D graphics = image.createGraphics();// creating
// the
// graphics
// for
// buffered
// image
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // Sets
// the
// Composite
// for
// the
// Graphics2D
// context
lbl.setOpaque(false);
lbl.paint(graphics); // painting the graphics to label
graphics.dispose();
}
dragSource.startDrag(dge, DragSource.DefaultMoveNoDrop, image, new Point(0, 0),
new TransferableNode(draggedNode), this);
}
}
/**
* Instructs the layout manager to perform the layout for the specified
* container.
*
* @param parent the Container for which this layout manager is being
* used
*/
public void layoutContainer(Container parent) {
JRootPane root = (JRootPane) parent;
Rectangle b = root.getBounds();
Insets i = root.getInsets();
int nextY = 0;
int w = b.width - i.right - i.left;
int h = b.height - i.top - i.bottom;
if (root.getLayeredPane() != null) {
root.getLayeredPane().setBounds(i.left, i.top, w, h);
}
if (root.getGlassPane() != null) {
root.getGlassPane().setBounds(i.left, i.top, w, h);
}
// Note: This is laying out the children in the layeredPane,
// technically, these are not our children.
if (root.getWindowDecorationStyle() != JRootPane.NONE && (root.getUI() instanceof SeaGlassRootPaneUI)) {
JComponent titlePane = ((SeaGlassRootPaneUI) root.getUI()).getTitlePane();
if (titlePane != null) {
Dimension tpd = titlePane.getPreferredSize();
if (tpd != null) {
int tpHeight = tpd.height;
titlePane.setBounds(0, 0, w, tpHeight);
nextY += tpHeight;
}
}
}
if (root.getJMenuBar() != null) {
boolean menuInTitle = (root.getClientProperty("JRootPane.MenuInTitle") == Boolean.TRUE);
Dimension mbd = root.getJMenuBar().getPreferredSize();
int x = menuInTitle? 20 : 0;
root.getJMenuBar().setBounds(x, menuInTitle ? 0 : nextY, w, mbd.height);
root.getJMenuBar().setOpaque(false);
root.getJMenuBar().setBackground(transparentColor);
if (!menuInTitle) {
nextY += mbd.height;
}
}
if (root.getContentPane() != null) {
/* Dimension cpd = */ root.getContentPane().getPreferredSize();
root.getContentPane().setBounds(0, nextY, w, h < nextY ? 0 : h - nextY);
}
}