Hi there. I'm new to programming and taking my first Java programming class. We're currently working on GUI things and I was given the assignment of working on panels and images.
Here are the instructions:
However, I am not sure on how to do one thing - How do I display two images in one panel? Like, how would I put the akali and viktor gifs into the League of Legends panel to be displayed, side by side?II. Read, run, and understand the sample program: NextedPanels and LabelDemo on pages 149/152.
III. Write a program that displays a frame containing at least four panels. Each panel should contain two images (use four unique images- your choice). Fix the size of the first panel so that both of its images remain side by side. Allow the other panel to change size as needed. Experiment with the size of the window to see the images change orientation. Make sure you understand why the application behaves as it does.
Here is what I have so far:
import java.awt.*; import javax.swing.*; public class Images{ public static void main (String[] args) { JFrame frame = new JFrame ("Nested Panels - HOSHI"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); ImageIcon devil = new ImageIcon ("devil.gif"); ImageIcon akali = new ImageIcon ("akali.gif"); ImageIcon sketch = new ImageIcon ("sketch.gif"); ImageIcon victor = new ImageIcon ("viktor.gif"); ImageIcon deva = new ImageIcon ("deva.gif"); JLabel label1, label2, label3, label4; l JPanel subPanel1 = new JPanel(); subPanel1.setPreferredSize (new Dimension(150, 100)); subPanel1.setBackground (Color.green); label1 = new JLabel ("Devil Left", devil, SwingConstants.CENTER); subPanel1.add (label1); JPanel subPanel2 = new JPanel(); subPanel2.setPreferredSize (new Dimension(400, 150)); subPanel2.setBackground (Color.red); label2 = new JLabel ("Art in Progress", akali, SwingConstants.CENTER); label2.setHorizontalTextPosition (SwingConstants.LEFT); label2.setVerticalTextPosition (SwingConstants.CENTER); subPanel2.add (label2); JPanel subPanel3 = new JPanel(); subPanel3.setPreferredSize (new Dimension(400, 150)); subPanel3.setBackground (Color.red); label3 = new JLabel ("Girl Characters", deva, SwingConstants.CENTER); label3.setHorizontalTextPosition (SwingConstants.CENTER); label3.setVerticalTextPosition (SwingConstants.BOTTOM); subPanel3.add (label3); JPanel subPanel4 = new JPanel(); subPanel4.setPreferredSize (new Dimension(400, 150)); subPanel4.setBackground (Color.red); label4 = new JLabel ("League of Legends", viktor, SwingConstants.CENTER); label4.setHorizontalTextPosition (SwingConstants.RIGHT); label4.setVerticalTextPosition (SwingConstants.CENTER); subPanel4.add (label4); JPanel primary = new JPanel(); primary.setBackground (Color.blue); primary.add (subPanel1); primary.add (subPanel2); primary.add (subPanel3); primary.add (subPanel4); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); } }
Thanks for reading.