javax.swing.JLabel#getPreferredSize ( )源码实例Demo

下面列出了javax.swing.JLabel#getPreferredSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jdk8u-dev-jdk   文件: TitledBorder.java
/**
 * Returns the minimum dimensions this border requires
 * in order to fully display the border and title.
 * @param c the component where this border will be drawn
 * @return the {@code Dimension} object
 */
public Dimension getMinimumSize(Component c) {
    Insets insets = getBorderInsets(c);
    Dimension minSize = new Dimension(insets.right+insets.left,
                                      insets.top+insets.bottom);
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        int position = getPosition();
        if ((position != ABOVE_TOP) && (position != BELOW_BOTTOM)) {
            minSize.width += size.width;
        }
        else if (minSize.width < size.width) {
            minSize.width += size.width;
        }
    }
    return minSize;
}
 
源代码2 项目: jclic   文件: EditorPanel.java
protected JLabel createTitleLabel(int preferredWidth) {
  JLabel result = new JLabel(getTitle());
  result.setHorizontalAlignment(SwingConstants.CENTER);
  if (getIcon() != null) {
    result.setIcon(getIcon());
    result.setIconTextGap(10);
  }
  result.setBackground(titleBgColor);
  result.setForeground(titleForeColor);
  result.setOpaque(true);
  result.setBorder(titleBorder);
  result.validate();
  Dimension d = result.getPreferredSize();
  result.setPreferredSize(new Dimension(Math.max(d.width, preferredWidth), d.height));
  result.setMinimumSize(result.getPreferredSize());
  return result;
}
 
源代码3 项目: openjdk-8-source   文件: TitledBorder.java
/**
 * Returns the minimum dimensions this border requires
 * in order to fully display the border and title.
 * @param c the component where this border will be drawn
 * @return the {@code Dimension} object
 */
public Dimension getMinimumSize(Component c) {
    Insets insets = getBorderInsets(c);
    Dimension minSize = new Dimension(insets.right+insets.left,
                                      insets.top+insets.bottom);
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        int position = getPosition();
        if ((position != ABOVE_TOP) && (position != BELOW_BOTTOM)) {
            minSize.width += size.width;
        }
        else if (minSize.width < size.width) {
            minSize.width += size.width;
        }
    }
    return minSize;
}
 
源代码4 项目: MobyDroid   文件: MaterialIcons.java
private static BufferedImage buildImage(String text, Font font, Color color) {
    JLabel icon = new JLabel(text);
    icon.setForeground(color);
    icon.setFont(font);
    Dimension dim = icon.getPreferredSize();
    int width = dim.width + 1;
    int height = dim.height + 1;
    icon.setSize(width, height);
    BufferedImage bufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    icon.print(g2d);
    g2d.dispose();
    return bufImage;
}
 
源代码5 项目: Java8CN   文件: TitledBorder.java
/**
 * Returns the minimum dimensions this border requires
 * in order to fully display the border and title.
 * @param c the component where this border will be drawn
 * @return the {@code Dimension} object
 */
public Dimension getMinimumSize(Component c) {
    Insets insets = getBorderInsets(c);
    Dimension minSize = new Dimension(insets.right+insets.left,
                                      insets.top+insets.bottom);
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        int position = getPosition();
        if ((position != ABOVE_TOP) && (position != BELOW_BOTTOM)) {
            minSize.width += size.width;
        }
        else if (minSize.width < size.width) {
            minSize.width += size.width;
        }
    }
    return minSize;
}
 
源代码6 项目: webanno   文件: LoadingSplashScreen.java
public SplashWindow(URL aUrl)
{
    JLabel l = new JLabel(new ImageIcon(aUrl));
    JLabel info = new JLabel("Application is loading...");
    getContentPane().add(l, BorderLayout.CENTER);
    getContentPane().add(info, BorderLayout.SOUTH);
    pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension labelSize = l.getPreferredSize();
    setLocation(screenSize.width / 2 - (labelSize.width / 2),
            screenSize.height / 2 - (labelSize.height / 2));
    setVisible(true);
}
 
