How to join a panel of your own definition
1 Importjavax.swing.*;2 Importjava.awt.event.*;3 Importjava.awt.*;4 5 class Mypanel extends JPanel {6 public void paintcomponent (Graphics g) {7 G.setcolor (color.orange ); 8 G.fillrect (20, 50, 100, 100); 9}10} One A - classGuiImplementsactionlistener{ - theJButton button =NewJButton ("Click Me"); -JFrame frame =NewJFrame (); - - Private voidSet_frame () { + frame.setdefaultcloseoperation (jframe.exit_on_close); -Frame.setsize (300, 200); +Frame.setvisible (true); A } at - Public voidShow_button () { - set_frame (); - Frame.getcontentpane (). Add (button); -Button.addactionlistener ( This); - } in - Public voidShow_my_panel () { to set_frame (); + Frame.getcontentpane (). Add (New Mypanel ()); - } the * Public voidactionperformed (ActionEvent event) { $Button.settext ("I ' ve been clicked!");Panax Notoginseng } - } the + classGuitest { A Public Static voidMain (string[] args) { theGUI GUI =NewGui (); + if(Args.length > 0 ) { - Gui.show_my_panel (); $ } $ Else { - Gui.show_button (); - } the } -}
Java Learning Note _gui (3)