本文介绍了在面板中居中字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我正在做这件事:
pre>
public void paintComponent(Graphics g){
super.paintComponent(g);
int stringWidth = 0;
int stringAccent = 0;
int xCoordinate = 0;
int yCoordinate = 0;
//获取当前字体的FontMetrics
FontMetrics fm = g.getFontMetrics();
$ b / **显示新消息* /
if(currentMessage.equals(message1)){
removeAll();
/ **居中文本* /
//找到显示的中心位置
stringWidth = fm.stringWidth(message2);
stringAccent = fm.getAscent();
//获取基线中最左边字符的位置
xCoordinate = getWidth()/ 2 - stringWidth / 2;
yCoordinate = getHeight()/ 2 + stringAccent / 2;
//绘制字符串
g.drawString(message2,xCoordinate,yCoordinate);
currentMessage = message2; // alternate message
}
}
有没有一种方法可以用来简化这个任务?
例如:
public void paintComponent(Graphics g){
super.paintComponent(g);
if(currentMessage.equals(message1)){
removeAll();
//绘制字符串居中(用一行代码)
}
}
看来我只是为了中心文本而做了很多工作。
在java标准中图书馆?不,你可以随时做这些事情。 private void drawStringMiddleOfPanel(String string,Graphics g){
String message2 = string;
int stringWidth = 0;
int stringAccent = 0;
int xCoordinate = 0;
int yCoordinate = 0;
//获取当前字体的FontMetrics
FontMetrics fm = g.getFontMetrics();
/ **显示新消息* /
/ **居中文本* /
//找到要显示的中心位置
stringWidth = fm.stringWidth(消息2);
stringAccent = fm.getAscent();
//获取基线中最左边字符的位置
xCoordinate = getWidth()/ 2 - stringWidth / 2;
yCoordinate = getHeight()/ 2 + stringAccent / 2;
//绘制字符串
g.drawString(message2,xCoordinate,yCoordinate);
currentMessage = message2; //备用信息
}
使用
public void paintComponent(Graphics g){
super.paintComponent(g);
if(currentMessage.equals(message1)){
removeAll();
drawStringMiddleOfPanel(message1,g);
}
}
I'm trying to center a String in a Panel.
Currently I'm doing this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
int stringWidth = 0;
int stringAccent = 0;
int xCoordinate = 0;
int yCoordinate = 0;
// get the FontMetrics for the current font
FontMetrics fm = g.getFontMetrics();
/** display new message */
if (currentMessage.equals(message1)) {
removeAll();
/** Centering the text */
// find the center location to display
stringWidth = fm.stringWidth(message2);
stringAccent = fm.getAscent();
// get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - stringWidth / 2;
yCoordinate = getHeight() / 2 + stringAccent / 2;
// draw String
g.drawString(message2, xCoordinate, yCoordinate);
currentMessage = message2; // alternate message
}
}
Is there a single method I can use to simplify this task?
For example:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (currentMessage.equals(message1)) {
removeAll();
// draw String centered (with one line of code)
}
}
Just seems like I'm doing a lot of work just to center text.
解决方案
In the java standard library? No, you can always do something along this
private void drawStringMiddleOfPanel(String string, Graphics g) {
String message2 = string;
int stringWidth = 0;
int stringAccent = 0;
int xCoordinate = 0;
int yCoordinate = 0;
// get the FontMetrics for the current font
FontMetrics fm = g.getFontMetrics();
/** display new message */
/** Centering the text */
// find the center location to display
stringWidth = fm.stringWidth(message2);
stringAccent = fm.getAscent();
// get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - stringWidth / 2;
yCoordinate = getHeight() / 2 + stringAccent / 2;
// draw String
g.drawString(message2, xCoordinate, yCoordinate);
currentMessage = message2; // alternate message
}
With
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (currentMessage.equals(message1)) {
removeAll();
drawStringMiddleOfPanel(message1, g);
}
}
这篇关于在面板中居中字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!