源代码7 项目: ramus   文件: TableRowHeader.java
private int preferredHeaderWidth() {
    final JLabel longestRowLabel = new JLabel("1.1.1");
    final JTableHeader header = table.getTableHeader();
    longestRowLabel.setBorder(header.getBorder());
    longestRowLabel.setHorizontalAlignment(SwingConstants.CENTER);
    longestRowLabel.setFont(header.getFont());
    return longestRowLabel.getPreferredSize().width;
}
 
protected final void refreshLabelWidths() {
    int maximumLabelWidth = computeLeftPanelMaximumLabelWidth();
    for (int i=0; i<this.parameterComponents.size(); i++) {
        JLabel label = this.parameterComponents.get(i).getLabel();
        Dimension labelSize = label.getPreferredSize();
        labelSize.width = maximumLabelWidth;
        label.setPreferredSize(labelSize);
        label.setMinimumSize(labelSize);
    }
}
 
源代码9 项目: ramus   文件: TableRowHeader.java
private int preferredHeaderWidth() {
    final JLabel longestRowLabel = new JLabel("1.1.1");
    final JTableHeader header = table.getTableHeader();
    longestRowLabel.setBorder(header.getBorder());
    longestRowLabel.setHorizontalAlignment(SwingConstants.CENTER);
    longestRowLabel.setFont(header.getFont());
    return longestRowLabel.getPreferredSize().width;
}
 
源代码10 项目: knopflerfish.org   文件: JLabelled.java
public JLabelled(String text, String tooltip, JComponent main, int labelWidth)
{
  // This panel is horizontal box.
  setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
  setAlignmentX(Component.LEFT_ALIGNMENT);

  // Shorten text to last part after a dot
  // if it's too long
  final FontMetrics fm = main.getFontMetrics(main.getFont());
  if (fm != null) {
    int ix;
    while (-1 != (ix = text.indexOf("."))
           && fm.stringWidth(text) > labelWidth) {
      text = text.substring(ix + 1);
    }
  }

  final JLabel label = new JLabel(text);
  final Dimension size = label.getPreferredSize();
  label.setPreferredSize(new Dimension(labelWidth, size.height + 2));

  if (tooltip != null && !"".equals(tooltip)) {
    label.setToolTipText("<html>" + tooltip + "</html>");
  }

  // Since we are in a scroll pane with glue at the bottom we do not want the
  // components to stretch vertically.
  main.setMaximumSize(new Dimension(Integer.MAX_VALUE, main
      .getPreferredSize().height));
  label.setMaximumSize(label.getPreferredSize());

  add(label);
  add(main);
}
 
源代码11 项目: Robot-Overlord-App   文件: DeltaRobot3Panel.java
protected CollapsiblePanel createSpeedPanel() {
	double speed=robot.getSpeed();
	int speedIndex;
	for(speedIndex=0;speedIndex<speedOptions.length;++speedIndex) {
		if( speedOptions[speedIndex] >= speed )
			break;
	}
	speedNow = new JLabel(Double.toString(speedOptions[speedIndex]),JLabel.CENTER);
	java.awt.Dimension dim = speedNow.getPreferredSize();
	dim.width = 50;
	speedNow.setPreferredSize(dim);

	CollapsiblePanel speedPanel = new CollapsiblePanel("Speed");

	GridBagConstraints con2 = PanelHelper.getDefaultGridBagConstraints();
	con2.weightx=0.25;
	speedPanel.getContentPane().add(speedNow,con2);

	speedControl = new JSlider(0,speedOptions.length-1,speedIndex);
	speedControl.addChangeListener(this);
	speedControl.setMajorTickSpacing(speedOptions.length-1);
	speedControl.setMinorTickSpacing(1);
	speedControl.setPaintTicks(true);
	con2.anchor=GridBagConstraints.NORTHEAST;
	con2.fill=GridBagConstraints.HORIZONTAL;
	con2.weightx=0.75;
	con2.gridx=1;
	speedPanel.getContentPane().add(speedControl,con2);
	
	return speedPanel;
}
 
源代码12 项目: CQL   文件: Option.java
/**
 * Creates a new label/component pair. If given multiple compnents, will add
 * them to a JPanel and then set that as the component. Note that this is better
 * than putting them into a JPanel yourself: the label will be centered on the
 * first component passed-in.
 *
 * @param label      the JLabel of the option
 * @param components
 */
