0

this is the main class.button first don't show up first but when you hover the the mouse over the position of button it shows up. but this is not the case with the textfield which is not showing up in any case.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test {
    public static void main(String[] args){

        JFrame f=new JFrame("Calculator");
        f.setSize(450,450);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        JPanel p=new JPanel();
        f.add(p);

        JTextField t=new JTextField(20);
        t.setBounds(5, 5, 0, 0);
        t.setLayout(null);
        p.add(t);

        JButton clear=new JButton("C");        
        clear.setBounds(5,100,50,50);
        clear.setSize(50,40);
        p.add(clear); 

    }
}
  • 1) `t.setLayout(null);` Since nothing (no component) is added to the text field, that will have no effect. But don't call it on any component. 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). 2) `t.setBounds(5, 5, 0, 0);` The last two arguments are width and height, .. – Andrew Thompson May 21 '17 at 07:49
  • .. so that component will not appear at all, unless the layout forces it to be larger. Also, the layout (of the parent panel, a flow layout) would typically ignore the set location of a component. 3) Swing GUIs should be started on the EDT. 4) And most importantly, move the call to `setVisible(..)` to after all components have been added, and the GUI has been laid out by calling `pack()`. – Andrew Thompson May 21 '17 at 07:52
  • 5) `JButton clear=new JButton("C");` it seems strange to give other programmers more information (i.e. `clear`) than is offered to the user (i.e. `C`). As a user I'd be wondering if it meant .. `Clear`? `Copy`.. ? 6) As an aside, it seems pretty clear that you have not yet done the [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/) lesson of the tutorial. Example code from around the internet, and asking questions on SO, are not substitutes for **doing the official tutorial.** 7) Provide ASCII art or a simple drawing .. – Andrew Thompson May 21 '17 at 08:01
  • .. of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson May 21 '17 at 08:04
  • @AndrewThompson thanks for all this bundle of information. it really is very helpful for me. but i am just a beginner who is learning basics and also this is my first assignment of GUI (i.e to make a basic calculator). so i hope that i will learn this all one day specially because of the help of people like you. – Syed Danish May 21 '17 at 08:43

2 Answers2

2
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test {
    public static void main(String[] args){

        JFrame f=new JFrame("Calculator");
        f.setSize(450,450);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p=new JPanel();
        f.add(p);

        JTextField t=new JTextField(20);
        t.setBounds(5, 5, 0, 0);
        t.setLayout(null);
        p.add(t);

        JButton clear=new JButton("C");        
        clear.setBounds(5,100,50,50);
        clear.setSize(50,40);
        p.add(clear); 
        f.setVisible(true);

    }
}

Try this (use f.setVisible(true) last).

SilverNak
  • 3,283
  • 4
  • 28
  • 44
1

Try enlarging the program window once you run the program. The button and text field should appear then.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433