javax.swing.JButton.getFontMetrics()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(106)

本文整理了Java中javax.swing.JButton.getFontMetrics()方法的一些代码示例,展示了JButton.getFontMetrics()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JButton.getFontMetrics()方法的具体详情如下:
包路径:javax.swing.JButton
类名称:JButton
方法名:getFontMetrics

JButton.getFontMetrics介绍

暂无

代码示例

代码示例来源:origin: EvoSuite/evosuite

public static void initialize(){
  /*
    Since trying Java 8, started to get weird behavior on a Linux cluster.
    Issue raises from GUI now trying to write on disk (ie due to Fonts...).
    However, that sometimes strangely fails, even though executed before any
    sandbox. It happens quite often on cluster experiments, but was not able
    to reproduce it to debug :(
    As workaround, we try here to load default file system (it would happen anyway when
    loading fonts in Java 8), but do not crash the test suite (ie throw exception here
    in this method, which is usually called from a @BeforeClass). Reason is that
    maybe not all tests will access GUI.
   */
  try{
    FileSystems.getDefault();
  } catch(Throwable t){
    logger.error("Failed to load default file system: "+t.getMessage());
    return;
  }
  /*
   * Force the loading of fonts.
   * This is needed because font loading in the JVM can take several seconds (done only once),
   * and that can mess up the JUnit test execution timeouts...   
   */
  (new javax.swing.JButton()).getFontMetrics(new java.awt.Font(null));
}

代码示例来源:origin: senbox-org/snap-desktop

void adjustDimension(JButton component) {
  FontMetrics metrics = component.getFontMetrics(component.getFont());
  int width = metrics.stringWidth(component.getText());
  component.setPreferredSize(new Dimension(width + 32, controlHeight));
  component.setBounds(new Rectangle(component.getLocation(), component.getPreferredSize()));
}

代码示例来源:origin: org.databene/databene-commons

public static void equalizeButtonSizes(Graphics g, JButton... buttons) {
  String[] labels = BeanUtil.extractProperties(buttons, "text", String.class);
  // Get the largest width and height
  Dimension maxSize = new Dimension(0, 0);
  Rectangle2D textBounds = null;
  JButton button0 = buttons[0];
  FontMetrics metrics = button0.getFontMetrics(button0.getFont());
  for (int i = 0; i < labels.length; ++i) {
    textBounds = metrics.getStringBounds(labels[i], g);
    maxSize.width = Math.max(maxSize.width, (int) textBounds.getWidth());
    maxSize.height = Math.max(maxSize.height, (int) textBounds.getHeight());
  }
  Insets insets = button0.getBorder().getBorderInsets(button0);
  maxSize.width += insets.left + insets.right;
  maxSize.height += insets.top + insets.bottom;
  // reset preferred and maximum size since BoxLayout takes both into account
  for (JButton button : buttons) {
    button.setPreferredSize((Dimension) maxSize.clone());
    button.setMaximumSize((Dimension) maxSize.clone());
  }
}

代码示例来源:origin: stackoverflow.com

JButton component = new JButton(text);
FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
LineMetrics metrics = fontMetrics.getLineMetrics(text, component.getGraphics());

float ascent = metrics.getAscent(), descent = metrics.getDescent();
component.setAlignmentY(ascent / (ascent + descent));

相关文章

微信公众号

最新文章

更多

JButton类方法