public Option(JLabel label, JComponent... components) {
	setLabel(label);

	if (components.length > 1) {
		JPanel panel = new JPanel();
		FlowLayout flow = (FlowLayout) panel.getLayout();

		flow.setAlignment(FlowLayout.LEFT);
		flow.setVgap(0);
		flow.setHgap(0);

		for (JComponent jc : components) {
			panel.add(jc);
		}

		JUtils.fixHeight(panel);
		setComponent(panel);
	} else {
		setComponent((components.length > 0) ? components[0] : null);
	}

	// We try to align the label with the first component (descending into
	// JPanels), by creating a border that aligns the middle of the border
	// with the middle of the first component.
	if (components.length > 0) {
		int labelOffset = -label.getPreferredSize().height;
		Component comp = components[0];

		while (comp instanceof JPanel) {
			labelOffset += 2 * (((JPanel) comp).getInsets().top);
			comp = ((JPanel) comp).getComponent(0);
		}

		if (comp != null) {
			labelOffset += comp.getPreferredSize().height;

			if (labelOffset > 0) { // Only do it if the first non-JPanel
									// component is bigger
				label.setBorder(new EmptyBorder(labelOffset / 2, 0, 0, 0));
			}
		}
	}
}
 
源代码13 项目: Bytecoder   文件: TitledBorder.java
/**
 * Returns the baseline.
 *
 * @throws NullPointerException {@inheritDoc}
 * @throws IllegalArgumentException {@inheritDoc}
 * @see javax.swing.JComponent#getBaseline(int, int)
 * @since 1.6
 */
public int getBaseline(Component c, int width, int height) {
    if (c == null) {
        throw new NullPointerException("Must supply non-null component");
    }
    if (width < 0) {
        throw new IllegalArgumentException("Width must be >= 0");
    }
    if (height < 0) {
        throw new IllegalArgumentException("Height must be >= 0");
    }
    Border border = getBorder();
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();
        Insets insets = getBorderInsets(border, c, new Insets(0, 0, 0, 0));

        int baseline = label.getBaseline(size.width, size.height);
        switch (getPosition()) {
            case ABOVE_TOP:
                return baseline;
            case TOP:
                insets.top = edge + (insets.top - size.height) / 2;
                return (insets.top < edge)
                        ? baseline
                        : baseline + insets.top;
            case BELOW_TOP:
                return baseline + insets.top + edge;
            case ABOVE_BOTTOM:
                return baseline + height - size.height - insets.bottom - edge;
            case BOTTOM:
                insets.bottom = edge + (insets.bottom - size.height) / 2;
                return (insets.bottom < edge)
                        ? baseline + height - size.height
                        : baseline + height - size.height + insets.bottom;
            case BELOW_BOTTOM:
                return baseline + height - size.height;
        }
    }
    return -1;
}
 
