Search code examples
javaswinguser-interfacelayoutnull-layout-manager

Can't display button


I can't display button and i don't know why Is there any way to fix it

I want it to appear at the top in the far left

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;

public class test extends JFrame{

    JButton b1 = new JButton("b1");
    
    public test() {
        
        b1.setBounds(0, 0, 125,100);
        
        add(b1);
        
    }

    public static void main(String[] args) {
            
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);
        f.setSize(925, 500);
        f.setVisible(true);

    }

}

Solution

  • I have edited your code like below to make it display the button

    import javax.swing.*;
    
    public class Test {
    
        public Test() {   
            JFrame f = new JFrame(); 
            JButton b1 = new JButton("b1");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLayout(null);
            f.setSize(925, 500);
            f.setVisible(true);
            b1.setBounds(0, 0, 125,100); 
            f.add(b1); 
        }
    
        public static void main(String[] args) {    
            new Test();
        }
    
    }