简体   繁体   中英

Java - Why doesn't my buttons show in the panel?

I'm trying to learn Java Swing . Right now, I'm making a simple program and I need to make a button. I have two classes: driver and swing. I create the button and import the javax.swing.JButton and added the button. Finally, the button added to the panel but Idk why I just get the panel? Can anyone help me, please? Here's my code:

 import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

public class Swing extends JFrame {

    private JFrame f;
    private JButton button;
    private JLabel label;
    private JPanel panel; 

    public Swing() {
    }

    public Swing(String titleName) {
        creatButton();
        creatFrame(titleName);
    }

    public void creatButton() {
        JButton btn = new JButton("click me");
        JPanel panel = new JPanel();
        panel.add(btn);
        btn.setBounds(50, 100, 95, 30);
        add(panel);

    }

    private void creatFrame(String title) {
        JFrame f = new JFrame(title);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setSize(400, 500);
        f.setLayout(null);

    }

}
public class Driver {
    public static void main (String [] args) {
        new Swing ("calculator");
    }

}

Okay,lets start with...

JButton btn = new JButton("click me");
JPanel panel = new JPanel();
panel.add(btn);
btn.setBounds(50, 100, 95, 30);
add(panel);

You:

  • Create a button
  • Create a panel
  • You add the button to panel
  • You add the panel to the frame

And then...

JFrame f = new JFrame("calculator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new 
f.setVisible(true);

You create a brand new instance of JFrame and show it, but it has nothing on to it?! 😱!

Instead, you should avoid extending from JFrame and maybe use JPanel instead, something like...

public class Swing extends JPanel {

    private JButton button;
    private JLabel label;

    public Swing() {
        creatButton();
        add(button);
    }

    public void creatButton() {
        JButton btn = new JButton("click me");
    }
}

Then you can just create a window (or other container) and add it to it

JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Swing());
f.pack(); 
f.setLocationRelativeTo(null);
f.setVisible(true);

As a general rule, JFrame is a really poor extension point, it's a complex, compound component and locks you into a single use case. It's generally a better idea to start with something JPanel which provides you with a lot more flexibility and a lot less complexity and is easily reusable.

You really, really, really need to avoid null layouts

creatFrame is creating a new JFrame different than the frame itself (your Swing class extending JFrame).

Remove the line:

JFrame f = new JFrame(title);

and call the methods over this instead of f .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM