下面列出了怎么用java.awt.Insets的API类实例代码及写法,或者点击链接到github查看源代码。
private
Panel
createPasswordPanel()
{
Panel panel = new Panel();
String gap = ": ";
// Build panel.
panel.setAnchor(GridBagConstraints.EAST);
panel.add(getSharedProperty("password") + gap, 0, 0, 1, 1, 0, 50);
panel.add(getProperty("retype") + gap, 0, 1, 1, 1, 0, 50);
panel.setFill(GridBagConstraints.HORIZONTAL);
panel.add(getFields()[PASSWORD], 1, 0, 1, 1, 100, 0);
panel.add(getFields()[PASSWORD_CONFIRM], 1, 1, 1, 1, 0, 0);
panel.setInsets(new Insets(10, 10, 10, 150));
return panel;
}
/**
* Lays out the specified container.
*
* @param parent the container to be laid out
* @throws IllegalStateException if any of the components added to
* this layout are not in both a horizontal and vertical group
*/
public void layoutContainer(Container parent) {
// Step 1: Prepare for layout.
prepare(SPECIFIC_SIZE);
Insets insets = parent.getInsets();
int width = parent.getWidth() - insets.left - insets.right;
int height = parent.getHeight() - insets.top - insets.bottom;
boolean ltr = isLeftToRight();
if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
hasPreferredPaddingSprings) {
// Step 2: Calculate autopadding springs
calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
width);
calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
height);
}
// Step 3: set the size of the groups.
horizontalGroup.setSize(HORIZONTAL, 0, width);
verticalGroup.setSize(VERTICAL, 0, height);
// Step 4: apply the size to the components.
for (ComponentInfo info : componentInfos.values()) {
info.setBounds(insets, width, ltr);
}
}
/**
* Adjusts the <code>Rectangle</code> <code>available</code> based on if
* the horizontal scrollbar is needed (<code>wantsHSB</code>).
* The location of the hsb is updated in <code>hsbR</code>, and
* the viewport border insets (<code>vpbInsets</code>) are used to offset
* the hsb. This is only called when <code>wantsHSB</code> has
* changed, eg you shouldn't invoked adjustForHSB(true) twice.
*/
private void adjustForHSB(boolean wantsHSB, Rectangle available,
Rectangle hsbR, Insets vpbInsets) {
int oldHeight = hsbR.height;
if (wantsHSB) {
int hsbHeight = Math.max(0, Math.min(available.height,
hsb.getPreferredSize().height));
available.height -= hsbHeight;
hsbR.y = available.y + available.height + vpbInsets.bottom;
hsbR.height = hsbHeight;
}
else {
available.height += oldHeight;
}
}
/**
* Lays out the specified container.
*
* @param parent the container to be laid out
* @throws IllegalStateException if any of the components added to
* this layout are not in both a horizontal and vertical group
*/
public void layoutContainer(Container parent) {
// Step 1: Prepare for layout.
prepare(SPECIFIC_SIZE);
Insets insets = parent.getInsets();
int width = parent.getWidth() - insets.left - insets.right;
int height = parent.getHeight() - insets.top - insets.bottom;
boolean ltr = isLeftToRight();
if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
hasPreferredPaddingSprings) {
// Step 2: Calculate autopadding springs
calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
width);
calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
height);
}
// Step 3: set the size of the groups.
horizontalGroup.setSize(HORIZONTAL, 0, width);
verticalGroup.setSize(VERTICAL, 0, height);
// Step 4: apply the size to the components.
for (ComponentInfo info : componentInfos.values()) {
info.setBounds(insets, width, ltr);
}
}
private String layout(JLabel label, FontMetrics fm,
int width, int height) {
Insets insets = label.getInsets(null);
String text = label.getText();
Icon icon = (label.isEnabled()) ? label.getIcon() :
label.getDisabledIcon();
Rectangle paintViewR = new Rectangle();
paintViewR.x = insets.left;
paintViewR.y = insets.top;
paintViewR.width = width - (insets.left + insets.right);
paintViewR.height = height - (insets.top + insets.bottom);
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
return layoutCL(label, fm, text, icon, paintViewR, paintIconR,
paintTextR);
}
/**
* Creates a button to toglle the labels.
*
* @param view The DrawingView the button will belong to.
* @return The created button.
*/
private JToggleButton toggleLabelsButton(final OpenTCSDrawingView drawingView) {
final JToggleButton toggleButton = new JToggleButton();
toggleButton.setToolTipText(
labels.getString("drawingViewPlacardPanel.button_toggleLabels.tooltipText")
);
toggleButton.setIcon(ImageDirectory.getImageIcon("/menu/comment-add.16.png"));
toggleButton.setMargin(new Insets(0, 0, 0, 0));
toggleButton.setFocusable(false);
toggleButton.addItemListener(
(ItemEvent event) -> drawingView.setLabelsVisible(toggleButton.isSelected())
);
return toggleButton;
}
private Dimension layoutSize(final Container target, final DimensionGetter getter) {
synchronized (target.getTreeLock()) {
final Dimension northSize = sumHorizontal(northList, getter);
final Dimension southSize = sumHorizontal(southList, getter);
final Dimension westSize = sumVertical(westList, getter);
final Dimension eastSize = sumVertical(eastList, getter);
final Dimension centerSize = sumCenter(centerList, getter);
final Dimension dim = new Dimension(
max(northSize.width, southSize.width, westSize.width + centerSize.width + eastSize.width),
northSize.height + max(westSize.height, centerSize.height, eastSize.height) + southSize.height);
final Insets insets = target.getInsets();
dim.width += insets.left + insets.right;
dim.height += insets.top + insets.bottom;
return dim;
}
}
/**
* Returns the preferred dimensions given the components
* in the target container.
*
* @param target the component to lay out
*/
public Dimension preferredLayoutSize(Container target) {
Dimension tarsiz = new Dimension(0, 0);
for (int i = 0; i < target.getComponentCount(); i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0) {
tarsiz.height += hgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * target.getComponentCount();
tarsiz.height += insets.top + insets.bottom + vgap * target.getComponentCount();
return tarsiz;
}
@Override
public void show() {
if( dropShadowDelegate != null )
dropShadowDelegate.show();
if( mediumWeightPanel != null )
showMediumWeightDropShadow();
super.show();
// fix location of light weight popup in case it has left or top drop shadow
if( lightComp != null ) {
Insets insets = lightComp.getInsets();
if( insets.left != 0 || insets.top != 0 )
lightComp.setLocation( lightComp.getX() - insets.left, lightComp.getY() - insets.top );
}
}
public void mousePressed(MouseEvent e) {
if (inDivider) {
canDrag = true;
dragX = e.getX();
offsetX = dragX - dividerOffsets.get(offsetIdx);
Insets insets = getInsets();
minX = offsetIdx == 0 ? insets.left : dividerOffsets.get(offsetIdx - 1) + DIVIDER_SIZE;
maxX = offsetIdx == dividerOffsets.size() - 1 ? getWidth() - insets.right - DIVIDER_SIZE :
dividerOffsets.get(offsetIdx + 1) - DIVIDER_SIZE;
c1 = visibleComponents.get(offsetIdx);
c2 = visibleComponents.get(offsetIdx + 1);
relWidth = componentsWeights.get(c1) + componentsWeights.get(c2);
}
}
@Override
public void layoutContainer(Container target) {
Component[] children = target.getComponents();
if (children.length > 0) {
Dimension size = children[0].getPreferredSize();
Dimension avail = target.getSize();
Insets insets = target.getInsets();
int x = insets.left;
int y = insets.top;
int margin = mSheet.getScale().scale(MARGIN);
for (Component child : children) {
child.setBounds(x, y, size.width, size.height);
x += size.width + margin;
if (x + size.width + insets.right > avail.width) {
x = insets.left;
y += size.height + margin;
}
}
}
}
/**
* Reinitialize the insets parameter with this Border's current Insets.
* @param c the component for which this border insets value applies
* @param insets the object to be reinitialized
*/
public Insets getBorderInsets(Component c, Insets insets) {
if (!(c instanceof JPopupMenu)) {
return insets;
}
FontMetrics fm;
int descent = 0;
int ascent = 16;
String title = ((JPopupMenu)c).getLabel();
if (title == null) {
insets.left = insets.top = insets.right = insets.bottom = 0;
return insets;
}
fm = c.getFontMetrics(font);
if(fm != null) {
descent = fm.getDescent();
ascent = fm.getAscent();
}
insets.top += ascent + descent + TEXT_SPACING + GROOVE_HEIGHT;
return insets;
}
/**
* getScreenWorkingArea, This returns the working area of the screen. (The working area excludes
* any task bars.) This function accounts for multi-monitor setups. If a window is supplied,
* then the the monitor that contains the window will be used. If a window is not supplied, then
* the primary monitor will be used.
*/
static public Rectangle getScreenWorkingArea(Window windowOrNull) {
Insets insets;
Rectangle bounds;
if (windowOrNull == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
insets = Toolkit.getDefaultToolkit().getScreenInsets(ge.getDefaultScreenDevice()
.getDefaultConfiguration());
bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
} else {
GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
insets = windowOrNull.getToolkit().getScreenInsets(gc);
bounds = gc.getBounds();
}
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
return bounds;
}
/**
* Reinitialize the insets parameter with this Border's current Insets.
* @param c the component for which this border insets value applies
* @param insets the object to be reinitialized
*/
public Insets getBorderInsets(Component c, Insets insets) {
if (!(c instanceof JPopupMenu)) {
return insets;
}
FontMetrics fm;
int descent = 0;
int ascent = 16;
String title = ((JPopupMenu)c).getLabel();
if (title == null) {
insets.left = insets.top = insets.right = insets.bottom = 0;
return insets;
}
fm = c.getFontMetrics(font);
if(fm != null) {
descent = fm.getDescent();
ascent = fm.getAscent();
}
insets.top += ascent + descent + TEXT_SPACING + GROOVE_HEIGHT;
return insets;
}
protected void calculateViewRectAndBaseline(MarkerBar bar) {
Insets insets = bar.getInsets();
viewRect.x = insets.left;
viewRect.y = insets.top;
viewRect.width = bar.getWidth() - (insets.right + viewRect.x);
viewRect.height = bar.getHeight() - (insets.bottom + viewRect.y);
if (bar.getOrientation() == SwingConstants.HORIZONTAL) {
baselineLeft = viewRect.x + MARKER_BODY_WIDTH / 2;
baselineLength = viewRect.width - MARKER_BODY_WIDTH - 1;
if (bar.isFliped()) {
baselineTop = viewRect.y;
} else {
baselineTop = viewRect.y + viewRect.height - 1;
}
} else {
baselineLeft = viewRect.y + MARKER_BODY_WIDTH / 2;
baselineLength = viewRect.height - MARKER_BODY_WIDTH - 1;
if (bar.isFliped()) {
baselineTop = viewRect.x + viewRect.width - 1;
} else {
baselineTop = viewRect.x;
}
}
}
@Override
public void paint(Graphics g0, JComponent c) {
Graphics2D g = (Graphics2D) g0;
JColorWell well = (JColorWell) c;
Color color = well.getColorSelectionModel().getSelectedColor();
Border border = c.getBorder();
Insets borderInsets = border.getBorderInsets(c);
if (color.getAlpha() < 255) {
TexturePaint checkers = PlafPaintUtils.getCheckerBoard(8);
g.setPaint(checkers);
g.fillRect(borderInsets.left, borderInsets.top, c.getWidth()
- borderInsets.left - borderInsets.right, c.getHeight()
- borderInsets.top - borderInsets.bottom);
}
g.setColor(color);
g.fillRect(borderInsets.left, borderInsets.top, c.getWidth()
- borderInsets.left - borderInsets.right, c.getHeight()
- borderInsets.top - borderInsets.bottom);
}
private Insets computeInsets(Insets insets) {
if (tileIcon != null && top == -1 && bottom == -1 &&
left == -1 && right == -1) {
int w = tileIcon.getIconWidth();
int h = tileIcon.getIconHeight();
insets.top = h;
insets.right = w;
insets.bottom = h;
insets.left = w;
} else {
insets.left = left;
insets.top = top;
insets.right = right;
insets.bottom = bottom;
}
return insets;
}
public Insets getBorderInsets(Component c, Insets insets) {
Insets margin = null;
if (c instanceof AbstractButton) {
margin = ((AbstractButton)c).getMargin();
}
if (margin == null || margin instanceof UIResource) {
// default margin so replace
insets.left = left;
insets.top = top;
insets.right = right;
insets.bottom = bottom;
} else {
// Margin which has been explicitly set by the user.
insets.left = margin.left;
insets.top = margin.top;
insets.right = margin.right;
insets.bottom = margin.bottom;
}
return insets;
}
/**
* Adds a border and a title around a given component.
* If the component already has some border, it is overridden (not kept).
*
* @param component component the border and title should be added to
* @param insets insets between the component and the titled border
* @param title text of the title
*/
private static void addTitledBorder(JComponent component,
Insets insets,
String title) {
Border insideBorder = BorderFactory.createEmptyBorder(
insets.top, insets.left, insets.bottom, insets.right);
Border outsideBorder = new TitledBorder(
BorderFactory.createEtchedBorder(), title);
component.setBorder(new CompoundBorder(outsideBorder, insideBorder));
}
public Dimension getSize(Container parent, boolean minimum) {
int n = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension d = new Dimension();
for (int i = 0; i < n; i++) {
Component comp = parent.getComponent(i);
if (comp instanceof EnableButton) {
continue;
}
Dimension p = (minimum
? comp.getMinimumSize()
: comp.getPreferredSize());
if (horizontal) {
d.width += p.width;
if (d.height < p.height) {
d.height = p.height;
}
} else {
if (d.width < p.width) {
d.width = p.width;
}
d.height += p.height;
}
}
d.width += (insets.left + insets.right);
d.height += (insets.top + insets.bottom);
return d;
}
public Insets getBorderInsets(Component c, Insets newInsets) {
if (MetalLookAndFeel.usingOcean()) {
newInsets.set(1, 2, 3, 2);
}
else {
newInsets.top = newInsets.left = newInsets.bottom = newInsets.right = 2;
}
if (!(c instanceof JToolBar)) {
return newInsets;
}
if ( ((JToolBar) c).isFloatable() ) {
if ( ((JToolBar) c).getOrientation() == HORIZONTAL ) {
if (c.getComponentOrientation().isLeftToRight()) {
newInsets.left = 16;
} else {
newInsets.right = 16;
}
} else {// vertical
newInsets.top = 16;
}
}
Insets margin = ((JToolBar) c).getMargin();
if ( margin != null ) {
newInsets.left += margin.left;
newInsets.top += margin.top;
newInsets.right += margin.right;
newInsets.bottom += margin.bottom;
}
return newInsets;
}
private void init() {
setLayout(new GridBagLayout());
// Create a blank label to use as a vertical fill so that the label/item pairs are aligned to
// the top of the panel
// and are not grouped in the center if the parent component is taller than the preferred size
// of the panel.
final GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 99;
constraints.insets = new Insets(10, 0, 0, 0);
constraints.weighty = 1.0;
constraints.fill = GridBagConstraints.VERTICAL;
final JLabel verticalFillLabel = new JLabel();
add(verticalFillLabel, constraints);
}
@Override
public Component getListCellRendererComponent(JList<? extends Member> list,
Member member, int index, boolean isSelected, boolean cellHasFocus) {
label.setText(member.getName());
if (member.isLeader()) {
label.setFont(boldFont);
} else {
label.setFont(normalFont);
}
/*
* This is very, very ugly. JList does not resize the component
* normally, so the StatusDisplayBar never gets the chance to update
* the model. Try to figure out the correct width.
*/
Insets insets = hpBar.getInsets();
int barWidth = list.getWidth() - insets.left - insets.right - 2;
insets = list.getInsets();
barWidth -= insets.left + insets.right;
hpBar.getModel().setMaxRepresentation(barWidth);
if (member.isPresent()) {
hpBar.setPresent(true);
hpBar.setRatio(member.getHpRatio());
} else {
hpBar.setPresent(false);
}
return renderer;
}
/**
* Reinitialize the insets parameter with this Border's current Insets.
* @param c the component for which this border insets value applies
* @param insets the object to be reinitialized
*/
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = left;
insets.top = top;
insets.right = right;
insets.bottom = bottom;
return insets;
}
/**
* Decorates the button with the approriate UI configurations.
*/
private void decorate() {
setBorderPainted(false);
setOpaque(true);
setContentAreaFilled(false);
setMargin(new Insets(0, 0, 0, 0));
}
@Override
public Insets getBorderInsets(Component c) {
boolean compressed = Boolean.parseBoolean(String.valueOf(((JProgressBar) c)
.getClientProperty(RapidLookTools.PROPERTY_PROGRESSBAR_COMPRESSED)));
if (compressed) {
return new Insets(3, 3, 3, 3);
} else {
return new Insets(0, 3, 10, 3);
}
}
@Override
protected PaintContext getPaintContext() {
//the paint context returned will have a few dummy values. The
//implementation of doPaint doesn't bother with the "decode" methods
//but calculates where to paint the circles manually. As such, we
//only need to indicate in our PaintContext that we don't want this
//to ever be cached
return new PaintContext(
new Insets(1, 0, 1, 0),
new Dimension(38, 7),
false, CacheMode.NO_CACHING, 1, 1);
}
private
Panel
createDetailsPanel()
{
Panel panel = new Panel();
Panel details = new Panel();
String gap = ": ";
// Build details panel.
details.setAnchor(GridBagConstraints.EAST);
details.add(getProperty("sum") + gap, 0, 0, 1, 1, 0, 33);
details.add(getProperty("unassigned") + gap, 0, 1, 1, 1, 0, 33);
details.add(getProperty("total") + gap, 0, 2, 1, 1, 0, 34);
details.setAnchor(GridBagConstraints.WEST);
for(int len = 0; len < getLabels().length; ++len)
{
details.add(getLabels()[len], 1, len, 1, 1, 100, 0);
}
details.setBorder(createTitledBorder(getProperty("details")));
// Build panel.
panel.setFill(GridBagConstraints.BOTH);
panel.add(details, 0, 0, 1, 1, 100, 100);
panel.setInsets(new Insets(0, 15, 0, 15));
return panel;
}
private void addSqlTextAreaPanel(final GridBagConstraints gbc)
{
gbc.weighty = .7;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(10, 25, 10, 10);
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textPanel.add(sqlTextArea, BorderLayout.CENTER);
add(textPanel, gbc);
}
public EnableButton(Group group, int type) {
super(icons[type]);
this.group = group;
this.type = type;
addActionListener(this);
setMargin(new Insets(0, 0, 0, 0));
setBorderPainted(false);
}