{"id":27229,"date":"2021-05-14T23:10:07","date_gmt":"2021-05-15T03:10:07","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/a-demo-providing-multiple-buttons-to-select-a-playing-card-a-panel-using-cardlayout-control-which-of-four-possible-subpanels-holding-a-different-card-to-display-programming-code-examples-java-j2e\/"},"modified":"2021-05-14T23:10:07","modified_gmt":"2021-05-15T03:10:07","slug":"a-demo-providing-multiple-buttons-to-select-a-playing-card-a-panel-using-cardlayout-control-which-of-four-possible-subpanels-holding-a-different-card-to-display-programming-code-examples-java-j2e","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27229","title":{"rendered":"A demo providing multiple buttons to select a playing card-A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing"},"content":{"rendered":"<pre>\n#######################\n# CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images:\n    * CardPanel.java A Panel that displays a playing card.\n    * ImageLabel.java A Canvas for displaying images.\n    * Ace.gif, King.gif, Queen.gif, Jack.gif.\n#####################\nimport java.applet.Applet;\nimport java.awt.*;\nimport java.awt.event.*;\n\n\/** An example of CardLayout. The right side of the window holds\n *  a Panel that uses CardLayout to control four possible\n *  subpanels (each of which is a CardPanel that shows a\n *  picture of a playing card). The buttons on the left side\n *  of the window manipulate the &quot;cards&quot; in this layout by\n *  calling methods in the right-hand panel's layout manager.\n *\n *********************************\n\npublic class CardDemo extends Applet implements ActionListener {\n  private Button first, last, previous, next;\n  private String[] cardLabels = { &quot;Jack&quot;,&quot;Queen&quot;,&quot;King&quot;,&quot;Ace&quot; };\n  private CardPanel[] cardPanels = new CardPanel[4];\n  private CardLayout layout;\n  private Panel cardDisplayPanel;\n\n  public void init() {\n    setBackground(Color.white);\n    setLayout(new BorderLayout());\n    addButtonPanel();\n    addCardDisplayPanel();\n  }\n\n  private void addButtonPanel() {\n    Panel buttonPanel = new Panel();\n    buttonPanel.setLayout(new GridLayout(9, 1));\n    Font buttonFont = new Font(&quot;SansSerif&quot;, Font.BOLD, 18);\n    buttonPanel.setFont(buttonFont);\n    for(int i=0; i&lt;cardlabels .length; i++) {\n      Button button = new Button(cardLabels[i]);\n      button.addActionListener(this);\n      buttonPanel.add(button);\n    }\n    first = new Button(&quot;First&quot;);\n    first.addActionListener(this);\n    last = new Button(&quot;Last&quot;);\n    last.addActionListener(this);\n    previous = new Button(&quot;Previous&quot;);\n    previous.addActionListener(this);\n    next = new Button(&quot;Next&quot;);\n    next.addActionListener(this);\n    buttonPanel.add(new Label(&quot;------------&quot;, Label.CENTER));\n    buttonPanel.add(first);\n    buttonPanel.add(last);\n    buttonPanel.add(previous);\n    buttonPanel.add(next);\n    add(buttonPanel, BorderLayout.WEST);\n  }\n\n  private void addCardDisplayPanel() {\n    cardDisplayPanel = new Panel();\n    layout = new CardLayout();\n    cardDisplayPanel.setLayout(layout);\n    String cardName;\n    for(int i=0; i&lt;cardLabels.length; i++) {\n      cardName = cardLabels[i];\n      cardPanels[i] =\n        new CardPanel(cardName, getCodeBase(),\n                      &quot;images\/&quot; + cardName + &quot;.gif&quot;);\n      cardDisplayPanel.add(cardPanels[i], cardName);\n    }\n    add(cardDisplayPanel, BorderLayout.CENTER);\n  }\n\n  public void actionPerformed(ActionEvent event) {\n    Button source = (Button)event.getSource();\n    if (source == first)\n      layout.first(cardDisplayPanel);\n    else if (source == last)\n      layout.last(cardDisplayPanel);\n    else if (source == previous)\n      layout.previous(cardDisplayPanel);\n    else if (source == next)\n      layout.next(cardDisplayPanel);\n    else\n      layout.show(cardDisplayPanel, source.getLabel());\n    return;\n  }\n}\n******************\nCardPanel.java A Panel that displays a playing card. \n&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;\nimport java.awt.*;\nimport java.net.*;\n\n\/** A Panel that displays a playing card. This window does\n *  <b>not use CardLayout. Rather, instances of CardPanel\n *  are contained in another window used in the CardDemo\n *  example. It is this enclosing window that uses CardLayout\n *  to manipulate which CardPanel it shows.\n *\n *************************\n\npublic class CardPanel extends Panel {\n  private Label name;\n  private ImageLabel picture;\n\n  public CardPanel(String cardName,\n                   URL directory, String imageFile) {\n    setLayout(new BorderLayout());\n    name = new Label(cardName, Label.CENTER);\n    name.setFont(new Font(&quot;SanSerif&quot;, Font.BOLD, 50));\n    add(name, BorderLayout.NORTH);\n    picture = new ImageLabel(directory, imageFile);\n    Panel picturePanel = new Panel();\n    picturePanel.add(picture);\n    add(picturePanel, BorderLayout.CENTER);\n    setSize(getPreferredSize());\n  }\n\n  public Label getLabel() {\n    return(name);\n  }\n\n  public ImageLabel getImageLabel() {\n    return(picture);\n  }\n}\n*******************\nImageLabel.java A Canvas for displaying images.\n*******************\nimport java.awt.*;\nimport java.net.*; \n\n\/** A class for displaying images. It places the Image\n *  into a canvas so that it can moved around by layout\n *  managers, will get repainted automatically, etc.\n *  No mouseXXX or action events are defined, so it is\n *  most similar to the Label Component.\n *  <p>\n *  By default, with FlowLayout the ImageLabel takes\n *  its minimum size (just enclosing the image). The\n *  default with BorderLayout is to expand to fill\n *  the region in width (North\/South), height\n *  (East\/West) or both (Center). This is the same\n *  behavior as with the builtin Label class. If you\n *  give an explicit setSize or\n *  setBounds call <b>before<\/b> adding the\n *  ImageLabel to the Container, this size will\n *  override the defaults.\n *  <\/p><p>\n *  Here is an example of its use:\n *  <\/p><p>\n *  <pre>\n *  public class ShowImages extends Applet {\n *    private ImageLabel image1, image2;\n *\n *    public void init() {\n *      image1 = new ImageLabel(getCodeBase(),\n *                              &quot;some-image.gif&quot;);\n *      image2 = new ImageLabel(getCodeBase(),\n *                              &quot;other-image.jpg&quot;);\n *      add(image1);\n *      add(image2);\n *    }\n *  }\n *  <\/pre>\n<p> *<br \/>\n  *\/<\/p>\n<p>public class ImageLabel extends Canvas {<br \/>\n  \/\/ Instance variables.<\/p>\n<p>  \/\/ The actual Image drawn on the canvas.<br \/>\n  private Image image;<\/p>\n<p>  \/\/ A String corresponding to the URL of the image you will<br \/>\n  \/\/ get if you call the constructor with no arguments.<br \/>\n  private static String defaultImageString<br \/>\n    = &quot;http:\/\/java.sun.com\/lib\/images\/&quot; +<br \/>\n      &quot;logo.java.color-transp.55x60.gif&quot;;<\/p>\n<p>  \/\/ The URL of the image. But sometimes we will use an existing<br \/>\n  \/\/ image object (e.g. made by createImage) for which this info<br \/>\n  \/\/ will not be available, so a default string is used here.<br \/>\n  private String imageString = &quot;&quot;;<\/p>\n<p>  \/\/ Turn this on to get verbose debugging messages.<br \/>\n  private boolean debug = false;<\/p>\n<p>  \/** Amount of extra space around the image. *\/<\/p>\n<p>  private int border = 0;<\/p>\n<p>  \/** If there is a non-zero border, what color should it be?<br \/>\n   *  Default is to use the background color of the Container.<br \/>\n   *\/<\/p>\n<p>  private Color borderColor = null;<\/p>\n<p>  \/\/ Width and height of the Canvas. This is the<br \/>\n  \/\/  width\/height of the image plus twice the border.<br \/>\n  private int width, height;<\/p>\n<p>  \/** Determines if it will be sized automatically. If the user<br \/>\n   *  issues a setSize() or setBounds()call before adding the<br \/>\n   *  label to the Container, or if the LayoutManager resizes<br \/>\n   *  before drawing (as with BorderLayout), then those sizes<br \/>\n   *  override the default, which is to make the label the same<br \/>\n   *  size as the image it holds (after reserving space for the<br \/>\n   *  border, if any). This flag notes this, so subclasses that<br \/>\n   *  override ImageLabel need to check this flag, and if it is<br \/>\n   *  true, and they draw modified image, then they need to draw<br \/>\n   *  them based on the width height variables, not just blindly<br \/>\n   *  drawing them full size.<br \/>\n   *\/<\/p>\n<p>  private boolean explicitSize = false;<br \/>\n  private int explicitWidth=0, explicitHeight=0;<\/p>\n<p>  \/\/ The MediaTracker that can tell if image has been loaded<br \/>\n  \/\/ before trying to paint it or setSize based on its size.<br \/>\n  private MediaTracker tracker;<\/p>\n<p>  \/\/ Used by MediaTracker to be sure image is loaded before<br \/>\n  \/\/ paint &amp; setSize, since you can't find out the size until it<br \/>\n  \/\/ is done loading.<br \/>\n  private static int lastTrackerID=0;<br \/>\n  private int currentTrackerID;<br \/>\n  private boolean doneLoading = false;<\/p>\n<p>  private Container parentContainer;<\/p>\n<p>  \/** Create an ImageLabel with the default image.<br \/>\n   *<br \/>\n   * @see #getDefaultImageString<br \/>\n   * @see #setDefaultImageString<br \/>\n   *\/<br \/>\n  \/\/ Remember that the funny &quot;this()&quot; syntax calls<br \/>\n  \/\/ constructor of same class<br \/>\n  public ImageLabel() {<br \/>\n    this(defaultImageString);<br \/>\n  }<\/p>\n<p>  \/** Create an ImageLabel using the image at URL<br \/>\n   *  specified by the string.<br \/>\n   *<br \/>\n   * @param imageURLString A String specifying the<br \/>\n   *   URL of the image.<br \/>\n   *\/<\/p>\n<p>  public ImageLabel(String imageURLString) {<br \/>\n    this(makeURL(imageURLString));<br \/>\n  }<\/p>\n<p>  \/** Create an ImageLabel using the image at URL<br \/>\n   *  specified.<br \/>\n   *<br \/>\n   * @param imageURL The URL of the image.<br \/>\n   *\/<\/p>\n<p>  public ImageLabel(URL imageURL) {<br \/>\n    this(loadImage(imageURL));<br \/>\n    imageString = imageURL.toExternalForm();<br \/>\n  }<\/p>\n<p>  \/** Create an ImageLabel using the image in the file<br \/>\n   *  in the specified directory.<br \/>\n   *<br \/>\n   * @param imageDirectory Directory containing image<br \/>\n   * @param file Filename of image<br \/>\n   *\/<\/p>\n<p>  public ImageLabel(URL imageDirectory, String file) {<br \/>\n    this(makeURL(imageDirectory, file));<br \/>\n    imageString = file;<br \/>\n  }<\/p>\n<p>  \/** Create an ImageLabel using the image specified. The other<br \/>\n   *  constructors eventually call this one, but you may want<br \/>\n   *  to call it directly if you already have an image (e.g.<br \/>\n   *  created through createImage).<br \/>\n   *<br \/>\n   * @param image The image<br \/>\n   *\/<\/p>\n<p>  public ImageLabel(Image image) {<br \/>\n    this.image = image;<br \/>\n    tracker = new MediaTracker(this);<br \/>\n    currentTrackerID = lastTrackerID++;<br \/>\n    tracker.addImage(image, currentTrackerID);<br \/>\n  }<\/p>\n<p>  \/** Makes sure that the Image associated with the Canvas is<br \/>\n   *  done loading before returning, since loadImage spins off<br \/>\n   *  a separate thread to do the loading. Once you get around<br \/>\n   *  to drawing the image, this will make sure it is loaded,<br \/>\n   *  waiting if not. The user does not need to call this at<br \/>\n   *  all, but if several ImageLabels are used in the same<br \/>\n   *  Container, this can cause several repeated layouts, so<br \/>\n   *  users might want to explicitly call this themselves before<br \/>\n   *  adding the ImageLabel to the Container. Another<br \/>\n   *  alternative is to start asynchronous loading by calling<br \/>\n   *  prepareImage on the ImageLabel's image (see getImage).<br \/>\n   *<br \/>\n   * @param doLayout Determines if the Container should be<br \/>\n   *   re-laid out after you are finished waiting. <b>This<br \/>\n   *   should be true when called from user functions<\/b>,<br \/>\n   *   but is set to false when called from getPreferredSize<br \/>\n   *   to avoid an infinite loop. This is needed when using<br \/>\n   *   BorderLayout, which calls getPreferredSize<br \/>\n   *   <b>before<\/b> calling paint.<br \/>\n   *\/<\/p>\n<p>  public void waitForImage(boolean doLayout) {<br \/>\n    if (!doneLoading) {<br \/>\n      debug(&quot;[waitForImage] - Resizing and waiting for &quot;<br \/>\n            + imageString);<br \/>\n      try { tracker.waitForID(currentTrackerID); }<br \/>\n      catch (InterruptedException ie) {}<br \/>\n      catch (Exception e) {<br \/>\n        System.out.println(&quot;Error loading &quot;<br \/>\n                           + imageString + &quot;: &quot;<br \/>\n                           + e.getMessage());<br \/>\n        e.printStackTrace();<br \/>\n      }<br \/>\n      if (tracker.isErrorID(0))<br \/>\n        new Throwable(&quot;Error loading image &quot;<br \/>\n                      + imageString).printStackTrace();<br \/>\n      doneLoading = true;<br \/>\n      if (explicitWidth != 0)<br \/>\n        width = explicitWidth;<br \/>\n      else<br \/>\n        width = image.getWidth(this) + 2*border;<br \/>\n      if (explicitHeight != 0)<br \/>\n        height = explicitHeight;<br \/>\n      else<br \/>\n        height = image.getHeight(this) + 2*border;<br \/>\n      setSize(width, height);<br \/>\n      debug(&quot;[waitForImage] - &quot; + imageString + &quot; is &quot;<br \/>\n            + width + &quot;x&quot; + height + &quot;.&quot;);<\/p>\n<p>      \/\/ If no parent, you are OK, since it will have<br \/>\n      \/\/ been resized before being added. But if<br \/>\n      \/\/ parent exists, you have already been added,<br \/>\n      \/\/ and the change in size requires re-layout.<br \/>\n      if (((parentContainer = getParent()) != null)<br \/>\n          &amp;&amp; doLayout) {<br \/>\n        setBackground(parentContainer.getBackground());<br \/>\n        parentContainer.doLayout();<br \/>\n      }<br \/>\n    }<br \/>\n  }<\/p>\n<p>  \/** Moves the image so that it is <i>centered<\/i> at<br \/>\n   *  the specified location, as opposed to the setLocation<br \/>\n   *  method of Component which places the top left<br \/>\n   *  corner at the specified location.<br \/>\n   *  <\/p>\n<p>\n   *  <b>Note:<\/b> The effects of this could be undone<br \/>\n   *  by the LayoutManager of the parent Container, if<br \/>\n   *  it is using one. So this is normally only used<br \/>\n   *  in conjunction with a null LayoutManager.<br \/>\n   *<br \/>\n   * @param x The X coord of center of the image<br \/>\n   *          (in parent's coordinate system)<br \/>\n   * @param y The Y coord of center of the image<br \/>\n   *          (in parent's coordinate system)<br \/>\n   * @see java.awt.Component#setLocation<br \/>\n   *\/<\/p>\n<p>  public void centerAt(int x, int y) {<br \/>\n    debug(&quot;Placing center of &quot; + imageString + &quot; at (&quot;<br \/>\n          + x + &quot;,&quot; + y + &quot;)&quot;);<br \/>\n    setLocation(x - width\/2, y - height\/2);<br \/>\n  }<\/p>\n<p>  \/** Determines if the x and y <b>(in the ImageLabel's<br \/>\n   *  own coordinate system)<\/b> is inside the<br \/>\n   *  ImageLabel. Put here because Netscape 2.02 has<br \/>\n   *  a bug in which it doesn't process contains() and<br \/>\n   *  locate() tests correctly.<br \/>\n   *\/<\/p>\n<p>  public synchronized boolean contains(int x, int y) {<br \/>\n    return((x &gt;= 0) &amp;&amp; (x = 0) &amp;&amp; (y &lt; = height));<br \/>\n  }<\/p>\n<p>  \/** Draws the image. If you override this in a<br \/>\n   *  subclass, be sure to call super.paint.<br \/>\n   *\/<\/p>\n<p>  public void paint(Graphics g) {<br \/>\n    if (!doneLoading)<br \/>\n      waitForImage(true);<br \/>\n    else {<br \/>\n      if (explicitSize)<br \/>\n        g.drawImage(image, border, border,<br \/>\n                    width-2*border, height-2*border,<br \/>\n                    this);<br \/>\n      else<br \/>\n        g.drawImage(image, border, border, this);<br \/>\n      drawRect(g, 0, 0, width-1, height-1,<br \/>\n               border, borderColor);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  \/** Used by layout managers to calculate the usual<br \/>\n   *  size allocated for the Component. Since some<br \/>\n   *  layout managers (e.g. BorderLayout) may<br \/>\n   *  call this before paint is called, you need to<br \/>\n   *  make sure that the image is done loading, which<br \/>\n   *  will force a setSize, which determines the values<br \/>\n   *  returned.<br \/>\n   *\/<br \/>\n  public Dimension getPreferredSize() {<br \/>\n    if (!doneLoading)<br \/>\n      waitForImage(false);<br \/>\n    return(super.getPreferredSize());<br \/>\n  }<\/p>\n<p>  \/** Used by layout managers to calculate the smallest<br \/>\n   *  size allocated for the Component. Since some<br \/>\n   *  layout managers (e.g. BorderLayout) may<br \/>\n   *  call this before paint is called, you need to<br \/>\n   *  make sure that the image is done loading, which<br \/>\n   *  will force a setSize, which determines the values<br \/>\n   *  returned.<br \/>\n   *\/<br \/>\n   public Dimension getMinimumSize() {<br \/>\n     if (!doneLoading)<br \/>\n       waitForImage(false);<br \/>\n     return(super.getMinimumSize());<br \/>\n   }<\/p>\n<p>  \/\/ LayoutManagers (such as BorderLayout) might call<br \/>\n  \/\/ setSize or setBounds with only 1 dimension of<br \/>\n  \/\/ width\/height non-zero. In such a case, you still<br \/>\n  \/\/ want the other dimension to come from the image<br \/>\n  \/\/ itself.<\/p>\n<p>  \/** Resizes the ImageLabel. If you don&#039;t setSize the<br \/>\n   *  label explicitly, then what happens depends on<br \/>\n   *  the layout manager. With FlowLayout, as with<br \/>\n   *  FlowLayout for Labels, the ImageLabel takes its<br \/>\n   *  minimum size, just enclosing the image. With<br \/>\n   *  BorderLayout, as with BorderLayout for Labels,<br \/>\n   *  the ImageLabel is expanded to fill the<br \/>\n   *  section. Stretching GIF\/JPG files does not always<br \/>\n   *  result in clear looking images. <b>So just as<br \/>\n   *  with builtin Labels and Buttons, don't<br \/>\n   *  use FlowLayout if you don't want the Buttons to<br \/>\n   *  get resized. If you don't use any<br \/>\n   *  LayoutManager, then the ImageLabel will also<br \/>\n   *  just fit the image.<br \/>\n   *  <\/b><\/p>\n<p>\n   *  Note that if you setSize explicitly, you must do<br \/>\n   *  it <b>before<\/b> the ImageLabel is added to the<br \/>\n   *  Container. In such a case, the explicit size<br \/>\n   *  overrides the image dimensions.<br \/>\n   *<br \/>\n   * @see #setBounds<br \/>\n   *\/<\/p>\n<p>  public void setSize(int width, int height) {<br \/>\n    if (!doneLoading) {<br \/>\n      explicitSize=true;<br \/>\n      if (width &gt; 0)<br \/>\n        explicitWidth=width;<br \/>\n      if (height &gt; 0)<br \/>\n        explicitHeight=height;<br \/>\n    }<br \/>\n    super.setSize(width, height);<br \/>\n  }<\/p>\n<p>  \/** Resizes the ImageLabel. If you don't setSize the<br \/>\n   *  label explicitly, then what happens depends on<br \/>\n   *  the layout manager. With FlowLayout, as with<br \/>\n   *  FlowLayout for Labels, the ImageLabel takes its<br \/>\n   *  minimum size, just enclosing the image. With<br \/>\n   *  BorderLayout, as with BorderLayout for Labels,<br \/>\n   *  the ImageLabel is expanded to fill the<br \/>\n   *  section. Stretching GIF\/JPG files does not always<br \/>\n   *  result in clear looking images. <b>So just as<br \/>\n   *  with builtin Labels and Buttons, don't<br \/>\n   *  use FlowLayout if you don't want the Buttons to<br \/>\n   *  get resized.<\/b> If you don't use any<br \/>\n   *  LayoutManager, then the ImageLabel will also<br \/>\n   *  just fit the image.<br \/>\n   *  <\/p>\n<p>\n   *  Note that if you setSize explicitly, you must do<br \/>\n   *  it <b>before<\/b> the ImageLabel is added to the<br \/>\n   *  Container. In such a case, the explicit size<br \/>\n   *  overrides the image dimensions.<br \/>\n   *<br \/>\n   * @see #setSize<br \/>\n   *\/<\/p>\n<p>  public void setBounds(int x, int y,<br \/>\n                      int width, int height) {<br \/>\n    if (!doneLoading) {<br \/>\n      explicitSize=true;<br \/>\n      if (width &gt; 0)<br \/>\n        explicitWidth=width;<br \/>\n      if (height &gt; 0)<br \/>\n        explicitHeight=height;<br \/>\n    }<br \/>\n    super.setBounds(x, y, width, height);<br \/>\n  }<\/p>\n<p>  \/\/ You can't just set the background color to<br \/>\n  \/\/ the borderColor and skip drawing the border,<br \/>\n  \/\/ since it messes up transparent gifs. You<br \/>\n  \/\/ need the background color to be the same as<br \/>\n  \/\/ the container.<\/p>\n<p>  \/** Draws a rectangle with the specified OUTSIDE<br \/>\n   *  left, top, width, and height.<br \/>\n   *  Used to draw the border.<br \/>\n   *\/<\/p>\n<p>  protected void drawRect(Graphics g,<br \/>\n                          int left, int top,<br \/>\n                          int width, int height,<br \/>\n                          int lineThickness,<br \/>\n                          Color rectangleColor) {<br \/>\n    g.setColor(rectangleColor);<br \/>\n    for(int i=0; i&lt;linethickness ; i++) {<br \/>\n      g.drawRect(left, top, width, height);<br \/>\n      if (i &lt; lineThickness-1) {  \/\/ Skip last iteration<br \/>\n        left = left + 1;<br \/>\n        top = top + 1;<br \/>\n        width = width - 2;<br \/>\n        height = height - 2;<br \/>\n      }<br \/>\n    }<br \/>\n  }<\/p>\n<p>  \/** Calls System.out.println if the debug variable<br \/>\n   *  is true; does nothing otherwise.<br \/>\n   *<br \/>\n   * @param message The String to be printed.<br \/>\n   *\/<\/p>\n<p>  protected void debug(String message) {<br \/>\n    if (debug)<br \/>\n      System.out.println(message);<br \/>\n  }<\/p>\n<p>  \/\/ Creates the URL with some error checking.<\/p>\n<p>  private static URL makeURL(String s) {<br \/>\n    URL u = null;<br \/>\n    try { u = new URL(s); }<br \/>\n    catch (MalformedURLException mue) {<br \/>\n      System.out.println(&quot;Bad URL &quot; + s + &quot;: &quot; + mue);<br \/>\n      mue.printStackTrace();<br \/>\n    }<br \/>\n    return(u);<br \/>\n  }<\/p>\n<p>  private static URL makeURL(URL directory,<br \/>\n                             String file) {<br \/>\n    URL u = null;<br \/>\n    try { u = new URL(directory, file); }<br \/>\n    catch (MalformedURLException mue) {<br \/>\n      System.out.println(&quot;Bad URL &quot; +<br \/>\n                         directory.toExternalForm() +<br \/>\n                         &quot;, &quot; + file + &quot;: &quot; + mue);<br \/>\n      mue.printStackTrace();<br \/>\n    }<br \/>\n    return(u);<br \/>\n  }<\/p>\n<p>  \/\/ Loads the image. Needs to be static since it is<br \/>\n  \/\/ called by the constructor.<\/p>\n<p>  private static Image loadImage(URL url) {<br \/>\n    return(Toolkit.getDefaultToolkit().getImage(url));<br \/>\n  }<\/p>\n<p>  \/** The Image associated with the ImageLabel. *\/<\/p>\n<p>  public Image getImage() {<br \/>\n    return(image);<br \/>\n  }<\/p>\n<p>  \/** Gets the border width. *\/<\/p>\n<p>  public int getBorder() {<br \/>\n    return(border);<br \/>\n  }<\/p>\n<p>  \/** Sets the border thickness. *\/<\/p>\n<p>  public void setBorder(int border) {<br \/>\n    this.border = border;<br \/>\n  }<\/p>\n<p>  \/** Gets the border color. *\/<\/p>\n<p>  public Color getBorderColor() {<br \/>\n    return(borderColor);<br \/>\n  }<\/p>\n<p>  \/** Sets the border color. *\/<\/p>\n<p>  public void setBorderColor(Color borderColor) {<br \/>\n    this.borderColor = borderColor;<br \/>\n  }<\/p>\n<p>  \/\/ You could just call size().width and size().height,<br \/>\n  \/\/ but since we&#039;ve overridden setSize to record<br \/>\n  \/\/ this, we might as well use it.<\/p>\n<p>  \/** Gets the width (image width plus twice border). *\/<\/p>\n<p>  public int getWidth() {<br \/>\n    return(width);<br \/>\n  }<\/p>\n<p>  \/** Gets the height (image height plus 2x border). *\/<\/p>\n<p>  public int getHeight() {<br \/>\n    return(height);<br \/>\n  }<\/p>\n<p>  \/** Has the ImageLabel been given an explicit size?<br \/>\n   *  This is used to decide if the image should be<br \/>\n   *  stretched or not. This will be true if you<br \/>\n   *  call setSize or setBounds on the ImageLabel before<br \/>\n   *  adding it to a Container. It will be false<br \/>\n   *  otherwise.<br \/>\n   *\/<\/p>\n<p>  protected boolean hasExplicitSize() {<br \/>\n    return(explicitSize);<br \/>\n  }<\/p>\n<p>  \/** Returns the string representing the URL that<br \/>\n   *  will be used if none is supplied in the<br \/>\n   *  constructor.<br \/>\n   *\/<\/p>\n<p>  public static String getDefaultImageString() {<br \/>\n    return(defaultImageString);<br \/>\n  }<\/p>\n<p>  \/** Sets the string representing the URL that<br \/>\n   *  will be used if none is supplied in the<br \/>\n   *  constructor. Note that this is static,<br \/>\n   *  so is shared by all ImageLabels. Using this<br \/>\n   *  might be convenient in testing, but &quot;real&quot;<br \/>\n   *  applications should avoid it.<br \/>\n   *\/<\/p>\n<p>  public static void setDefaultImageString(String file) {<br \/>\n    defaultImageString = file;<br \/>\n  }<\/p>\n<p>  \/** Returns the string representing the URL<br \/>\n   *  of image.<br \/>\n   *\/<\/p>\n<p>  protected String getImageString() {<br \/>\n    return(imageString);<br \/>\n  }<\/p>\n<p>  \/** Is the debugging flag set? *\/<\/p>\n<p>  public boolean isDebugging() {<br \/>\n    return(debug);<br \/>\n  }<\/p>\n<p>  \/** Set the debugging flag. Verbose messages<br \/>\n   *  will be printed to System.out if this is true.<br \/>\n   *\/<br \/>\n  public void setIsDebugging(boolean debug) {<br \/>\n    this.debug = debug;<br \/>\n  }<br \/>\n}<br \/>\n***********************\n<\/p>\n<p><\/b><\/p>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10348<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Advanced Swing<br \/>Tags:Java\/J2EE\/J2MEAdvanced Swing<br \/> Post Data:2017-01-02 16:04:35<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>####################### # CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images: * CardPanel.java A Panel that displays a playing card. * ImageLabel.java A Canvas for displaying images. * Ace.gif, King.gif, Queen.gif, Jack.gif. &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=27229\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-27229","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10295,"url":"http:\/\/bangla.sitestree.com\/?p=10295","url_meta":{"origin":27229,"position":0},"title":"A demo providing multiple buttons to select a playing card-A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display","author":"","date":"August 26, 2015","format":false,"excerpt":"####################### # CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images: \u00a0\u00a0\u00a0 * CardPanel.java A Panel that displays a playing card. \u00a0\u00a0\u00a0 * ImageLabel.java A Canvas for\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10282,"url":"http:\/\/bangla.sitestree.com\/?p=10282","url_meta":{"origin":27229,"position":1},"title":"Places a Panel holding 100 buttons in a ScrollPane","author":"","date":"August 26, 2015","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; \/** Places a Panel holding 100 buttons in a ScrollPane that is \u00a0*\u00a0 too small to hold it. \u00a0* \u00a0 *\/ public class ScrollPaneTest extends Applet { \u00a0 public void init() { \u00a0\u00a0\u00a0 setLayout(new BorderLayout()); \u00a0\u00a0\u00a0 ScrollPane pane = new ScrollPane(); \u00a0\u00a0\u00a0 Panel bigPanel = new\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27217,"url":"http:\/\/bangla.sitestree.com\/?p=27217","url_meta":{"origin":27229,"position":2},"title":"Places a Panel holding 100 buttons in a ScrollPane #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 14, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; \/** Places a Panel holding 100 buttons in a ScrollPane that is * too small to hold it. * *\/ public class ScrollPaneTest extends Applet { public void init() { setLayout(new BorderLayout()); ScrollPane pane = new ScrollPane(); Panel bigPanel = new Panel(); bigPanel.setLayout(new GridLayout(10, 10)); for(int\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27000,"url":"http:\/\/bangla.sitestree.com\/?p=27000","url_meta":{"origin":27229,"position":3},"title":"Eight buttons: four each in two panels #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 7, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; *************************** \/** Eight buttons: four each in two panels. * *\/ public class ButtonTest2 extends Applet { public void init() { String[] labelPrefixes = { \"Start\", \"Stop\", \"Pause\", \"Resume\" }; Panel p1 = new Panel(); for (int i=0; i<4; i++) { p1.add(new Button(labelPrefixes[i] + \" Thread1\"));\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10333,"url":"http:\/\/bangla.sitestree.com\/?p=10333","url_meta":{"origin":27229,"position":4},"title":"Eight buttons: four each in two panels","author":"","date":"August 26, 2015","format":false,"excerpt":"mport java.applet.Applet; import java.awt.*; *************************** \/** Eight buttons: four each in two panels. * *\/ public class ButtonTest2 extends Applet { public void init() { String[] labelPrefixes = { \"Start\", \"Stop\", \"Pause\", \"Resume\" }; Panel p1 = new Panel(); for (int i=0; i<4; i++) { p1.add(new Button(labelPrefixes[i] + \" Thread1\"));\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26948,"url":"http:\/\/bangla.sitestree.com\/?p=26948","url_meta":{"origin":27229,"position":5},"title":"Creates three radio buttons and illustrates handling #Programming Code Examples #Java\/J2EE\/J2ME #Basic Swing","author":"Author-Check- Article-or-Video","date":"May 6, 2021","format":false,"excerpt":"JRadioButtonTest.java Creates three radio buttons and illustrates handling ItemEvents in response to selecting a radio button. import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.*; import java.awt.event.*; import javax.swing.*; \/** *\/ public class JRadioButtonTest extends JPanel implements ItemListener { public JRadioButtonTest() { String[] labels = {\"Java Swing\",\"Java Servlets\", \"JavaServer Pages\"}; JRadioButton[] buttons =\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27229"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27229\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27229"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}