源代码14 项目: openjdk-8   文件: TitledBorder.java
/**
 * 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) {
    Border border = getBorder();
    insets = getBorderInsets(border, c, insets);

    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        switch (getPosition()) {
            case ABOVE_TOP:
                insets.top += size.height - edge;
                break;
            case TOP: {
                if (insets.top < size.height) {
                    insets.top = size.height - edge;
                }
                break;
            }
            case BELOW_TOP:
                insets.top += size.height;
                break;
            case ABOVE_BOTTOM:
                insets.bottom += size.height;
                break;
            case BOTTOM: {
                if (insets.bottom < size.height) {
                    insets.bottom = size.height - edge;
                }
                break;
            }
            case BELOW_BOTTOM:
                insets.bottom += size.height - edge;
                break;
        }
        insets.top += edge + TEXT_SPACING;
        insets.left += edge + TEXT_SPACING;
        insets.right += edge + TEXT_SPACING;
        insets.bottom += edge + TEXT_SPACING;
    }
    return insets;
}
 
源代码15 项目: Logisim   文件: ExportImage.java
OptionsPanel(JList<?> list) {
	// set up components
	formatPng = new JRadioButton("PNG");
	formatGif = new JRadioButton("GIF");
	formatJpg = new JRadioButton("JPEG");
	ButtonGroup bgroup = new ButtonGroup();
	bgroup.add(formatPng);
	bgroup.add(formatGif);
	bgroup.add(formatJpg);
	formatPng.setSelected(true);

	slider = new JSlider(SwingConstants.HORIZONTAL, -3 * SLIDER_DIVISIONS, 3 * SLIDER_DIVISIONS, 0);
	slider.setMajorTickSpacing(10);
	slider.addChangeListener(this);
	curScale = new JLabel("222%");
	curScale.setHorizontalAlignment(SwingConstants.RIGHT);
	curScale.setVerticalAlignment(SwingConstants.CENTER);
	curScaleDim = new Dimension(curScale.getPreferredSize());
	curScaleDim.height = Math.max(curScaleDim.height, slider.getPreferredSize().height);
	stateChanged(null);

	printerView = new JCheckBox();
	printerView.setSelected(true);

	// set up panel
	gridbag = new GridBagLayout();
	gbc = new GridBagConstraints();
	setLayout(gridbag);

	// now add components into panel
	gbc.gridy = 0;
	gbc.gridx = GridBagConstraints.RELATIVE;
	gbc.anchor = GridBagConstraints.NORTHWEST;
	gbc.insets = new Insets(5, 0, 5, 0);
	gbc.fill = GridBagConstraints.NONE;
	addGb(new JLabel(Strings.get("labelCircuits") + " "));
	gbc.fill = GridBagConstraints.HORIZONTAL;
	addGb(new JScrollPane(list));
	gbc.fill = GridBagConstraints.NONE;

	gbc.gridy++;
	addGb(new JLabel(Strings.get("labelImageFormat") + " "));
	Box formatsPanel = new Box(BoxLayout.Y_AXIS);
	formatsPanel.add(formatPng);
	formatsPanel.add(formatGif);
	formatsPanel.add(formatJpg);
	addGb(formatsPanel);

	gbc.gridy++;
	addGb(new JLabel(Strings.get("labelScale") + " "));
	addGb(slider);
	addGb(curScale);

	gbc.gridy++;
	addGb(new JLabel(Strings.get("labelPrinterView") + " "));
	addGb(printerView);
}
 
源代码16 项目: snap-desktop   文件: ProductPlacemarkView.java
private int getColumnMinWith(TableColumn column, TableCellRenderer renderer, int margin) {
    final Object headerValue = column.getHeaderValue();
    final JLabel label = (JLabel) renderer.getTableCellRendererComponent(null, headerValue, false, false, 0, 0);
    return label.getPreferredSize().width + margin;
}
 
源代码17 项目: raccoon4   文件: BriefAppDescriptionBuilder.java
@Override
protected JPanel assemble() {
	JPanel panel = new JPanel();
	panel.setLayout(new GridBagLayout());
	GridBagConstraints gbc = new GridBagConstraints();

	JLabel appNameLabel = new JLabel(doc.getTitle(), SwingConstants.CENTER);
	Font font = appNameLabel.getFont();
	Font boldFont = new Font(font.getFontName(), Font.BOLD, font.getSize() + 2);
	appNameLabel.setFont(boldFont);
	appNameLabel.setToolTipText(doc.getTitle());
	Dimension tmp = appNameLabel.getPreferredSize();
	tmp.width = 150;
	appNameLabel.setPreferredSize(tmp);

	JLabel vendorNameLabel = new JLabel(doc.getCreator(), SwingConstants.CENTER);
	tmp = vendorNameLabel.getPreferredSize();
	tmp.width = 150;
	vendorNameLabel.setPreferredSize(tmp);
	vendorNameLabel.setToolTipText(doc.getCreator());

	button = new JButton();
	button.addActionListener(this);
	button.setIcon(SPINNER);

	globals.get(ImageLoaderService.class).request(this,
			DocUtil.getAppIconUrl(doc));

	JPanel stars = new StarPanel(5,
			doc.getAggregateRating().getStarRating() / 5);
	DecimalFormat df = new DecimalFormat("#.## \u2605");
	stars.setToolTipText(df.format(doc.getAggregateRating().getStarRating()));

	gbc.gridx = 0;
	gbc.gridy = 0;
	gbc.insets.bottom = 10;
	panel.add(button, gbc);

	gbc.insets.bottom = 0;
	gbc.gridy++;
	panel.add(appNameLabel, gbc);

	gbc.gridy++;
	panel.add(vendorNameLabel, gbc);

	gbc.gridy++;
	gbc.fill = GridBagConstraints.HORIZONTAL;
	gbc.insets.top = 10;
	gbc.insets.left = 15;
	gbc.insets.right = 15;
	gbc.insets.bottom = 15;
	panel.add(stars, gbc);

	return panel;
}
 
源代码18 项目: jdk8u_jdk   文件: TitledBorder.java
/**
 * 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) {
    Border border = getBorder();
    insets = getBorderInsets(border, c, insets);

    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        switch (getPosition()) {
            case ABOVE_TOP:
                insets.top += size.height - edge;
                break;
            case TOP: {
                if (insets.top < size.height) {
                    insets.top = size.height - edge;
                }
                break;
            }
            case BELOW_TOP:
                insets.top += size.height;
                break;
            case ABOVE_BOTTOM:
                insets.bottom += size.height;
                break;
            case BOTTOM: {
                if (insets.bottom < size.height) {
                    insets.bottom = size.height - edge;
                }
                break;
            }
            case BELOW_BOTTOM:
                insets.bottom += size.height - edge;
                break;
        }
        insets.top += edge + TEXT_SPACING;
        insets.left += edge + TEXT_SPACING;
        insets.right += edge + TEXT_SPACING;
        insets.bottom += edge + TEXT_SPACING;
    }
    return insets;
}
 
源代码19 项目: JDKSourceCode1.8   文件: TitledBorder.java
/**
 * Returns the baseline.
 *
 * @throws NullPointerException {@inheritDoc}
 * @throws IllegalArgumentException {@inheritDoc}
 * @see javax.swing.JComponent#getBaseline(int, int)
 * @since 1.6
 */
