通常,我只会JLabel
使用字符串作为第一个参数和JLabel.CENTER
第二个参数来创建;使用将标签添加到面板BorderLayout.CENTER
会导致标签中的文本在面板的中心对齐。
但是,我正在使用“RichJLabel”类来在我的文本上添加阴影。为此,它会Component.paintComponent
以丢失对齐信息的方式覆盖,并且无论我做什么,标签的文本都会绘制在面板的左上角。
据我了解,解决方法是将标签封装在另一个面板内;这样,我可以在父面板内对齐面板本身,但我不确定如何做到这一点。
我的完整目标是:
- 找出给定字符串需要什么字体大小才能填充其父 JPanel
- 为该文本添加阴影
- 在其 JPanel 中居中文本
这是我到目前为止所得到的:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class RichJLabel extends JLabel {
private int tracking;
public RichJLabel(String text, int tracking) {
super(text, JLabel.CENTER);
this.tracking = tracking;
}
private int left_x, left_y, right_x, right_y;
private Color left_color, right_color;
public void setLeftShadow(int x, int y, Color color) {
left_x = x;
left_y = y;
left_color = color;
}
public void setRightShadow(int x, int y, Color color) {
right_x = x;
right_y = y;
right_color = color;
}
public Dimension getPreferredSize() {
String text = getText();
FontMetrics fm = this.getFontMetrics(getFont());
int w = fm.stringWidth(text);
w += (text.length()-1)*tracking;
w += left_x + right_x;
int h = fm.getHeight();
h += left_y + right_y;
return new Dimension(w,h);
}
public void paintComponent(Graphics g) {
((Graphics2D)g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
char[] chars = getText().toCharArray();
FontMetrics fm = this.getFontMetrics(getFont());
int h = fm.getAscent();
int x = 0;
for(int i=0; i<chars.length; i++) {
char ch = chars[i];
int w = fm.charWidth(ch) + tracking;
g.setColor(left_color);
g.drawString(""+chars[i],x-left_x,h-left_y);
g.setColor(right_color);
g.drawString(""+chars[i],x+right_x,h+right_y);
g.setColor(getForeground());
g.drawString(""+chars[i],x,h);
x+=w;
}
((Graphics2D)g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
} // end paintComponent()
public static void main(String[] args) {
JPanel panel1 = new JPanel( new BorderLayout() );
panel1.setBackground( Color.BLUE );
panel1.setBorder( BorderFactory.createBevelBorder( BevelBorder.LOWERED ));
JPanel interiorPanel = new JPanel( new BorderLayout() );
panel1.add(interiorPanel, BorderLayout.CENTER);
RichJLabel label = new RichJLabel("100", 0);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
label.setVisible( true );
label.setForeground( Color.YELLOW );
interiorPanel.add(label, BorderLayout.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 140));
label.setFont(label.getFont().deriveFont(140f));
//resize code
Font labelFont = label.getFont();
String labelText = label.getText();
int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText);
int componentWidth = interiorPanel.getWidth();
// Find out how much the font can grow in width.
double widthRatio = (double)componentWidth / (double)stringWidth;
int newFontSize = (int)(labelFont.getSize() * widthRatio);
int componentHeight = interiorPanel.getHeight();
// Pick a new font size so it will not be larger than the height of label.
int fontSizeToUse = Math.min(newFontSize, componentHeight);
// Set the label's font size to the newly determined size.
label.setFont(new Font(labelFont.getName(), Font.BOLD, fontSizeToUse));
label.setLeftShadow(-3,-3,Color.BLACK);
// drop shadow w/ highlight
label.setRightShadow(2,3,Color.black);
label.setForeground(Color.gray);
JFrame frame = new JFrame("Label SSCCEE");
frame.getContentPane().add(panel1);
frame.pack();
frame.setVisible(true);
}
}
它现在所做的是: