Home > Software design >  Can't display button
Can't display button

Time:09-22

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

    }

}

CodePudding user response:

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

import javax.swing.*;

public class test extends JFrame{

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

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

}
  • Related