Creates four image icons with text labels, adds them to a panel, and then adds the panel to a frame. - Java Swing

Java examples for Swing:JLabel

Description

Creates four image icons with text labels, adds them to a panel, and then adds the panel to a frame.

Demo Code

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

public class Main extends JFrame {
  JButton load, save, subscribe, unsubscribe;

  public Main() {
    super("Icon Frame");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    // create icons
    ImageIcon loadIcon = new ImageIcon("load.gif");
    ImageIcon saveIcon = new ImageIcon("save.gif");
    ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
    ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
    // create buttons
    load = new JButton("Load", loadIcon);
    save = new JButton("Save", saveIcon);
    subscribe = new JButton("Subscribe", subscribeIcon);
    unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
    // add buttons to panel
    panel.add(load);//from   www. j  a v a  2s  .c  om
    panel.add(save);
    panel.add(subscribe);
    panel.add(unsubscribe);
    // add the panel to a frame
    add(panel);
    pack();
    setVisible(true);
  }

  public static void main(String[] arguments) {
    Main ike = new Main();
  }
}

Related Tutorials