0

I am new to Java and am trying to run this code. What could be error in the following code—my JTextField, txtfld, is shown just as a line instead of as a full text box?

public class calculator
{
    public static void main(String s[])
    {
        JFrame j=new JFrame();
        j.setSize(400,600);

        JPanel p1=new JPanel();
        JPanel p2=new JPanel();
        p1.setSize(400, 100);
        p2.setSize(400, 500);
        p1.setLocation(0, 0);
        p2.setLocation(0, 100);

        p2.setLayout(new GridLayout(4,4));
        j.add(p1);
        j.add(p2);


        JTextField txtfld=new JTextField();
        txtfld.setSize(390, 92);
        txtfld.setLocation(5, 2);
        //txtfld.setVisible(true);    
        p1.add(txtfld);

        j.setVisible(true);

    }
}
ostrichofevil
  • 749
  • 7
  • 19
  • 2
    I'm sure you don't write your code left justified, so fix the code with proper formatting to make it easier for us to read the code. Also class names should start with an upper case character. Follow Java standards. – camickr Apr 02 '17 at 04:07
  • Could you send a screenshot of the `JTextField`? – ostrichofevil Apr 02 '17 at 04:24
  • 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Apr 02 '17 at 05:58
  • .. 4) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). – Andrew Thompson Apr 02 '17 at 05:59
  • .. 5) 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. – Andrew Thompson Apr 02 '17 at 06:12

1 Answers1

3
JTextField txtfld=new JTextField();  

You need to give a hint to the layout manager what the size should be.

So you should use something like:

JTextField txtfld=new JTextField(10);  

Now the preferred size will be such that 10 "W" characters can be displayed in the text field before scrolling is required.

You should also pack() the frame before making it visible:

j.pack();
j.setVisible();

This will allow the frame to display all the components at their preferred sizes.

Also, get rid of all the setSize() and setLocation() statements. It is the job of the layout manager to set the size and location. Those values will be recalculated by the layout manager.

j.add(p1);
j.add(p2);

The default layout manager for a frame is a BorderLayout. So the above code will cause p2 to replace p1 on the frame.

Basically your entire code is wrong.

Start by reading the section from the Swing tutorial on How to Use Layout Managers for more information and working examples to get you started. Maybe start with the BorderLayout example, since this is the default layout of the frame you need to understand how it works first.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • thanks for your reply.actually i am making a calculator so i am setting size and location to leaving some padding on sides and in between buttons.should i still use layout manager? – kamalpreet kaur Apr 02 '17 at 04:18
  • *" leaving some padding on sides and in between buttons"* 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).** – Andrew Thompson Apr 02 '17 at 05:59
  • @kamalpreetkaur, - `i am setting size and location to leaving some padding on sides and in between buttons` - read the tutorial. There is a section on `How to Use Borders` for leaving space on the sides. Each layout manager will also have different parameters or ways to add space between the components. – camickr Apr 02 '17 at 18:40