Alterar fonte do botão ao redimensioná-lo [RESOLVIDO]

4 respostas
M

Oii gente.. :)

Queria saber se tem como, ao redimensionar um JButton comum, o tamanho da fonte do seu texto fosse redimensionado também.. e não ficasse as reticencias, cortando a palavra :? Um amigo me indicou esse link para eu usar como base, dai fiz o seguinte:
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class ResizeButtonFont extends JButton {

    public static final int MIN_FONT_SIZE = 3;
    public static final int MAX_FONT_SIZE = 200;
    Graphics g;

    public ResizeButtonFont(String text) {
        super(text);
        init();
    }

    protected void init() {
        addComponentListener(new ComponentAdapter() {

            public void componentResized(ComponentEvent e) {
                adaptLabelFont(ResizeButtonFont.this);
            }
        });
    }

    protected void adaptLabelFont(JButton button) {
        if (g == null) {
            return;
        }
        Component[] components = button.getComponents();
        Rectangle r = button.getBounds();
        int fontSize = MIN_FONT_SIZE;
        Font f = button.getFont();

        Rectangle r1 = new Rectangle();
        Rectangle r2 = new Rectangle();
        while (fontSize < MAX_FONT_SIZE) {
            r1.setSize(getTextSize(button, f.deriveFont(f.getStyle(), fontSize)));
            r2.setSize(getTextSize(button, 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(JButton button, Font f) {
        Dimension size = new Dimension();
        g.setFont(f);
        FontMetrics fm = g.getFontMetrics(f);
        size.width = (fm.stringWidth(button.getText()));
        size.height = (fm.getHeight());

        return size;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.g = g;
    }

    public static void main(String[] args) throws Exception {
        ResizeButtonFont label = new ResizeButtonFont("Hello World");
        JFrame frame = new JFrame("Teste - fonte redimensionavel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(label);

        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

a fonte altera o tamanho, conforme eu redimensiono o frame.. só que ainda continua cortando a palavra :cry:
o que estou fazendo errado?

4 Respostas

Y

Isso não está acontecendo porque a fonte esta aumentando mais que o botão não? tenta diminuir o valor que ta aumentando a fonte.

M

você fala em diminuir o MAX_FONT_SIZE?
se é isso, ele aparece toda a palavra no inicio, mas quando eu diminuo o frame, as reticencias voltam…

A

Oi :smiley:

Ai vai uns links que talvez possa te ajudar:

http://www.guj.com.br/java/42116-organizar-botoes-e-metodo-setbounds

http://www.ebah.com.br/content/ABAAABkq8AC/interface-grafica

http://www.guj.com.br/java/231285-redimensionar-um-jbutton

http://www.guj.com.br/java/210845-resolvido-setbounds-e-pra-definir-o-lugar-ou-que

M

consegui resolver tirando a borda do botão..

ficou assim:

public ResizeButtonFont(String text) {
        super(text);
        setBorder(null); //<---- tira a borda

        init();
    }

achei aqui :D

Criado 10 de setembro de 2012
Ultima resposta 10 de set. de 2012
Respostas 4
Participantes 3