Alterar tamanho jLabel + jTextField + jFormatedField

2 respostas
N

Boa noite!

tenho um cadastro com muito campos… rotulos… combobox etc…

como faço para alterar em tempo de execução o tamanho dos campos, jlabels etc… pois a pessoa que esta irá usar o sistema nao exerga direito rs rs rsrs

Obrigado!

2 Respostas

E

Se você usou corretamente os layouts do Java (em vez de usar Null ou Absolute Layout), em tese seria simplesmente você acrescentar um shortcut de teclado (como Ctrl+"+" e Ctrl-"-") e criar uma Action para percorrer os componentes dessa tela, e ir aumentando o tamanho das fontes de cada um dos JComponents.
Se precisar aumentar também as fontes de componentes como menus etc. talvez seja necessário também mexer em alguns settings do seu Look & Feel.

E

Um exemplo bem bobo. Use Control + “+” do teclado numérico, e Control + “-” do teclado numérico.

package guj;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class ExemploFontesVariaveis extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JPanel pnlBotoes = null;
    private JButton btnOK = null;

    private JButton btnCancel = null;
    private JPanel pnlComponentes = null;
    private JLabel lblCombo = null;
    private JComboBox cboCombo = null;
    private JLabel lblRadios = null;
    private JPanel pnlRadios = null;
    private JRadioButton rdoRadio1 = null;
    private JRadioButton rdoRadio2 = null;
    private JLabel lblTextField = null;
    private JTextField txtTextField = null;
    private ButtonGroup btgRadios = null;

    private JPanel getPnlBotoes() {
        if (pnlBotoes == null) {
            pnlBotoes = new JPanel();
            pnlBotoes.setLayout(new FlowLayout());
            pnlBotoes.add(getBtnOK(), null);
            pnlBotoes.add(getBtnCancel(), null);
        }
        return pnlBotoes;
    }

    private JButton getBtnOK() {
        if (btnOK == null) {
            btnOK = new JButton();
            btnOK.setText("OK");
        }
        return btnOK;
    }

    private JButton getBtnCancel() {
        if (btnCancel == null) {
            btnCancel = new JButton();
            btnCancel.setText("Cancelar");
        }
        return btnCancel;
    }

    private JPanel getPnlComponentes() {
        if (pnlComponentes == null) {
            lblTextField = new JLabel();
            lblTextField.setText("Text Field:");
            lblRadios = new JLabel();
            lblRadios.setText("Radios:");
            lblCombo = new JLabel();
            lblCombo.setText("Combo:");
            GridLayout gridLayout = new GridLayout();
            gridLayout.setRows(3);
            gridLayout.setHgap(2);
            gridLayout.setVgap(2);
            gridLayout.setColumns(2);
            pnlComponentes = new JPanel();
            pnlComponentes.setLayout(gridLayout);
            pnlComponentes.add(lblCombo, null);
            pnlComponentes.add(getCboCombo(), null);
            pnlComponentes.add(lblRadios, null);
            pnlComponentes.add(getPnlRadios(), null);
            pnlComponentes.add(lblTextField, null);
            pnlComponentes.add(getTxtTextField(), null);
        }
        return pnlComponentes;
    }

    private JComboBox getCboCombo() {
        if (cboCombo == null) {
            cboCombo = new JComboBox();
            ((DefaultComboBoxModel) cboCombo.getModel()).addElement("Exemplo 1");
            ((DefaultComboBoxModel) cboCombo.getModel()).addElement("Exemplo 2");
        }
        return cboCombo;
    }

    private JPanel getPnlRadios() {
        if (pnlRadios == null) {
            pnlRadios = new JPanel();
            pnlRadios.setLayout(new FlowLayout());
            pnlRadios.add(getRdoRadio1(), null);
            pnlRadios.add(getRdoRadio2(), null);
            btgRadios = new ButtonGroup();
            btgRadios.add(getRdoRadio1());
            btgRadios.add(getRdoRadio2());
        }
        return pnlRadios;
    }

    private JRadioButton getRdoRadio1() {
        if (rdoRadio1 == null) {
            rdoRadio1 = new JRadioButton();
            rdoRadio1.setText("Radio 1");
        }
        return rdoRadio1;
    }

    private JRadioButton getRdoRadio2() {
        if (rdoRadio2 == null) {
            rdoRadio2 = new JRadioButton();
            rdoRadio2.setText("Radio 2");
        }
        return rdoRadio2;
    }

    private JTextField getTxtTextField() {
        if (txtTextField == null) {
            txtTextField = new JTextField();
        }
        return txtTextField;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ExemploFontesVariaveis thisClass = new ExemploFontesVariaveis();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

    public ExemploFontesVariaveis() {
        super();
        initialize();
    }

    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJContentPane());
        this.setTitle("Exemplo com fontes variáveis");
    }

    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getPnlBotoes(), BorderLayout.SOUTH);
            jContentPane.add(getPnlComponentes(), BorderLayout.CENTER);
        }
        return jContentPane;
    }

    @Override
    protected JRootPane createRootPane() {
        JRootPane pane = super.createRootPane();
        KeyStroke ctrlPlus = KeyStroke.getKeyStroke(KeyEvent.VK_ADD, KeyEvent.CTRL_DOWN_MASK);
        KeyStroke ctrlMinus = KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.CTRL_DOWN_MASK);
        pane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Dimension d = ExemploFontesVariaveis.this.getSize ();
                d.height = (int) (0.9 * d.height);
                d.width = (int) (0.9 * d.width);
                ExemploFontesVariaveis.this.setSize(d);
                reduceAllFonts(getRootPane());
            }
        }, ctrlMinus, JComponent.WHEN_IN_FOCUSED_WINDOW);
        pane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Dimension d = ExemploFontesVariaveis.this.getSize ();
                d.height = (int) (1.1 * d.height);
                d.width = (int) (1.1 * d.width);
                ExemploFontesVariaveis.this.setSize(d);
                enlargeAllFonts(getRootPane());
            }
        }, ctrlPlus, JComponent.WHEN_IN_FOCUSED_WINDOW);
        return pane;
    }

    private void reduceAllFonts(JComponent comp) {
        for (Component sub : comp.getComponents()) {
            Font font = sub.getFont();
            if (font != null) {
                Font newFont;
                float fontSize = font.getSize2D();
                newFont = font.deriveFont(fontSize * 0.9f);
                sub.setFont(newFont);
            }
            if (sub instanceof JComponent)
                reduceAllFonts((JComponent) sub);
        }
    }

    private void enlargeAllFonts(JComponent comp) {
        for (Component sub : comp.getComponents()) {
            Font font = sub.getFont();
            if (font != null) {
                Font newFont;
                float fontSize = font.getSize2D();
                newFont = font.deriveFont(fontSize * 1.1f);
                sub.setFont(newFont);
            }
            if (sub instanceof JComponent)
                enlargeAllFonts((JComponent) sub);
        }
    }
}
Criado 13 de julho de 2011
Ultima resposta 14 de jul. de 2011
Respostas 2
Participantes 2