Kevin Barz
Kevin Barz

Reputation: 41

Resizing Components and Font with MigLayout

i'm pretty new to Layout Managers and i have absolutely no idea how to resize the Font-Size automatically with the MigLayout Manager. I have already managed to resize the components with the grow and fill constraint, but I don't seem to get the font-size to change with the size of the components. How do i do this?

Here my few code lines:

public class Projekte {
public Projekte()
{
    main();
}
public static void main() {

    JFrame projekte = new JFrame();
    projekte.setBounds(100, 100,1080,1900);
    projekte.setExtendedState(Frame.MAXIMIZED_BOTH);
    projekte.setTitle("Testframe");
    projekte.getContentPane().setBackground(new Color(255,255,255));
    projekte.getContentPane().setLayout(new MigLayout("", "[][][][][][grow,fill][][][][]", "[][][][][][][][][][][grow,fill][][]"));

        JLabel lblTest = new JLabel("Test");
        projekte.getContentPane().add(lblTest, "cell 4 10,alignx trailing");
        JTextField textField = new JTextField();
        projekte.getContentPane().add(textField, "cell 5 10");
        textField.setColumns(10);
        JLabel lblTest_2 = new JLabel("Test_2");
        projekte.getContentPane().add(lblTest_2, "cell 6 10,alignx trailing");
        JTextField textField_2 = new JTextField();
        projekte.getContentPane().add(textField_2, "cell 7 10");
        textField_2.setColumns(10);
        JLabel lblTest_3 = new JLabel("Test_3");
        projekte.getContentPane().add(lblTest_3, "cell 4 11,alignx trailing");
        JTextField textField_3 = new JTextField();
        projekte.getContentPane().add(textField_3, "cell 5 11");
        textField_3.setColumns(10);
}
}

I think it is quite easy, but I don't seem to find the solution, maybe you can help.

Upvotes: 1

Views: 391

Answers (1)

Tamara Koliada
Tamara Koliada

Reputation: 1194

Font have attributes, but any of them relate to layout - you can manually scale font with some parameter using deriveFont(float size) - creates a new Font object by replicating the current Font object and applying a new size to it.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
float scale = screenWidth/1000;
label.getFont().deriveFont(scale);

Upvotes: 0

Related Questions