{"id":757,"date":"2021-01-11T17:59:29","date_gmt":"2021-01-11T17:59:29","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=757"},"modified":"2021-01-11T18:13:57","modified_gmt":"2021-01-11T18:13:57","slug":"buttons","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/buttons\/","title":{"rendered":"Buttons"},"content":{"rendered":"\n<p>Here&#8217;s an example of <em>action events<\/em>, using <em>Swing buttons<\/em>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [5,6,8,13,18,19,20,21,22,23,24,25,26,27,28,29,30,31,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.Color;\nimport java.awt.Container;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\n\nimport javax.swing.JButton;\nimport javax.swing.JFrame;\nimport javax.swing.JPanel;\n\nclass ColorButtonsPanel extends JPanel\n    implements ActionListener\n{\n  public ColorButtonsPanel()\n  {\n    setBackground(Color.LIGHT_GRAY);\n    add(m_yellow);\n    m_yellow.addActionListener(this);\n    add(m_blue);\n    m_blue.addActionListener(this);\n    add(m_orange);\n    m_orange.addActionListener(this);\n    add(m_cyan);\n    m_cyan.addActionListener(this);\n    add(m_pink);\n    m_pink.addActionListener(this);\n    add(m_red);\n    m_red.addActionListener(this);\n    add(m_white);\n    m_white.addActionListener(this);\n  }\n  \n  \/\/ ActionListener required methods\n  public void actionPerformed(ActionEvent evt)\n  {\n    JButton button = (JButton) evt.getSource();\n    Color back = Color.BLACK;\n    if (button == m_yellow)\n      back = Color.YELLOW;\n    else if (button == m_blue)\n      back = Color.BLUE;\n    else if (button == m_orange)\n      back = Color.ORANGE;\n    else if (button == m_cyan)\n      back = Color.cyan;\n    else if (button == m_pink)\n      back = Color.PINK;\n    else if (button == m_red)\n      back = Color.RED;\n    else if (button == m_white)\n      back = Color.WHITE;\n    setBackground(back);\n    repaint();\n  }\n  \n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/ Data \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n  private JButton m_yellow = new JButton(&quot;Yellow&quot;);\n  private JButton m_blue   = new JButton(&quot;Blue&quot;);\n  private JButton m_orange = new JButton(&quot;Orange&quot;);\n  private JButton m_cyan   = new JButton(&quot;Cyan&quot;);\n  private JButton m_pink   = new JButton(&quot;Pink&quot;);\n  private JButton m_red    = new JButton(&quot;Red&quot;);\n  private JButton m_white  = new JButton(&quot;White&quot;);\n}\n\nclass ColorButtonsFrame extends JFrame\n{\n  public ColorButtonsFrame()\n  {\n    setTitle(&quot;ColorButtons&quot;);\n    setSize(300, 200);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n    Container contentPane = getContentPane();\n    contentPane.add( new ColorButtonsPanel() );\n  }\n}\n\npublic class ColorButtonsDisplay\n{\n  public static void main(String&#x5B;] args)\n  {\n    ColorButtonsFrame frame = new ColorButtonsFrame();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>which produces this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ColorButtons1.gif\" alt=\"\" class=\"wp-image-761\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ColorButtons2.gif\" alt=\"\" class=\"wp-image-762\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ColorButtons3.gif\" alt=\"\" class=\"wp-image-765\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ColorButtons4.gif\" alt=\"\" class=\"wp-image-766\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:38px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Of course, the first is the original display, and the others are the result of clicking on the appropriate button to change the background color.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example of action events, using Swing buttons: which produces this: Of course, the first is the original display, and the others are the result of clicking on the appropriate button to change the background color.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":69,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-757","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Here&#8217;s an example of action events, using Swing buttons: which produces this: Of course, the first is the original display, and the others are the result of clicking on the appropriate button to change the background color.","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=757"}],"version-history":[{"count":8,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/757\/revisions"}],"predecessor-version":[{"id":772,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/757\/revisions\/772"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/69"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}