简体   繁体   中英

My GUI does not print anything at all

MyFrame(){
    String titel = "Rezept";
    JLabel label = new JLabel(titel);
    JButton abutton = new JButton("Abbrechen");
    JButton sbutton = new JButton("Speichern");

    JPanel jp1 = new JPanel();
    jp1.setLayout(new FlowLayout(FlowLayout.CENTER));
    jp1.add(label);

    JPanel jp2 = new JPanel();
    jp2.setLayout(new GridLayout(1,2));
    jp2.add(abutton);
    jp2.add(sbutton);

    setLayout(new BorderLayout());
    setSize(300,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    add(jp1, BorderLayout.NORTH);
    add(jp2, BorderLayout.SOUTH);
}

    public static void main(String[]args){
       new MyFrame();
    } 

I just don't get it. When I run this GUI it doesn't print out anything at all.

I only get an empty frame. What am I doing wrong here?

(Code is in "MyFrame extends JFrame" class)

you will never see anything if you call the method setVisible(true); before adding the JPanels...

change the order to something like:

add(jp1, BorderLayout.NORTH);
add(jp2, BorderLayout.SOUTH);
setVisible(true);

after that you should see something like

在此处输入图片说明

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