{"id":901,"date":"2021-01-11T21:16:06","date_gmt":"2021-01-11T21:16:06","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=901"},"modified":"2021-01-11T21:19:56","modified_gmt":"2021-01-11T21:19:56","slug":"labels","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/labels\/","title":{"rendered":"Labels"},"content":{"rendered":"\n<p><strong>Labels<\/strong> are simple text components.&nbsp; They simply display textual information;&nbsp; they do not accept input &#8212; in fact, <em>they do not accept input focus<\/em>. However, they can contain an image, which can be very useful at times.<\/p>\n\n\n\n<p>Here&#8217;s a simple example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; highlight: [7,8,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]; title: ; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.Color;\nimport java.awt.Container;\nimport java.awt.Font;\n\nimport javax.swing.ImageIcon;\nimport javax.swing.JLabel;\nimport javax.swing.JFrame;\nimport javax.swing.JPanel;\nimport javax.swing.SwingConstants;\n\nclass SimpleLabelsPanel extends JPanel\n{\n  public SimpleLabelsPanel()\n  {\n    JLabel look = new JLabel(&quot;Look at me!  I&#039;m a label!&quot;);\n    add(look);\n    JLabel whoopee = new JLabel(&quot;Whoopee!&quot;);\n    whoopee.setForeground(Color.red);\n    whoopee.setFont(\n        new Font(&quot;monospaced&quot;,\n                 Font.BOLD | Font.ITALIC, 18));\n    add(whoopee);\n    JLabel conservative = new JLabel(\n        &quot;Please quiet down up there; I&#039;m trying to sleep...&quot;);\n    conservative.setForeground(Color.blue);\n    conservative.setFont(new Font(&quot;serif&quot;, Font.PLAIN, 10));\n    add(conservative);\n    \n    ImageIcon&#x5B;] images = loadImages();\n    JLabel crossLabel =\n        new JLabel(&quot;I am a cross image plus text&quot;, images&#x5B;0],\n        SwingConstants.RIGHT);\n    add(crossLabel);\n    JLabel noughtLabel =\n        new JLabel(&quot;I am a nought image plus text&quot;, images&#x5B;1],\n        SwingConstants.CENTER);\n    add(noughtLabel);\n  }\n  \n  private ImageIcon&#x5B;] loadImages()\n  {\n    ImageIcon&#x5B;] icons = new ImageIcon&#x5B;2];\n    \/\/ Find the images relative to the current class\n    Class baseClass = SimpleLabelsPanel.class;\n    icons&#x5B;0] = new ImageIcon(\n        baseClass.getResource(&quot;..\/images\/cross.gif&quot;) );\n    icons&#x5B;1] = new ImageIcon(\n        baseClass.getResource(&quot;..\/images\/nought.gif&quot;) );\n    return icons;\n  }\n}\n\nclass SimpleLabelsFrame extends JFrame\n{\n  public SimpleLabelsFrame()\n  {\n    setTitle(&quot;SimpleLabel&quot;);\n    setSize(300, 200);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n    Container contentPane = getContentPane();\n    contentPane.add( new SimpleLabelsPanel() );\n  }\n}\n\npublic class SimpleLabels\n{\n  public static void main(String&#x5B;] args)\n  {\n    SimpleLabelsFrame frame = new SimpleLabelsFrame();\n    frame.setVisible(true);\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>which produces the following:<\/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\/Labels25.jpg\" alt=\"\" class=\"wp-image-905\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:31px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Labels are simple text components.&nbsp; They simply display textual information;&nbsp; they do not accept input &#8212; in fact, they do not accept input focus. However, they can contain an image, which can be very useful at times. Here&#8217;s a simple example: which produces the following:<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":71,"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-901","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":"Labels are simple text components.&nbsp; They simply display textual information;&nbsp; they do not accept input &#8212; in fact, they do not accept input focus. However, they can contain an image, which can be very useful at times. Here&#8217;s a simple example: which produces the following:","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/901","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=901"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/901\/revisions"}],"predecessor-version":[{"id":907,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/901\/revisions\/907"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/71"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}