0

I want to add two text fields in the panel. The layout of the panel is a Grid Layout. I add setBounds for the height and width of the text field but setBounds is not working properly. This is my code...

  import java.awt.*;
  import javax.swing.*;
  class gui1
    {
        public static void main(String[] args)
        {
            JFrame frm = new JFrame();
            JPanel pan = new JPanel();
            Button btn = new Button("SUBMIT");
            
            TextField txt1 = new TextField();
            TextField txt2 = new TextField();
            Label lbl = new Label("LOGIN FORM");
            
            txt1.setBounds(20,20,100,200);
            txt2.setBounds(20,20,200,300);
            frm.setLayout(new BorderLayout());
            pan.setLayout(new GridLayout(1,2));
            pan.add(txt1);
            pan.add(txt2);
            frm.add(pan,BorderLayout.CENTER);
            frm.add(lbl,BorderLayout.NORTH);
            frm.setSize(800,500);
            
            frm.setVisible(true);
            frm.setDefaultCloseOperation(frm.EXIT_ON_CLOSE);
       }
    }
  • 1
    Layout managers are there to calculate the bounds, so setting bounds manually is not needed. Add sketch of the desired layout. – c0der Jul 20 '21 at 16:04
  • how I set the size of text field – Syed Talal Jilani Jul 20 '21 at 16:06
  • The layout manager does it for you: https://replit.com/@c0derrepo/KaleidoscopicLuckyBloatware#Main.java . See [A Visual Guide to Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and [How to set the height and the width of a textfield in Java?](https://stackoverflow.com/questions/14805124/how-to-set-the-height-and-the-width-of-a-textfield-in-java) – c0der Jul 20 '21 at 16:12
  • 1
    Firstly, don't mix Swing and AWT components. Use all Swing. *"how I set the size of text field"*, the easiest way is via the constructor that accepts a number of columns. The referred size will be further altered by the `Font` size used. **General Tips:** 1) Change `frm.setSize(800,500); frm.setVisible(true);` (the 1st line is a guess, and invariably wrong) to `frm.pack(); frm.setVisible(true);`. 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. .. – Andrew Thompson Jul 20 '21 at 16:23
  • 2
    .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) One of the (many) reasons to avoid trying to set bounds of components is seen in `txt1.setBounds(20,20,100,200); txt2.setBounds(20,20,200,300);` - they have the same x,y starting position and thus the 2nd .. – Andrew Thompson Jul 20 '21 at 16:24
  • 1
    .. would obscure the first. **Learn ho to use (and combine) layouts.** – Andrew Thompson Jul 20 '21 at 16:27

1 Answers1

1

The layout manager will reset or ignore manually set values depending on the manager. Don't set UI bounds/size manually. We have layout managers for a reason; use them. As a general rule, if you're putting layouts inside of other layouts, you're doing it wrong. You can likely do what you want by using a different manager. I would suggest using GridBagLayout. It's a little more complicated to use, but you'll get better results in the long run.

Ryan
  • 1,762
  • 6
  • 11