Note that there are some explanatory texts on larger screens.

plurals
  1. POdisplaying images from an array in a gui window using swing in java
    primarykey
    data
    text
    <p>I am developing a program that will populate an array with 52 images of cards from a file. I would like to display these images in a gui window. This is for a program that will select five random card images and display them in a gui window. So, right now, i am trying to develop the part of the code which will display images from an array in a window and i am at a loss as to how to display png images in a jframe. This is the code i have so far. I used a system.out.println statement so i know that the array of 52 card images is populating correctly, however, i do not know how to display them properly in a window. </p> <pre><code>String[] cardsArray = new String[52]; for (int i = 0; i &lt; 52; i++) { cardsArray[i] = "C:\\Users\\mike\\Documents\\NetBeansProjects\\card shuffler\\cards\\\"+String.valueOf(i+1)+".png"; } System.out.println(Arrays.toString(cardsArray)); </code></pre> <p>additional note. I have to use a jframe to display the results in a side by side layout. I thought to use flowLayout to accomplish this, but, i a not sure how to pass in an array of images. I have no problem doing it with a single image from a file. I am using the code below as a guide.</p> <pre><code>JFrame myJFrame = new JFrame(); // create and assign a FlowLayout for myFrame myJFrame.setLayout(new FlowLayout()); // Create a label with an image icon JLabel jlCSCI = new JLabel(new ImageIcon("CSCI.jpg")); // add the Label to the frame myJFrame.add(jlCSCI); // Add thelabel to MyGridLayout // set the title, size, location and exit behavior for the frame myJFrame.setTitle("ImageIcon Demo"); myJFrame.setSize(240, 200); myJFrame.setLocation(200, 100); myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // make the frame visible (activate the GUI frame) myJFrame.setVisible(true); </code></pre> <p>I am not sure how to develop the statement that would utilize an array that i created within the program.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload