// these image that put in program must be in the same folder that saved the program . //AlbumInJava.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class AlbumInJava extends JPanel { static int size=5; static String aray[] =new String [size]; static int top=-1;
public static void push(String item) { if (top+1>=size) JOptionPane.showMessageDialog(null," Album is Full you can not push the image ."); else top=top+1; aray[top]=item; } public static void pop() { // what are these code that must be write in this method ? } ImageIcon[] images; static JPanel p; static JTextField t; static JLabel l; static JButton b = new JButton("push"); static JButton b1 = new JButton("pop"); static String z; static JList namelist; public AlbumInJava() { super(new BorderLayout()); //Load the pet images and create an array of indexes. images = new ImageIcon[aray.length]; Integer[] intArray = new Integer[aray.length]; for (int i = 0; i < aray.length; i++) { intArray[i] = new Integer(i); images[i] = createImageIcon(aray[i] + ".jpg"); if (images[i] != null) { images[i].setDescription(aray[i]); } } //Create the combo box. JComboBox petList = new JComboBox(intArray); ComboBoxRenderer renderer= new ComboBoxRenderer(); renderer.setPreferredSize(new Dimension(600, 200)); petList.setRenderer(renderer); petList.setMaximumRowCount(2); //Lay out the demo. add(petList, BorderLayout.PAGE_END); setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); p = new JPanel(new GridLayout(3, 3)); t = new JTextField("pic ()",0); l = new JLabel("Enter The Name Image "); p.add(l); p.add(t); p.add(b); p.add(b1); } /** Returns an ImageIcon, or null if the path was invalid. */ static ImageIcon createImageIcon(String path) { java.net.URL img = AlbumInJava.class.getResource(path);// please what dose the work ? if (img != null) { return new ImageIcon(img); } return null; } public static void doThis()// what dose the work method ? { push(z); } static JFrame frame; static JComponent newContentPane; /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. frame = new JFrame("My Album "); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. newContentPane = new AlbumInJava(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); frame.getContentPane().add(p, BorderLayout.PAGE_START); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } } ); b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource() == b) { z = t.getText(); doThis(); newContentPane = new AlbumInJava(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.getContentPane().add(p, BorderLayout.PAGE_START); frame.pack();// what dose the work method ?
} if(e.getSource() == b1) { pop();// i wont if i press (b1) button call pop(), and one image removed. how ? } } } ); }
class ComboBoxRenderer extends JLabel implements ListCellRenderer { private Font uhOhFont; public ComboBoxRenderer() { setOpaque(true); setHorizontalAlignment(CENTER); setVerticalAlignment(CENTER); } /* * This method finds the image and text corresponding * to the selected value and returns the label, set up * to display the text and image. */ public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { //Get the selected index. (The index param isn't //always valid, so just use the value.) int selectedIndex = ((Integer)value).intValue(); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } //Set the icon and text. If icon was null, say so. ImageIcon icon = images[selectedIndex]; String pet = aray[selectedIndex]; setIcon(icon); if (icon != null) { setText(pet); setFont(list.getFont()); } else { setUhOhText(pet + " (no image available)", list.getFont()); } return this; } //Set the font and text when no image was found. protected void setUhOhText(String uhOhText, Font normalFont) { if (uhOhFont== null) { //lazily create this font uhOhFont = normalFont.deriveFont(Font.ITALIC); } setFont(uhOhFont); setText(uhOhText); } }
}
Ads