Syed Danish
Syed Danish

Reputation: 29

Java Swing GUI buttons and textfields do not appear.This is a test code containing a Jbutton and a JTextField

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); 

    }
}

Upvotes: 0

Views: 71

Answers (2)

Malik Abdullah
Malik Abdullah

Reputation: 34

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).

Upvotes: 2

Patrick Chu
Patrick Chu

Reputation: 21

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

Upvotes: 1

Related Questions