Java Learning Note _gui (5)

Source: Internet
Author: User

Demo how to create a corresponding response function for a different button

1 Importjavax.swing.*;2 Importjava.awt.event.*;3 Importjava.awt.*;4 5 classMypanel_2extendsJPanel {6      Public voidpaintcomponent (Graphics g) {7G.fillrect (0, 0, This. GetWidth (), This. GetHeight ());8         intRed = (int) (Math.random () * 255);9         intGreen = (int) (Math.random () * 255);Ten         intBlue = (int) (Math.random () * 255); OneColor Randomcolor =NewColor (red, green, blue); A G.setcolor (randomcolor); -G.filloval (70, 40, 100, 100); -     } the } -  - classgui{ -      +JButton button =NewJButton ("Change Color"); -JButton Button_change_label =NewJButton ("Change label"); +JLabel label =NewJLabel ("I ' m a label"); AJFrame frame =NewJFrame (); at      -     Private voidSet_frame () { - frame.setdefaultcloseoperation (jframe.exit_on_close); -Frame.setsize (800, 600); -Frame.setvisible (true); -     } in      -      Public voidShow_my_panel () { to set_frame (); + Frame.getcontentpane (). Add (Borderlayout.south, button); -Button.addactionlistener (NewColorbuttonactionlistener ()); the  * Frame.getcontentpane (). Add (Borderlayout.east, Button_change_label); $Button_change_label.addactionlistener (NewLabelbuttonactionlistener ());Panax Notoginseng          - Frame.getcontentpane (). Add (borderlayout.west, label); the          +Frame.getcontentpane (). Add (Borderlayout.center,Newmypanel_2 ()); A     } the  +     classColorbuttonactionlistenerImplementsActionListener { -          Public voidactionperformed (ActionEvent event) { $ frame.repaint (); $         } -     } -  the     classLabelbuttonactionlistenerImplementsActionListener { -          Public voidactionperformed (ActionEvent event) {WuyiLabel.settext ("That hurt!"); the         } -     } Wu } -  About classGuitest { $      Public Static voidMain (string[] args) { -GUI GUI =NewGui (); - Gui.show_my_panel (); -     } A}

Java Learning Note _gui (5)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.