本文整理了Java中javax.swing.JTextField.getFontMetrics()
方法的一些代码示例,展示了JTextField.getFontMetrics()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JTextField.getFontMetrics()
方法的具体详情如下:
包路径:javax.swing.JTextField
类名称:JTextField
方法名:getFontMetrics
暂无
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Returns true if the given mouse event occurred within a highlight h on label.
*/
public static boolean isInHighlight(MouseEvent e, JTextField label, Highlighter h) {
Highlight[] hls = h.getHighlights();
if(hls == null || hls.length == 0)
return false;
Highlight hl = hls[0];
FontMetrics fm = label.getFontMetrics(label.getFont());
int offset = getCharOffset(fm, label.getText(), e.getX());
return hl.getStartOffset() <= offset && offset < hl.getEndOffset();
}
代码示例来源:origin: stanfordnlp/CoreNLP
private boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2) {
//Two parts: adding the highlight on the label, and scrolling the list appropriately
//HighlightUtils handles the first part, we handle the second part here
boolean highlightSuccessful = HighlightUtils.addHighlight(label, mouseEvent1, mouseEvent2);
FontMetrics fm = label.getFontMetrics(label.getFont());
int firstXpos = mouseEvent1.getX();
int lastXpos = mouseEvent2.getX();
int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
if(lastOffset != firstOffset) {
if(firstOffset > lastOffset) {
int tmp = firstOffset;
firstOffset = lastOffset;
lastOffset = tmp;
}
Rectangle curVisible = list.getVisibleRect();
if(lastXpos > curVisible.x+curVisible.width) {
list.scrollRectToVisible(new Rectangle(new Point(lastXpos-curVisible.width, curVisible.y), curVisible.getSize()));
} else if(lastXpos < curVisible.x) {
list.scrollRectToVisible(new Rectangle(new Point(lastXpos, curVisible.y), curVisible.getSize()));
}
list.repaint();
return highlightSuccessful;
} else
return false;
}
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Highlight the given label from the first mouse event to the second
* Returns true if the highlight was successful, false otherwise.
*/
public static boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2) {
FontMetrics fm = label.getFontMetrics(label.getFont());
int firstXpos = mouseEvent1.getX();
int lastXpos = mouseEvent2.getX();
int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
if(lastOffset != firstOffset) {
if(firstOffset > lastOffset) {
int tmp = firstOffset;
firstOffset = lastOffset;
lastOffset = tmp;
}
try {
label.getHighlighter().removeAllHighlights();
label.getHighlighter().addHighlight(firstOffset, lastOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
return true;
} catch (BadLocationException e1) {
return false;
}
} else
return false;
}
代码示例来源:origin: stackoverflow.com
JTextField textField = new JTextField();
String textToTest = "abcdefg";
FontRenderContext fontRenderContext = textField.getFontMetrics(font).getFontRenderContext();
GlyphVector glyphVector = font.layoutGlyphVector(fontRenderContext, textToTest.toCharArray(), 0, 4, Font.LAYOUT_LEFT_TO_RIGHT);
int layoutFlags = glyphVector.getLayoutFlags();
boolean hasComplexGlyphs = (layoutFlags & GlyphVector.FLAG_COMPLEX_GLYPHS) != 0;
int numberOfGlyphs = glyphVector.getNumGlyphs();
代码示例来源:origin: com.github.lgooddatepicker/LGoodDatePicker
/**
* getFormattedDateWidthInPixels, This returns the width (in pixels) of the longest formatted
* time, using the supplied DateTimeFormatter instance and font. Note that the locale
* information is built into the display format.
*
* You may optionally add extra characters to the longestTimeString that is used in the
* calculation, by supplying a nonzero value for the parameter numberOfExtraCharacters. This
* parameter can also be used with a negative value to reduce the size that is returned.
*/
static public int getFormattedTimeWidthInPixels(DateTimeFormatter formatForDisplayTime,
Font fontValidTime, int numberOfExtraCharacters) {
// Create the font metrics that will be used in the calculation.
JTextField textField = new JTextField();
FontMetrics fontMetrics = textField.getFontMetrics(fontValidTime);
// Create the longest possible time.
LocalTime longestTime = LocalTime.of(22, 59, 59, 999999999);
// Generate the longest time string. Note, The locale is built into the formatter instance.
String longestTimeString = longestTime.format(formatForDisplayTime);
// Get the width of the longest time string (in pixels), using the supplied font metrics.
int longestTimeWidth = fontMetrics.stringWidth(longestTimeString);
// Add space for two characters.
int singleNumericCharacterWidth = fontMetrics.stringWidth("8");
longestTimeWidth += (2 * singleNumericCharacterWidth);
// Don't return return anything less than 50 pixels by default.
longestTimeWidth = (longestTimeWidth < 50) ? 50 : longestTimeWidth;
// If requested, pad the result with space for any (programmer specified) extra characters.
longestTimeWidth += (numberOfExtraCharacters * singleNumericCharacterWidth);
// Return the width of the longest formatted time, in pixels.
return longestTimeWidth;
}
代码示例来源:origin: dcaoyuan/nbscala
/** Creates new form LineBreakpointPanel */
public ActionsPanel (JPDABreakpoint b) {
breakpoint = b;
initComponents ();
cbSuspend.addItem (java.util.ResourceBundle.getBundle("org/netbeans/modules/debugger/jpda/ui/breakpoints/Bundle").getString("LBL_CB_Actions_Panel_Suspend_None"));
cbSuspend.addItem (java.util.ResourceBundle.getBundle("org/netbeans/modules/debugger/jpda/ui/breakpoints/Bundle").getString("LBL_CB_Actions_Panel_Suspend_Current"));
cbSuspend.addItem (java.util.ResourceBundle.getBundle("org/netbeans/modules/debugger/jpda/ui/breakpoints/Bundle").getString("LBL_CB_Actions_Panel_Suspend_All"));
switch (b.getSuspend ()) {
case JPDABreakpoint.SUSPEND_NONE:
cbSuspend.setSelectedIndex (0);
break;
case JPDABreakpoint.SUSPEND_EVENT_THREAD:
cbSuspend.setSelectedIndex (1);
break;
case JPDABreakpoint.SUSPEND_ALL:
cbSuspend.setSelectedIndex (2);
break;
}
if (b.getPrintText () != null)
tfPrintText.setText (b.getPrintText ());
tfPrintText.setPreferredSize(new Dimension(
30*tfPrintText.getFontMetrics(tfPrintText.getFont()).charWidth('W'),
tfPrintText.getPreferredSize().height));
tfPrintText.setCaretPosition(0);
}
代码示例来源:origin: edu.stanford.nlp/stanford-corenlp
/**
* Returns true if the given mouse event occurred within a highlight h on label.
*/
public static boolean isInHighlight(MouseEvent e, JTextField label, Highlighter h) {
Highlight[] hls = h.getHighlights();
if(hls == null || hls.length == 0)
return false;
Highlight hl = hls[0];
FontMetrics fm = label.getFontMetrics(label.getFont());
int offset = getCharOffset(fm, label.getText(), e.getX());
return hl.getStartOffset() <= offset && offset < hl.getEndOffset();
}
代码示例来源:origin: com.github.lgooddatepicker/LGoodDatePicker
FontMetrics fontMetrics = textField.getFontMetrics(fontValidDate);
代码示例来源:origin: edu.stanford.nlp/stanford-corenlp
private boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2) {
//Two parts: adding the highlight on the label, and scrolling the list appropriately
//HighlightUtils handles the first part, we handle the second part here
boolean highlightSuccessful = HighlightUtils.addHighlight(label, mouseEvent1, mouseEvent2);
FontMetrics fm = label.getFontMetrics(label.getFont());
int firstXpos = mouseEvent1.getX();
int lastXpos = mouseEvent2.getX();
int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
if(lastOffset != firstOffset) {
if(firstOffset > lastOffset) {
int tmp = firstOffset;
firstOffset = lastOffset;
lastOffset = tmp;
}
Rectangle curVisible = list.getVisibleRect();
if(lastXpos > curVisible.x+curVisible.width) {
list.scrollRectToVisible(new Rectangle(new Point(lastXpos-curVisible.width, curVisible.y), curVisible.getSize()));
} else if(lastXpos < curVisible.x) {
list.scrollRectToVisible(new Rectangle(new Point(lastXpos, curVisible.y), curVisible.getSize()));
}
list.repaint();
return highlightSuccessful;
} else
return false;
}
代码示例来源:origin: dcaoyuan/nbscala
new Dimension(8*tfHitCountFilter.getFontMetrics(tfHitCountFilter.getFont()).charWidth('8'),
tfHitCountFilter.getPreferredSize().height));
cbHitStyle.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
代码示例来源:origin: dcaoyuan/nbscala
/** Creates new form LineBreakpointPanel */
public LineBreakpointPanel (LineBreakpoint b) {
breakpoint = b;
initComponents ();
String url = b.getURL();
try {
URI uri = new URI(url);
tfFileName.setText(uri.getPath());
} catch (Exception e) {
tfFileName.setText(url);
}
tfFileName.setPreferredSize(new Dimension(
30*tfFileName.getFontMetrics(tfFileName.getFont()).charWidth('W'),
tfFileName.getPreferredSize().height));
tfLineNumber.setText(Integer.toString(b.getLineNumber()));
conditionsPanel = new ConditionsPanel();
setupConditionPane();
conditionsPanel.showClassFilter(false);
conditionsPanel.setCondition(b.getCondition());
conditionsPanel.setHitCountFilteringStyle(b.getHitCountFilteringStyle());
conditionsPanel.setHitCount(b.getHitCountFilter());
cPanel.add(conditionsPanel, "Center");
actionsPanel = new ActionsPanel (b);
pActions.add (actionsPanel, "Center");
}
代码示例来源:origin: edu.stanford.nlp/stanford-corenlp
/**
* Highlight the given label from the first mouse event to the second
* Returns true if the highlight was successful, false otherwise.
*/
public static boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2) {
FontMetrics fm = label.getFontMetrics(label.getFont());
int firstXpos = mouseEvent1.getX();
int lastXpos = mouseEvent2.getX();
int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
if(lastOffset != firstOffset) {
if(firstOffset > lastOffset) {
int tmp = firstOffset;
firstOffset = lastOffset;
lastOffset = tmp;
}
try {
label.getHighlighter().removeAllHighlights();
label.getHighlighter().addHighlight(firstOffset, lastOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
return true;
} catch (BadLocationException e1) {
return false;
}
} else
return false;
}
代码示例来源:origin: LibraryOfCongress/bagger
public BagTree(BagView bagView, String path) {
super();
this.setShowsRootHandles(true);
basePath = path;
parentNode = new DefaultMutableTreeNode(basePath);
initialize();
initListeners();
JTextField nameTextField = new JTextField();
int fieldHeight = nameTextField.getFontMetrics(nameTextField.getFont()).getHeight() + 5;
BAGTREE_ROW_MODIFIER = fieldHeight;
this.setDragEnabled(true);
this.setDropMode(DropMode.ON_OR_INSERT);
this.setTransferHandler(new BagTreeTransferHandler());
this.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
bagView.registerTreeListener(path, this);
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-visualweb-propertyeditors
FontMetrics fontMetrics = fontSizeField.getFontMetrics(fontSizeField.getFont());
int width = fontMetrics.stringWidth((String) fontSizes.get(0)) + 10;
int height = (fontMetrics.getHeight() + 10) > 25 ? fontMetrics.getHeight() + 10 : 25;
代码示例来源:origin: org.apache.odftoolkit/simple-odf
FontMetrics fm = txtField.getFontMetrics(txtField.getFont());
代码示例来源:origin: org.odftoolkit/simple-odf
FontMetrics fm = txtField.getFontMetrics(txtField.getFont());
代码示例来源:origin: apache/odftoolkit
FontMetrics fm = txtField.getFontMetrics(txtField.getFont());
内容来源于网络,如有侵权,请联系作者删除!