下面列出了javax.swing.border.Border#getBorderInsets ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void paint(final Graphics g) {
super.paint(g);
final Border border = getBorder();
int realLeft = 0;
int realWidth = getWidth();
if (border != null) {
final Insets insets = border.getBorderInsets(this);
realWidth -= insets.right;
realWidth -= insets.left;
realLeft += insets.left;
}
g.drawLine(realLeft, getHeight() - 2, realWidth, getHeight() - 2);
}
private static void test(Border border) {
Insets actual = border.getBorderInsets(null);
if (NEGATIVE.equals(actual)) {
throw new Error("unexpected insets in " + border.getClass());
}
Insets expected = (Insets) actual.clone();
// modify
actual.top++;
actual.left++;
actual.right++;
actual.bottom++;
// validate
if (!expected.equals(border.getBorderInsets(null))) {
throw new Error("shared insets in " + border.getClass());
}
}
private static void test(Border border) {
Insets actual = border.getBorderInsets(null);
if (NEGATIVE.equals(actual)) {
throw new Error("unexpected insets in " + border.getClass());
}
Insets expected = (Insets) actual.clone();
// modify
actual.top++;
actual.left++;
actual.right++;
actual.bottom++;
// validate
if (!expected.equals(border.getBorderInsets(null))) {
throw new Error("shared insets in " + border.getClass());
}
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setComponentOrientation(list.getComponentOrientation());
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
if( cellHasFocus ) {
Border b = UIManager.getBorder("List.focusCellHighlightBorder"); //NOI18N
if( null != b && null != b.getBorderInsets(this) )
setBorder( b );
} else {
setBorder( noFocusBorder);
}
setText((value == null) ? "" : value.toString());
setSelected(((CheckListModel) list.getModel()).isChecked(index));
return this;
}
/**
* Damages the area surrounding the caret to cause
* it to be repainted in a new location. If paint()
* is reimplemented, this method should also be
* reimplemented. This method should update the
* caret bounds (x, y, width, and height).
*
* @param r the current location of the caret
* @see #paint
*/
protected synchronized void damage(final Rectangle r) {
if (r == null || fPainting) return;
x = r.x - 4;
y = r.y;
width = 10;
height = r.height;
// Don't damage the border area. We can't paint a partial border, so get the
// intersection of the caret rectangle and the component less the border, if any.
final Rectangle caretRect = new Rectangle(x, y, width, height);
final Border border = getComponent().getBorder();
if (border != null) {
final Rectangle alloc = getComponent().getBounds();
alloc.x = alloc.y = 0;
final Insets borderInsets = border.getBorderInsets(getComponent());
alloc.x += borderInsets.left;
alloc.y += borderInsets.top;
alloc.width -= borderInsets.left + borderInsets.right;
alloc.height -= borderInsets.top + borderInsets.bottom;
Rectangle2D.intersect(caretRect, alloc, caretRect);
}
x = caretRect.x;
y = caretRect.y;
width = Math.max(caretRect.width, 1);
height = Math.max(caretRect.height, 1);
repaint();
}
void test(String uiName) {
Border b = UIManager.getBorder(uiName);
Insets i = b.getBorderInsets(null);
if (i == null) {
throw new RuntimeException("getBorderInsets() returns null for " + uiName);
}
}
/**
* Damages the area surrounding the caret to cause
* it to be repainted in a new location. If paint()
* is reimplemented, this method should also be
* reimplemented. This method should update the
* caret bounds (x, y, width, and height).
*
* @param r the current location of the caret
* @see #paint
*/
protected synchronized void damage(final Rectangle r) {
if (r == null || fPainting) return;
x = r.x - 4;
y = r.y;
width = 10;
height = r.height;
// Don't damage the border area. We can't paint a partial border, so get the
// intersection of the caret rectangle and the component less the border, if any.
final Rectangle caretRect = new Rectangle(x, y, width, height);
final Border border = getComponent().getBorder();
if (border != null) {
final Rectangle alloc = getComponent().getBounds();
alloc.x = alloc.y = 0;
final Insets borderInsets = border.getBorderInsets(getComponent());
alloc.x += borderInsets.left;
alloc.y += borderInsets.top;
alloc.width -= borderInsets.left + borderInsets.right;
alloc.height -= borderInsets.top + borderInsets.bottom;
Rectangle2D.intersect(caretRect, alloc, caretRect);
}
x = caretRect.x;
y = caretRect.y;
width = Math.max(caretRect.width, 1);
height = Math.max(caretRect.height, 1);
repaint();
}
@Override
protected void paintBackground(Graphics graphics) {
Graphics2D g = (Graphics2D)graphics;
final JTextComponent c = getComponent();
final Container parent = c.getParent();
if (parent != null) {
g.setColor(parent.getBackground());
g.fillRect(0, 0, c.getWidth(), c.getHeight());
}
final Border border = c.getBorder();
if (border instanceof ModernTextBorder) {
g.setColor(c.getBackground());
final int width = c.getWidth();
final int height = c.getHeight();
final Insets i = border.getBorderInsets(c);
if (c.hasFocus()) {
final GraphicsConfig config = new GraphicsConfig(g);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 10, height - i.top - i.bottom + 6);
config.restore();
}
else {
g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 12, height - i.top - i.bottom + 6);
}
}
else {
super.paintBackground(g);
}
}
public static void main(String[] args) {
Component c = new Component() {};
c.setBackground(Color.WHITE);
c.setForeground(Color.BLACK);
Graphics g = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB).getGraphics();
g.setClip(0, 0, 1024, 768);
for (Border border : BORDERS) {
System.out.println(border.getClass());
border.getBorderInsets(c);
border.paintBorder(c, g, 0, 0, 1024, 768);
}
}
/**
* If a border has been set on this component, returns the
* border's insets, else calls super.getInsets.
*
* @return the value of the insets property.
* @see #setBorder
*/
public Insets getInsets() {
Border border = getBorder();
if (border != null) {
return border.getBorderInsets(this);
}
return super.getInsets();
}
private static void test(Border border, Insets insets) {
Insets result = border.getBorderInsets(getComponent(border));
if (insets == result) {
throw new Error("both instances are the same for " + border.getClass());
}
if (!insets.equals(result)) {
throw new Error("both insets are not equal for " + border.getClass());
}
}
void test(String uiName) {
Border b = UIManager.getBorder(uiName);
Insets i = b.getBorderInsets(null);
if (i == null) {
throw new RuntimeException("getBorderInsets() returns null for " + uiName);
}
}
private static void test(Border border, Insets insets) {
Insets result = border.getBorderInsets(getComponent(border));
if (insets == result) {
throw new Error("both instances are the same for " + border.getClass());
}
if (!insets.equals(result)) {
throw new Error("both insets are not equal for " + border.getClass());
}
}
@Override
protected void paintBackground(Graphics graphics) {
Graphics2D g = (Graphics2D)graphics;
final JTextComponent c = getComponent();
final Container parent = c.getParent();
if (parent != null) {
g.setColor(parent.getBackground());
g.fillRect(0, 0, c.getWidth(), c.getHeight());
}
final Border border = c.getBorder();
if (border instanceof DarculaTextBorder) {
g.setColor(c.getBackground());
final int width = c.getWidth();
final int height = c.getHeight();
final Insets i = border.getBorderInsets(c);
if (c.hasFocus()) {
final GraphicsConfig config = new GraphicsConfig(g);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
g.fillRoundRect(i.left - 5, i.top - 2, width - i.left - i.right + 10, height - i.top - i.bottom + 6, 5, 5);
config.restore();
}
else {
g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 12, height - i.top - i.bottom + 6);
}
} else {
super.paintBackground(g);
}
}
/**
* Paint highlighted borders. Meanot to be used at mouseover.
*
* @param g graphics
*/
private void highLightBorder(Graphics g) {
g.setColor(style.getHighLightColor());
Border border = style.getBorder();
if (orientation == JSplitPane.HORIZONTAL_SPLIT) {
int width = border.getBorderInsets(this).left;
g.fillRect(0, 0, width, getHeight());
g.fillRect(getWidth() - width, 0, width, getHeight());
} else {
int height = border.getBorderInsets(this).top;
g.fillRect(0, 0, getWidth(), height);
g.fillRect(0, getHeight() - height, getWidth(), height);
}
}
public static void main(String[] args) {
Component c = new Component() {};
c.setBackground(Color.WHITE);
c.setForeground(Color.BLACK);
Graphics g = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB).getGraphics();
g.setClip(0, 0, 1024, 768);
for (Border border : BORDERS) {
System.out.println(border.getClass());
border.getBorderInsets(c);
border.paintBorder(c, g, 0, 0, 1024, 768);
}
}
public static void main(String[] args) {
Component c = new Component() {};
c.setBackground(Color.WHITE);
c.setForeground(Color.BLACK);
Graphics g = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB).getGraphics();
g.setClip(0, 0, 1024, 768);
for (Border border : BORDERS) {
System.out.println(border.getClass());
border.getBorderInsets(c);
border.paintBorder(c, g, 0, 0, 1024, 768);
}
}
/**
* Calculate the placement and size of the popup portion of the combo box based
* on the combo box location and the enclosing screen bounds. If
* no transformations are required, then the returned rectangle will
* have the same values as the parameters.
*
* @param px starting x location
* @param py starting y location
* @param pw starting width
* @param ph starting height
* @return a rectangle which represents the placement and size of the popup
*/
protected Rectangle computePopupBounds(int px,int py,int pw,int ph) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds;
// Calculate the desktop dimensions relative to the combo box.
GraphicsConfiguration gc = comboBox.getGraphicsConfiguration();
Point p = new Point();
SwingUtilities.convertPointFromScreen(p, comboBox);
if (gc != null) {
Insets screenInsets = toolkit.getScreenInsets(gc);
screenBounds = gc.getBounds();
screenBounds.width -= (screenInsets.left + screenInsets.right);
screenBounds.height -= (screenInsets.top + screenInsets.bottom);
screenBounds.x += (p.x + screenInsets.left);
screenBounds.y += (p.y + screenInsets.top);
}
else {
screenBounds = new Rectangle(p, toolkit.getScreenSize());
}
int borderHeight = 0;
Border popupBorder = getBorder();
if (popupBorder != null) {
Insets borderInsets = popupBorder.getBorderInsets(this);
borderHeight = borderInsets.top + borderInsets.bottom;
screenBounds.width -= (borderInsets.left + borderInsets.right);
screenBounds.height -= borderHeight;
}
Rectangle rect = new Rectangle(px, py, pw, ph);
if (py + ph > screenBounds.y + screenBounds.height) {
if (ph <= -screenBounds.y - borderHeight) {
// popup goes above
rect.y = -ph - borderHeight;
} else {
// a full screen height popup
rect.y = screenBounds.y + Math.max(0, (screenBounds.height - ph) / 2 );
rect.height = Math.min(screenBounds.height, ph);
}
}
return rect;
}
private Map<Component, Rectangle> createBlueprint(Container parent) {
CellLayout masterLayout;
if (group.contains(parent)) {
masterLayout = new CellLayout();
for (Container container : group) {
if (container.isVisible()) {
CellLayout cellLayout = getCellLayout(container);
masterLayout.add(cellLayout);
}
}
} else {
masterLayout = getCellLayout(parent);
}
int x = 0;
int y = 0;
if (parent instanceof JComponent) {
Border b = ((JComponent) parent).getBorder();
if (b != null) {
Insets i = b.getBorderInsets(parent);
x = i.left;
y = i.top;
}
}
List<List<Component>> grid = createGrid(parent);
Map<Component, Rectangle> returnValue = new HashMap<>();
int y0 = y;
for (int row = 0; row < grid.size(); row++) {
int x0 = x;
for (int column = 0; column < grid.get(row).size(); column++) {
Component c = grid.get(row).get(column);
Dimension d = c.getPreferredSize();
Rectangle r = new Rectangle(x0, y0, d.width, d.height);
// masterLayout.columnWidths[column],
// masterLayout.rowHeights[row]);
returnValue.put(c, r);
x0 += masterLayout.columnWidths[column];
x0 += horizontalPadding;
}
y0 += masterLayout.rowHeights[row];
y0 += verticalPadding;
}
return returnValue;
}
private static Border emptyBorder(Border border) {
Insets i = border == null ? null : border.getBorderInsets(new JTextField());
return i == null ? BorderFactory.createEmptyBorder() :
BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right);
}