Java 旋转缩放组件的文本字体
我想在ComponentResistized事件上设置按钮的字体大小。我得到屏幕大小和按钮的大小。但无法计算要设置的首选字体大小。如果按钮宽度增加/减少,字体大小也应相应增加/减少。我也无法获得图形对象Java 旋转缩放组件的文本字体,java,swing,fonts,Java,Swing,Fonts,我想在ComponentResistized事件上设置按钮的字体大小。我得到屏幕大小和按钮的大小。但无法计算要设置的首选字体大小。如果按钮宽度增加/减少,字体大小也应相应增加/减少。我也无法获得图形对象 这个问题的解决方案是什么?这更多的是暴力解决方案 1) 您可以尝试使用以下操作获取字体度量: // get metrics from the graphics FontMetrics metrics = graphics.getFontMetrics(font); // get the heig
这个问题的解决方案是什么?这更多的是暴力解决方案 1) 您可以尝试使用以下操作获取字体度量:
// get metrics from the graphics
FontMetrics metrics = graphics.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public final class ButtonFrame extends JFrame {
ButtonFrame(String buttonText) {
final JButton button = new JButton(buttonText);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
float fittedFontSize = 1.0f;
while (getFittedText(button, fittedFontSize += 1.0f).equals(button.getText()));
button.setFont(button.getFont().deriveFont(fittedFontSize - 1.0f));
button.revalidate();
button.repaint();
}
});
getContentPane().add(button);
}
private String getFittedText(JButton button, float fontSize) {
Insets i = button.getInsets();
Rectangle viewRect = new Rectangle();
Rectangle textRect = new Rectangle();
Rectangle iconRect = new Rectangle();
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = button.getWidth() - (i.right + viewRect.x);
viewRect.height = button.getHeight() - (i.bottom + viewRect.y);
textRect.x = textRect.y = textRect.width = textRect.height = 0;
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
return SwingUtilities.layoutCompoundLabel(
button,
button.getFontMetrics(button.getFont().deriveFont(fontSize)),
button.getText(),
button.getIcon(),
button.getVerticalAlignment(),
button.getHorizontalAlignment(),
button.getVerticalTextPosition(),
button.getHorizontalTextPosition(),
viewRect,
textRect,
iconRect,
button.getIconTextGap());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new ButtonFrame("sample text");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
});
}
}
2) 然后可以搜索适合组件的字体大小
Font savedFont = oldFont;
for (int i = 0; i < 100; i++) {
Font newFont = new Font(oldFont.getFontName(), oldFont.getStyle(), i);
Dimension d = getFontSize(g,newFont,text);
if(componentSize.height < d.height || componentSize.width < d.width){
return savedFont;
}
savedFont = newFont;
}
Font savedFont=oldFont;
对于(int i=0;i<100;i++){
Font newFont=新字体(oldFont.getFontName(),oldFont.getStyle(),i);
维度d=getFontSize(g,新字体,文本);
if(componentSize.height
将其全部放在一起(注意,这未经测试)
公共维度getFontSize(图形、字体、字符串文本){
//从图形中获取度量
FontMetrics=graphics.getFontMetrics(字体);
//获取此字体中文本行的高度并呈现上下文
int hgt=metrics.getHeight();
//在此字体和渲染上下文中获取我的文本的进度
int adv=metrics.stringWidth(文本);
//计算一个框的大小以容纳带有一些填充的文本。
尺寸=新尺寸(adv+2,hgt+2);
返回大小;
}
公共字体findFont(标注组件大小、字体oldFont、字符串文本、图形g){
//最多搜索100个
Font savedFont=oldFont;
对于(int i=0;i<100;i++){
Font newFont=新字体(oldFont.getFontName(),oldFont.getStyle(),i);
维度d=getFontSize(g,新字体,文本);
if(componentSize.height
使用组件进行编辑以获取FontMetrics
public Dimension getFontSize(FontMetrics metrics ,Font font, String text){
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);
return size;
}
public Font findFont(Component component, Dimension componentSize, Font oldFont, String text){
//search up to 100
Font savedFont = oldFont;
for (int i = 0; i < 100; i++) {
Font newFont = new Font(oldFont.getFontName(), oldFont.getStyle(), i);
Dimension d = getFontSize(component.getFontMetrics(newFont),newFont,text);
if(componentSize.height < d.height || componentSize.width < d.width){
return savedFont;
}
savedFont = newFont;
}
return oldFont;
}
公共维度getFontSize(FontMetrics度量、字体、字符串文本){
//获取此字体中文本行的高度并呈现上下文
int hgt=metrics.getHeight();
//在此字体和渲染上下文中获取我的文本的进度
int adv=metrics.stringWidth(文本);
//计算一个框的大小以容纳带有一些填充的文本。
尺寸=新尺寸(adv+2,hgt+2);
返回大小;
}
公共字体findFont(组件、维度组件大小、字体oldFont、字符串文本){
//最多搜索100个
Font savedFont=oldFont;
对于(int i=0;i<100;i++){
Font newFont=新字体(oldFont.getFontName(),oldFont.getStyle(),i);
维度d=getFontSize(组件.getFontMetrics(newFont)、newFont、text);
if(componentSize.height
您可以创建一个带有paintComponent的类ResizingButton,该paintComponent封装了super.paintComponent。然后在(Graphics2D)图形上进行大小转换。原因是:字体大小是逐步调整的 如果您深入研究字体大小调整,请使用FontRenderingContext和分数度量
针对评论,一些代码:
public class ResizingButton extends JButton {
@Override
protected void paintComponent(Graphics g) {
int h = this.getHeight();
final int DEFAULT_H = 26;
double resizal = ((double)h) / DEFAULT_H;
String t = getText();
setText("<html><span style='font-size:" + (resizal*11) + "'>" + t);
super.paintComponent(g);
setText(t);
}
public类ResizingButton扩展了JButton{
@凌驾
受保护组件(图形g){
int h=this.getHeight();
最终int默认值_H=26;
double resizal=((double)h)/DEFAULT_h;
字符串t=getText();
setText(“+t”);
超级组件(g);
setText(t);
}
我发现上面的(Java 6/7)可以工作。Graphics2D.scale也可以缩放边框,因此太麻烦了。如果您希望在调整大小时自动调整字体大小,可以尝试以下方法:
// get metrics from the graphics
FontMetrics metrics = graphics.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public final class ButtonFrame extends JFrame {
ButtonFrame(String buttonText) {
final JButton button = new JButton(buttonText);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
float fittedFontSize = 1.0f;
while (getFittedText(button, fittedFontSize += 1.0f).equals(button.getText()));
button.setFont(button.getFont().deriveFont(fittedFontSize - 1.0f));
button.revalidate();
button.repaint();
}
});
getContentPane().add(button);
}
private String getFittedText(JButton button, float fontSize) {
Insets i = button.getInsets();
Rectangle viewRect = new Rectangle();
Rectangle textRect = new Rectangle();
Rectangle iconRect = new Rectangle();
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = button.getWidth() - (i.right + viewRect.x);
viewRect.height = button.getHeight() - (i.bottom + viewRect.y);
textRect.x = textRect.y = textRect.width = textRect.height = 0;
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
return SwingUtilities.layoutCompoundLabel(
button,
button.getFontMetrics(button.getFont().deriveFont(fontSize)),
button.getText(),
button.getIcon(),
button.getVerticalAlignment(),
button.getHorizontalAlignment(),
button.getVerticalTextPosition(),
button.getHorizontalTextPosition(),
viewRect,
textRect,
iconRect,
button.getIconTextGap());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new ButtonFrame("sample text");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
});
}
}
将文本设置为最后一个答案会导致CPU上的高负载。 我的建议是相应地缩放字体:
public class ScalableJButton extends JButton {
int mCurrentSize = 0;
Font mInitialFont = null;
int mInitialHeight;
private static final long serialVersionUID = 1L;
public ScalableJButton(String pString) {
super(pString);
init();
}
public ScalableJButton() {
super();
init();
}
private void init() {
mInitialFont = getFont();
}
@Override
protected void paintComponent(Graphics g) {
if (mInitialHeight == 0) {
mInitialHeight = getHeight();
}
int resizal = this.getHeight() * mInitialFont.getSize() / mInitialHeight;
if(resizal != mCurrentSize){
setFont(mInitialFont.deriveFont((float) resizal));
mCurrentSize = resizal;
}
super.paintComponent(g);
}
}
重复?另请参见。如何获取图形-不使用绘画(图形g)。只需使用普通按钮并尝试计算。如何获取图形或其他。图形用于获取FontMetrics。您也可以使用此组件。getFontMetrics(字体);你能给我一个小剪贴画来了解这个想法吗。调整按钮大小这是按钮的一个组件-子类吗???对于更具响应性的版本,你可以覆盖JButton.paintComponent(),而不是使用组件侦听器。