Eratys
Eratys

Reputation: 3

Java swing setSize

I have a problem, I set the size for frame, but doesn't work. I found a topic about this, but it's not helpful for me...

I tried with getContentPane().getSize(500,500), but nothing.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Mere extends JFrame{
    private JPanel b;
    public Mere(String titlu){
        setSize(500,500);
        setLayout(null);
        this.getSmth();
        pack();
        show();
    }
    public void getSmth(){
        JLabel user=new JLabel("User");
        user.setBounds(10,10,80,25);
        getContentPane().add(user);

        JTextField userText=new JTextField(20);
        userText.setBounds(100,10,160,25);
        getContentPane().add(userText);

        JPasswordField pas=new JPasswordField(20);
        pas.setBounds(100,40,160,25);
        getContentPane().add(pas);

        JButton n=new JButton("Register");
        n.setBounds(180,80,100,30);
        getContentPane().add(n);

        JLabel pass=new JLabel("Pass");
        pass.setBounds(10,40,100,25);
        getContentPane().add(pass);
        getContentPane().setVisible(true);
    }
    public static void main(String[]args){
        new Mere("yas");
    }
}

Upvotes: 0

Views: 8129

Answers (1)

Frakcool
Frakcool

Reputation: 11143

  1. You're calling pack(); after calling setSize().

    From the Oracle docs

    Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.

    So you rather override the getPreferredSize() method or remove the pack() call. See Should I avoid the use of setPreferred/Maximum/MinimumSize? (YES)

    However the best practice would be to use a layout manager (as explanined below) and then call pack() and let the manager do it's job while calculating the preferredSize for you

  2. Also you're using a null layout. Swing was designed to work with different PLAFs and screen sizes and resolutions, while pixel perfect GUIs might seem like the best and faster approach to make a complex GUI the more you advance in this the more problems you'll have in maintain it due to this, so go ahead and use a proper Layout Manager or combinations of them. See Null Layout is Evil and Why is it frowned upon to use a null layout in Java Swing?

  3. Another thing is that you're not placing your program on the Event Dispatch Thread (EDT), see SwingUtilities.invokeLater() why is it needed? and this answer for an example on how to use it

  4. Another thing I see is that you're extening JFrame which translated to english it says that your class is a JFrame, JFrame isn't flexible when you need to add it to another Component, instead you should create a JFrame instance and if you really need to extend something, extend from a JPanel.

Upvotes: 4

Related Questions