通常,我只是使用字符串作为第一个参数,并使用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);
}
}
现在它是做什么的:
最佳答案
该代码过早检查了容器的大小。在显示之前,它的宽度/高度为0。
修改后的代码
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.setLeftShadow(-3,-3,Color.BLACK);
// drop shadow w/ highlight
label.setRightShadow(2,3,Color.black);
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();
JFrame frame = new JFrame("Label SSCCEE");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(panel1);
frame.pack();
frame.setVisible(true);
int componentWidth = interiorPanel.getWidth();
String labelText = label.getText();
int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText);
// 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);
System.out.println("fontSizeToUse: " + fontSizeToUse);
if (fontSizeToUse<1) {
System.err.println("Font size less than 1!");
System.exit(1);
}
// Set the label's font size to the newly determined size.
label.setFont(new Font(labelFont.getName(), Font.BOLD, fontSizeToUse));
label.setForeground(Color.gray);
}
}