public int getBaseline(Component c, int width, int height) {
    if (c == null) {
        throw new NullPointerException("Must supply non-null component");
    }
    if (width < 0) {
        throw new IllegalArgumentException("Width must be >= 0");
    }
    if (height < 0) {
        throw new IllegalArgumentException("Height must be >= 0");
    }
    Border border = getBorder();
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();
        Insets insets = getBorderInsets(border, c, new Insets(0, 0, 0, 0));

        int baseline = label.getBaseline(size.width, size.height);
        switch (getPosition()) {
            case ABOVE_TOP:
                return baseline;
            case TOP:
                insets.top = edge + (insets.top - size.height) / 2;
                return (insets.top < edge)
                        ? baseline
                        : baseline + insets.top;
            case BELOW_TOP:
                return baseline + insets.top + edge;
            case ABOVE_BOTTOM:
                return baseline + height - size.height - insets.bottom - edge;
            case BOTTOM:
                insets.bottom = edge + (insets.bottom - size.height) / 2;
                return (insets.bottom < edge)
                        ? baseline + height - size.height
                        : baseline + height - size.height + insets.bottom;
            case BELOW_BOTTOM:
                return baseline + height - size.height;
        }
    }
    return -1;
}
 
源代码20 项目: hottub   文件: TitledBorder.java
/**
 * 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) {
    Border border = getBorder();
    insets = getBorderInsets(border, c, insets);

    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        switch (getPosition()) {
            case ABOVE_TOP:
                insets.top += size.height - edge;
                break;
            case TOP: {
                if (insets.top < size.height) {
                    insets.top = size.height - edge;
                }
                break;
            }
            case BELOW_TOP:
                insets.top += size.height;
                break;
            case ABOVE_BOTTOM:
                insets.bottom += size.height;
                break;
            case BOTTOM: {
                if (insets.bottom < size.height) {
                    insets.bottom = size.height - edge;
                }
                break;
            }
            case BELOW_BOTTOM:
                insets.bottom += size.height - edge;
                break;
        }
        insets.top += edge + TEXT_SPACING;
        insets.left += edge + TEXT_SPACING;
        insets.right += edge + TEXT_SPACING;
        insets.bottom += edge + TEXT_SPACING;
    }
    return insets;
}