简体   繁体   中英

ImageIcon display URL image

I want to know if it is possible to load an array of ImageIcon to be loaded with images from an on line source so that the images are not stored locally. And is there a way to add a action listener to an array of buttons so that when they are pressed a new frame is opened.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class MenuView extends JFrame {

  String[] names =
      {"banana split", "chicken with rice", "rice", "noodles", "fried vermicelli", "smoothie"};
  String[] namesImage = {"banana split.jpeg", "chicken with rice.jpeg", "rice.jpeg", "noodles.jpeg",
      "fried vermicelli.jpeg", "smoothie.jpeg"};

  ImageIcon[] foodImages = new ImageIcon[namesImage.length];
  JButton[] jbtChoc = new JButton[names.length];

  {

    for (int i = 0; i < names.length; i++) {
      jbtChoc[i] = new JButton(names[i]);
      for (int x = 0; x < names.length; x++) {
        foodImages[x] = new ImageIcon(namesImage[x]);
      }
    }
  }

  /**
   * Constructor for the MenuView.
   */

  public MenuView() {
    Container cont = getContentPane();
    cont.setLayout(new BorderLayout(5, 5));;
    cont.setBackground(Color.white);

    JPanel girdSetup = new JPanel(new GridLayout(2, 3, 5, 5));

    for (int i = 0; i < foodImages.length; i++) {
      jbtChoc[i].setIcon(foodImages[i]);
      girdSetup.add(jbtChoc[i]);
      jbtChoc[i].setVerticalTextPosition(AbstractButton.BOTTOM);
      jbtChoc[i].setHorizontalTextPosition(AbstractButton.CENTER);
    }

    cont.add(girdSetup, BorderLayout.CENTER);
  }

  /**
   * Main method for test.
   * 
   * @param args Initial setup.
   */

  public static void main(String[] args) {
    MenuView frame = new MenuView();
    frame.setTitle("MenuView");
    frame.setSize(950, 400);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }
}

if it is possible to load an array of ImageIcon to be loaded with images from an on line source

Image icon from url

URL url = new URL("UrlPath");
BufferedImage img = ImageIO.read(url);
ImageIcon icon = new ImageIcon(img);

In your case

URL url = new URL(namesImage[x]);
BufferedImage img = ImageIO.read(url);
foodImages[x] = new ImageIcon(img);

is there a way to add a action listener to an array of buttons

Add action listener to array of buttons

jbtChoc[i].addActionListener(this);

when they are pressed a new frame is opened.

Your action performed method

public void actionPerformed(ActionEvent e){
   if( e.getSource() instanceof JButton) {
       //do your action here
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM