Ning2018 / JavaFundamentals

0 stars 0 forks source link

JavaSwing panel #8

Open Ning2018 opened 6 years ago

Ning2018 commented 6 years ago

From multiple resources: A JFrame can't carry components. JFrame has several layers, but the main one for adding components is called "content pane". To get this pane, Container contentPane = frame.getContentPane(); To add panels to frames: Container contentPane = frame.getContentPane(); contentPane.add(new MyPanel()); You need to define your own MyPanel() class, which should extend the JPanel class,

WindowListener is an interface with 7 methods to handle events of various kinds ("window closing event" is the one of interest here). AWT provides a default implementation called WindowAdapter (found in java.awt.event.*) where all these 7 methods are null! But you can just extend this class and write any non-null methods to override the default: