You have JLabels in your Swing GUI interface and are set to fill the empty spaces when the GUI interface is resized. You want the size of the text on the label to change accordingly when the size of the label changes.
One way to do it is to set a range of the text font size, add a ComponentListener to the label and reset the size of the text font. Below is a sample code.
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LabelFontResizeTest extends JLabel {
//The range of the font size
private static final int MIN_FONT_SIZE = 3;
private static final int MAX_FONT_SIZE = 240;
private Graphics g;
public LabelFontResizeTest(String text) {
super(text);
addComponentListener(new LabelListener());
}
//The ComponentListener that sets the font size
private class LabelListener extends ComponentAdapter {
@Override
public void componentResized(ComponentEvent e) {
if (g == null) {
return;
}
Rectangle r = LabelFontResizeTest.this.getBounds();
int fontSize = MIN_FONT_SIZE;
Font f = LabelFontResizeTest.this.getFont();
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
while (fontSize < MAX_FONT_SIZE) {
r1.setSize(getTextSize(LabelFontResizeTest.this, f.deriveFont(f.getStyle(), fontSize)));
r2.setSize(getTextSize(LabelFontResizeTest.this, f.deriveFont(f.getStyle(), fontSize + 1)));
if (r.contains(r1) && !r.contains(r2)) {
break;
}
fontSize++;
}
setFont(f.deriveFont(f.getStyle(), fontSize));
repaint();
}
}
private Dimension getTextSize(JLabel l, Font f) {
Dimension size = new Dimension();
g.setFont(f);
FontMetrics fm = g.getFontMetrics(f);
size.width = fm.stringWidth(l.getText());
size.height = fm.getHeight();
return size;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.g = g;
}
public static void main(String[] args) throws Exception {
LabelFontResizeTest label = new LabelFontResizeTest("Magic Text expand and shrinks automatically!!!");
JFrame frame = new JFrame("Resize label font");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.setSize(300, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Reference
1. How to change JLabel font size to fill JPanel free space while resizing?
One way to do it is to set a range of the text font size, add a ComponentListener to the label and reset the size of the text font. Below is a sample code.
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LabelFontResizeTest extends JLabel {
//The range of the font size
private static final int MIN_FONT_SIZE = 3;
private static final int MAX_FONT_SIZE = 240;
private Graphics g;
public LabelFontResizeTest(String text) {
super(text);
addComponentListener(new LabelListener());
}
//The ComponentListener that sets the font size
private class LabelListener extends ComponentAdapter {
@Override
public void componentResized(ComponentEvent e) {
if (g == null) {
return;
}
Rectangle r = LabelFontResizeTest.this.getBounds();
int fontSize = MIN_FONT_SIZE;
Font f = LabelFontResizeTest.this.getFont();
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
while (fontSize < MAX_FONT_SIZE) {
r1.setSize(getTextSize(LabelFontResizeTest.this, f.deriveFont(f.getStyle(), fontSize)));
r2.setSize(getTextSize(LabelFontResizeTest.this, f.deriveFont(f.getStyle(), fontSize + 1)));
if (r.contains(r1) && !r.contains(r2)) {
break;
}
fontSize++;
}
setFont(f.deriveFont(f.getStyle(), fontSize));
repaint();
}
}
private Dimension getTextSize(JLabel l, Font f) {
Dimension size = new Dimension();
g.setFont(f);
FontMetrics fm = g.getFontMetrics(f);
size.width = fm.stringWidth(l.getText());
size.height = fm.getHeight();
return size;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.g = g;
}
public static void main(String[] args) throws Exception {
LabelFontResizeTest label = new LabelFontResizeTest("Magic Text expand and shrinks automatically!!!");
JFrame frame = new JFrame("Resize label font");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.setSize(300, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Reference
1. How to change JLabel font size to fill JPanel free space while resizing?
-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.
No comments:
Post a Comment