最近将Swing项目转换为JavaFX。我需要转换的一个功能是我的ghetto JLabel自动调整大小。这是我的贫民窟Swing代码的半部分。
package driver;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class AutoResizeJLabel {
public static void createGUI(){
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = jFrame.getContentPane();
JLabel jLabel = new JLabel("HELLO");
jLabel.setName("Hello");
jLabel.setBackground(Color.lightGray);
jLabel.setOpaque(true);
jLabel.addComponentListener(new ComponentListener(){
@Override
public void componentResized(ComponentEvent e) {
new Thread(new Runnable(){
public void run(){
Font font = jLabel.getFont();
int height = jLabel.getHeight();
int width = jLabel.getWidth();
int fontSize = font.getSize();
int size = determineFontSize(jLabel.getText(), jLabel.getFontMetrics(font), fontSize, width, height);
jLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, size));
}
}, "Get new size").start();
}
@Override
public void componentMoved(ComponentEvent e) {}
@Override
public void componentShown(ComponentEvent e) {}
@Override
public void componentHidden(ComponentEvent e) {}
});
container.add(jLabel);
jFrame.setSize(500, 500);
jFrame.setVisible(true);
}
/**
* Method : Calculates the Middle Point of Graphic with Text
* @param s : Text
* @param g : Graphics
* @param width : Width of the Component
* @param height : Height of the Component
* @return Middle Point
*/
public static Point determinePoint(String s, Graphics g, int width, int height){
//Initialize variables
int stringWidth = 0;
int stringAccent = 0;
int x = 0;
int y = 0;
// get the FontMetrics for the current font
FontMetrics fm = g.getFontMetrics();
// find the center location to display
stringWidth = fm.stringWidth(s);
stringAccent = fm.getAscent();
//get the position of the left most character in the baseline
x = (width / 2) - (stringWidth / 2);
y = ((height - fm.getHeight()) / 2) + stringAccent;
Point p = new Point(x, y);
return p;
}
/**
* Method : Determine font size of Component
* @param s : Text
* @param fM : FontMetrics of Component
* @param fontSize : Original Font size
* @param width : Width of Component
* @param height : Height of Component
* @return new font size
* Credit : coobird over at Stack Overflow
*/
public static int determineFontSize(String s, FontMetrics fM, int fontSize, int width, int height){
//String Variables needed
String stringText = s;
if(stringText == null){
stringText = new String("");
}
int stringWidth = fM.stringWidth(stringText);
//int stringHeight = fM.getHeight();
// Find out how much the font can grow in width.
double widthRatio = (double)width / (double)stringWidth;
int newFontSize = (int)(fontSize * widthRatio);
//Determine new Font Size
int fontSizeToUse = Math.min(newFontSize, height);
return fontSizeToUse;
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createGUI();
}
});
}
}
我说是半因为我实际上是使用图形对象而不是使用setText,因为我发现如果有意义的话,setText与文本的位置紧密相关。
无论如何,我想知道/希望有一种更简单的方法让文本自动调整,以便在JavaFX中填充整个Label对象。我是JavaFX的新手,无法真正找到FontMetrics的等价物。