下面列出了java.awt.Rectangle#getSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Point getPopupLocation() {
Dimension popupSize = new Dimension((int) this.comboBox.getSize().getWidth(), (int) this.comboBox.getSize()
.getHeight());
Insets insets = getInsets();
popupSize.setSize(popupSize.width - (insets.right + insets.left),
getPopupHeightForRowCount(this.comboBox.getMaximumRowCount()));
Rectangle popupBounds = computePopupBounds(0, this.comboBox.getBounds().height, popupSize.width,
popupSize.height);
Dimension scrollSize = popupBounds.getSize();
Point popupLocation = popupBounds.getLocation();
this.scroller.setMaximumSize(scrollSize);
this.scroller.setPreferredSize(scrollSize);
this.scroller.setMinimumSize(scrollSize);
this.list.revalidate();
return popupLocation;
}
/**
* Calculates the upper left location of the Popup.
*
* @return the Point representing the upper-left coordinate of the Popup.
*/
private Point getPopupLocation() {
Dimension popupSize = comboBox.getSize();
Insets insets = getInsets();
// reduce the width of the scrollpane by the insets so that the popup
// is the same width as the combo box.
popupSize.setSize(popupSize.width - (insets.right + insets.left), getPopupHeightForRowCount(getMaximumRowCount()));
Rectangle popupBounds = computePopupBounds(0, comboBox.getBounds().height, popupSize.width, popupSize.height);
Dimension scrollSize = popupBounds.getSize();
Point popupLocation = popupBounds.getLocation();
scroller.setMaximumSize(scrollSize);
scroller.setPreferredSize(scrollSize);
scroller.setMinimumSize(scrollSize);
list.revalidate();
return popupLocation;
}
/**
* Returns the preferred size of the button.
*
* @param b an instance of {@code AbstractButton}
* @param textIconGap a gap between text and icon
* @return the preferred size of the button
*/
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
if(b.getComponentCount() > 0) {
return null;
}
Icon icon = b.getIcon();
String text = b.getText();
Font font = b.getFont();
FontMetrics fm = b.getFontMetrics(font);
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
SwingUtilities.layoutCompoundLabel(
b, fm, text, icon,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
viewR, iconR, textR, (text == null ? 0 : textIconGap)
);
/* The preferred size of the button is the size of
* the text and icon rectangles plus the buttons insets.
*/
Rectangle r = iconR.union(textR);
Insets insets = b.getInsets();
r.width += insets.left + insets.right;
r.height += insets.top + insets.bottom;
return r.getSize();
}
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
if(b.getComponentCount() > 0) {
return null;
}
Icon icon = b.getIcon();
String text = b.getText();
Font font = b.getFont();
FontMetrics fm = b.getFontMetrics(font);
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
SwingUtilities.layoutCompoundLabel(
b, fm, text, icon,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
viewR, iconR, textR, (text == null ? 0 : textIconGap)
);
/* The preferred size of the button is the size of
* the text and icon rectangles plus the buttons insets.
*/
Rectangle r = iconR.union(textR);
Insets insets = b.getInsets();
r.width += insets.left + insets.right;
r.height += insets.top + insets.bottom;
return r.getSize();
}
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
if(b.getComponentCount() > 0) {
return null;
}
Icon icon = b.getIcon();
String text = b.getText();
Font font = b.getFont();
FontMetrics fm = b.getFontMetrics(font);
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
SwingUtilities.layoutCompoundLabel(
b, fm, text, icon,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
viewR, iconR, textR, (text == null ? 0 : textIconGap)
);
/* The preferred size of the button is the size of
* the text and icon rectangles plus the buttons insets.
*/
Rectangle r = iconR.union(textR);
Insets insets = b.getInsets();
r.width += insets.left + insets.right;
r.height += insets.top + insets.bottom;
return r.getSize();
}
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
if(b.getComponentCount() > 0) {
return null;
}
Icon icon = b.getIcon();
String text = b.getText();
Font font = b.getFont();
FontMetrics fm = b.getFontMetrics(font);
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
SwingUtilities.layoutCompoundLabel(
b, fm, text, icon,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
viewR, iconR, textR, (text == null ? 0 : textIconGap)
);
/* The preferred size of the button is the size of
* the text and icon rectangles plus the buttons insets.
*/
Rectangle r = iconR.union(textR);
Insets insets = b.getInsets();
r.width += insets.left + insets.right;
r.height += insets.top + insets.bottom;
return r.getSize();
}
/**
* Translates the given rectangle from one window to another.
* Returns null if the translation is failed
*/
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
if (translatedLoc == null)
{
return null;
}
else
{
return new Rectangle(translatedLoc, r.getSize());
}
}
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
if(b.getComponentCount() > 0) {
return null;
}
Icon icon = b.getIcon();
String text = b.getText();
Font font = b.getFont();
FontMetrics fm = b.getFontMetrics(font);
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
SwingUtilities.layoutCompoundLabel(
b, fm, text, icon,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
viewR, iconR, textR, (text == null ? 0 : textIconGap)
);
/* The preferred size of the button is the size of
* the text and icon rectangles plus the buttons insets.
*/
Rectangle r = iconR.union(textR);
Insets insets = b.getInsets();
r.width += insets.left + insets.right;
r.height += insets.top + insets.bottom;
return r.getSize();
}
/**
* Translates the given rectangle from one window to another.
* Returns null if the translation is failed
*/
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
if (translatedLoc == null)
{
return null;
}
else
{
return new Rectangle(translatedLoc, r.getSize());
}
}
/**
* Returns a point where the given popup menu should be shown. The point is
* calculated by adjusting the X and Y coordinates so that the popup menu
* will not be clipped by the screen boundaries.
*
* @param popup
* the popup menu
* @param x
* the x position in screen coordinate
* @param y
* the y position in screen coordinates
* @return the point where the popup menu should be shown in screen
* coordinates
*/
public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) {
Dimension sizeMenu = popup.getPreferredSize();
Point bottomRightMenu = new Point(x + sizeMenu.width, y
+ sizeMenu.height);
Rectangle[] screensBounds = getScreenBounds();
int n = screensBounds.length;
for (int i = 0; i < n; i++) {
Rectangle screenBounds = screensBounds[i];
if (screenBounds.x <= x
&& x <= (screenBounds.x + screenBounds.width)) {
Dimension sizeScreen = screenBounds.getSize();
sizeScreen.height -= 32; // Hack to help prevent menu being
// clipped by Windows/Linux/Solaris
// Taskbar.
int xOffset = 0;
if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width))
xOffset = -sizeMenu.width;
int yOffset = 0;
if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height))
yOffset = sizeScreen.height - bottomRightMenu.y;
return new Point(x + xOffset, y + yOffset);
}
}
return new Point(x, y); // ? that would mean that the top left point was
// not on any screen.
}
/**
* Create and display the popup at the given bounds.
*
* @param popupBounds location and size of the popup.
* @param displayAboveCaret whether the popup is displayed above the anchor
* bounds or below them (it does not be right above them).
*/
private void show(Rectangle popupBounds, boolean displayAboveCaret) {
// Hide the original popup if exists
if (popup != null) {
popup.hide();
popup = null;
}
// Explicitly set the preferred size
Dimension origPrefSize = getPreferredSize();
Dimension newPrefSize = popupBounds.getSize();
JComponent contComp = getContentComponent();
if (contComp == null){
return;
}
contComp.setPreferredSize(newPrefSize);
showRetainedPreferredSize = newPrefSize.equals(origPrefSize);
PopupFactory factory = PopupFactory.getSharedInstance();
// Lightweight completion popups don't work well on the Mac - trying
// to click on its scrollbars etc. will cause the window to be hidden,
// so force a heavyweight parent by passing in owner==null. (#96717)
JTextComponent owner = layout.getEditorComponent();
if(owner != null && owner.getClientProperty("ForceHeavyweightCompletionPopup") != null) {
owner = null;
}
// #76648: Autocomplete box is too close to text
if(displayAboveCaret && Utilities.isMac()) {
popupBounds.y -= 10;
}
popup = factory.getPopup(owner, contComp, popupBounds.x, popupBounds.y);
popup.show();
this.popupBounds = popupBounds;
this.displayAboveCaret = displayAboveCaret;
}
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
if(b.getComponentCount() > 0) {
return null;
}
Icon icon = b.getIcon();
String text = b.getText();
Font font = b.getFont();
FontMetrics fm = b.getFontMetrics(font);
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
SwingUtilities.layoutCompoundLabel(
b, fm, text, icon,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
viewR, iconR, textR, (text == null ? 0 : textIconGap)
);
/* The preferred size of the button is the size of
* the text and icon rectangles plus the buttons insets.
*/
Rectangle r = iconR.union(textR);
Insets insets = b.getInsets();
r.width += insets.left + insets.right;
r.height += insets.top + insets.bottom;
return r.getSize();
}
/**
* Translates the given rectangle from one window to another.
* Returns null if the translation is failed
*/
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
if (translatedLoc == null)
{
return null;
}
else
{
return new Rectangle(translatedLoc, r.getSize());
}
}
/**
* Translates the given rectangle from one window to another.
* Returns null if the translation is failed
*/
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
if (translatedLoc == null)
{
return null;
}
else
{
return new Rectangle(translatedLoc, r.getSize());
}
}
/**
* Translates the given rectangle from one window to another.
* Returns null if the translation is failed
*/
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
if (translatedLoc == null)
{
return null;
}
else
{
return new Rectangle(translatedLoc, r.getSize());
}
}
/**
* Translates the given rectangle from one window to another.
* Returns null if the translation is failed
*/
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
if (translatedLoc == null)
{
return null;
}
else
{
return new Rectangle(translatedLoc, r.getSize());
}
}
@Override
public Dimension render(Graphics2D graphics)
{
if (image == null)
{
return new Dimension();
}
graphics.setFont(getSize() < DEFAULT_SIZE ? FontManager.getRunescapeSmallFont() : FontManager.getRunescapeFont());
final int baseX = preferredLocation.x;
final int baseY = preferredLocation.y;
// Calculate dimensions
final FontMetrics metrics = graphics.getFontMetrics();
final int size = getSize();
final Rectangle bounds = new Rectangle(baseX, baseY, size, size);
// Render background
final BackgroundComponent backgroundComponent = new BackgroundComponent();
backgroundComponent.setBackgroundColor(backgroundColor);
backgroundComponent.setRectangle(bounds);
backgroundComponent.render(graphics);
// Render image
graphics.drawImage(
image,
baseX + (size - image.getWidth(null)) / 2,
baseY + (size - image.getHeight(null)) / 2,
null);
// Render caption
if (!Strings.isNullOrEmpty(text))
{
final TextComponent textComponent = new TextComponent();
textComponent.setColor(color);
textComponent.setText(text);
textComponent.setPosition(new Point(baseX + ((size - metrics.stringWidth(text)) / 2), baseY + size - SEPARATOR));
textComponent.render(graphics);
}
this.bounds.setBounds(bounds);
return bounds.getSize();
}
/**
* Creates the popup portion of the combo box.
*
* @return an instance of <code>ComboPopup</code>
* @see ComboPopup
*/
protected ComboPopup createPopup() {
return new BasicComboPopup( comboBox ){
/** popupOffsetX是jb2011自定的属性,用于修正下拉框的弹出窗的X坐标 */
private int popupOffsetX = UIManager.getInt("ComboBox.popupOffsetX");
/** popupOffsetY是jb2011自定的属性,用于修正下拉框的弹出窗的Y坐标 */
private int popupOffsetY = UIManager.getInt("ComboBox.popupOffsetY");
//* copy from parent and modified by Jack Jiang
/**
* Implementation of ComboPopup.show().
*/
public void show() {
setListSelection(comboBox.getSelectedIndex());
Point location = getPopupLocation();
show( comboBox
//以下x、y坐标修正代码由Jack Jiang增加
, location.x + popupOffsetX //*~ popupOffsetX是自定属性,用于修改弹出窗的X坐标
, location.y + popupOffsetY //*~ popupOffsetY是自定属性,用于修改弹出窗的Y坐标
);
}
//* copy from parent and no modified
/**
* Sets the list selection index to the selectedIndex. This
* method is used to synchronize the list selection with the
* combo box selection.
*
* @param selectedIndex the index to set the list
*/
private void setListSelection(int selectedIndex) {
if ( selectedIndex == -1 ) {
list.clearSelection();
}
else {
list.setSelectedIndex( selectedIndex );
list.ensureIndexIsVisible( selectedIndex );
}
}
//* copy from parent and no modified
/**
* Calculates the upper left location of the Popup.
*/
private Point getPopupLocation() {
Dimension popupSize = comboBox.getSize();
Insets insets = getInsets();
// reduce the width of the scrollpane by the insets so that the popup
// is the same width as the combo box.
popupSize.setSize(popupSize.width - (insets.right + insets.left),
getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
Rectangle popupBounds = computePopupBounds( 0, comboBox.getBounds().height,
popupSize.width, popupSize.height);
Dimension scrollSize = popupBounds.getSize();
Point popupLocation = popupBounds.getLocation();
scroller.setMaximumSize( scrollSize );
scroller.setPreferredSize( scrollSize );
scroller.setMinimumSize( scrollSize );
list.revalidate();
return popupLocation;
}
};
}
private void setSizeAndDimensions(@NotNull JTable table,
@NotNull JBPopup popup,
@NotNull RelativePoint popupPosition,
@NotNull List<UsageNode> data) {
JComponent content = popup.getContent();
Window window = SwingUtilities.windowForComponent(content);
Dimension d = window.getSize();
int width = calcMaxWidth(table);
width = (int)Math.max(d.getWidth(), width);
Dimension headerSize = ((AbstractPopup)popup).getHeaderPreferredSize();
width = Math.max((int)headerSize.getWidth(), width);
width = Math.max(myWidth, width);
if (myWidth == -1) myWidth = width;
int newWidth = Math.max(width, d.width + width - myWidth);
myWidth = newWidth;
int rowsToShow = Math.min(30, data.size());
Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
dimension = rectangle.getSize();
Point location = window.getLocation();
if (!location.equals(rectangle.getLocation())) {
window.setLocation(rectangle.getLocation());
}
if (!data.isEmpty()) {
TableScrollingUtil.ensureSelectionExists(table);
}
table.setSize(dimension);
//table.setPreferredSize(dimension);
//table.setMaximumSize(dimension);
//table.setPreferredScrollableViewportSize(dimension);
Dimension footerSize = ((AbstractPopup)popup).getFooterPreferredSize();
int newHeight = (int)(dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4/* invisible borders, margins etc*/;
Dimension newDim = new Dimension(dimension.width, newHeight);
window.setSize(newDim);
window.setMinimumSize(newDim);
window.setMaximumSize(newDim);
window.validate();
window.repaint();
table.revalidate();
table.repaint();
}
private Point getPopupLocation()
{
Dimension popupSize = comboBox.getSize();
Insets insets = getInsets();
// reduce the width of the scrollpane by the insets so that the popup
// is the same width as the combo box.
popupSize.setSize(popupSize.width - (insets.right + insets.left),
getPopupHeightForRowCount(comboBox.getMaximumRowCount()));
Rectangle popupBounds = computePopupBounds(0,
comboBox.getBounds().height, popupSize.width, popupSize.height);
Dimension scrollSize = popupBounds.getSize();
Point popupLocation = popupBounds.getLocation();
scroller.setMaximumSize(scrollSize);
scroller.setPreferredSize(scrollSize);
scroller.setMinimumSize(scrollSize);
list.revalidate();
return popupLocation;
}