Click here to Skip to main content
14,449,574 members
Rate this:
Please Sign up or sign in to vote.
See more:
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JLabel;

public class ResizeLabelFont
  extends JLabel
  private static final long serialVersionUID = 8642870745555129506L;
  public static final int MIN_FONT_SIZE = 1;
  public static final int MAX_FONT_SIZE = 30;
  Graphics a;
  public ResizeLabelFont(String text) {
  protected void init() {
    addComponentListener(new ComponentAdapter(this)
          public void componentResized(ComponentEvent e) { this.a.adaptLabelFont(this.a); }
  protected void adaptLabelFont(JLabel l) {
    Font f = l.getFont();
    int maxHeight = l.getHeight() - 10;
    int maxWidth = l.getWidth() - 10;
    int fontHeight = 1;
    int fontWidth = 1;
    int fontSize = 1;
    while (fontHeight < maxHeight && fontWidth < maxWidth && fontSize < 30) {
      fontHeight = l.getFontMetrics(l.getFont()).getHeight();
      fontWidth = l.getFontMetrics(l.getFont()).stringWidth(l.getText());

    setFont(f.deriveFont(f.getStyle(), fontSize));
  protected void paintComponent(Graphics g) {
    this.a = g;

What I have tried:

Can I please have some help converting the following Java code into C++?
Updated 23-Dec-19 9:05am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Converting from one language to another is not always straightforward. You can try online converters (Google will find them), or if you are skilled at both languages then doing it line by line may be the best option.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You cant convert it directly because the JLabel isnt a C++ class and there are other classes in C++.

You must first check which control in C++ you want to change and then understands its functions for changing the wanted properties. My tip is that you may use an CStatic, but you may write the app at first. Take the time to understand the GetTextExtend explained in this article.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100