Search code examples
javaswingsizeawtjtextfield

JTextField size can't be changed


public class AddMult extends JFrame {
    public AddMult() {
    setSize(600, 600);

    txtEnterNum = new JTextField();
    txtEnterNum.setSize(100,50);
    lblEnterNum.setLocation(100, 150);
    add(txtEnterNum);
    txtEnterNum.setVisible(true);


    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

} I tried to create a form and a JTextField on it. But the problem is, my location and size doesn't work. The TextField is filling the whole form. How can I fix it?


Solution

  • The size is not "working" due to the default layout (FlowLayout) in your JFrame. This layout doesn't let you change the size or location of any object. Try to use your own layout (modify x and y values in order to modify the size and location of your components):

    setLayout(new LayoutManager() {
            @Override
            public void addLayoutComponent(String name, Component comp) {
    
    
    
            }
    
            @Override
            public void removeLayoutComponent(Component comp) {
    
            }
    
            @Override
            public Dimension preferredLayoutSize(Container parent) {
                return null;
            }
    
            @Override
            public Dimension minimumLayoutSize(Container parent) {
                return null;
            }
    
            @Override
            public void layoutContainer(Container parent) {
    
            public void layoutContainer(Container parent) {
    
                float modifierx = Toolkit.getDefaultToolkit().getScreenSize().width/100;
    
                float Sizemodifier = Toolkit.getDefaultToolkit().getScreenSize().height/50;
    
                float modifiery = Toolkit.getDefaultToolkit().getScreenSize().height/150;
    
                int x=(int)(Toolkit.getDefaultToolkit().getScreenSize().width/modifierx);
    
                int y=(int)(Toolkit.getDefaultToolkit().getScreenSize().height/modifiery);
    
                int height =(int)(Toolkit.getDefaultToolkit().getScreenSize().height/Sizemodifier);
    
                int DX =(int)(Toolkit.getDefaultToolkit().getScreenSize().width/modifierx);
    
                int DY =(int)(Toolkit.getDefaultToolkit().getScreenSize().height/modifiery);
    
                for(int i = 0; i<parent.getComponentCount();i++){
    
                    if(i%2==0){
    
                        parent.getComponent(i).setBounds(x,y,x,height);
    
                        x+= DX;
    
                    }else{
    
                        parent.getComponent(i).setBounds(x,y,x,height);
    
                        x=Toolkit.getDefaultToolkit().getScreenSize().width/100;
    
                        y+=DY;
    
                    }
    
                }
            }
    
    
        });
    

    You have to set the layout in the constructor of the JFrame:

    frame() {
    
        setSize(600, 600);
        setLayout(new LayoutManager() {...}
    
        txtEnterNum = new JTextField();
        txtEnterNum.setSize(100, 50);
        setLocation(100, 150);
        add(txtEnterNum);
        txtEnterNum.setVisible(true);
    
    
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }