Java Swing JFrame Layout

前端 未结 6 676
面向向阳花
面向向阳花 2020-12-09 19:53

I just wrote a simple code where I want a textfield and a button to appear on the main frame, but after running all I see is the textfield.

If I write the code of th

6条回答
  •  醉酒成梦
    2020-12-09 20:16

    Add your components to a JPanel and then add that panel to the ContentPane of JFrame.

    JFrame window = new JFrame();
    JPanel mainframe = new JPanel();
    
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(0,0,200,200);
    
    JButton jb = new JButton();
    jb.setText("Leech");
    
    mainframe.add(jb);
    
    JTextField link = new JTextField(50);
    mainframe.add(link);
    
    window.getContentPane().add(mainframe);
    window.pack();
    window.setVisible(true);
    

提交回复
热议问题