Program Description:
The program below is a simple java code, a slideshow using JButton to change the images. This also demonstrates two things:
1. How to set the label to image.
Output:
The program below is a simple java code, a slideshow using JButton to change the images. This also demonstrates two things:
1. How to set the label to image.
2. How to align the JButton icons and labels in uniform because by default the icons and the labels are aligned in the center.
Output:
Code:
//Simple Image Slide show using JButton
//by: http://java-code-complete.blogspot.com
//File: imageSlideShowJbutton.java
//Java Extension Packages
import javax.swing.*;
//Java Core Packages
import java.awt.*;
import java.awt.event.*;
public class imageSlideShowJbutton extends JFrame {
//Initializing Components
private JButton b1,b2,b3,b4,b5;
private JPanel panel, panel2;
private JLabel images;
private Icon arrow, orcs, warrior, mage, elf, dwarf;
//Setting up GUI
public imageSlideShowJbutton() {
//Title of the Frame or Window
super("Image Slide Show using JButton");
//Size of the Frame or Window
setSize(370,250);
//Exit Property of the Frame of Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
images = new JLabel("Waiting for Image");
//Constructing Icon class
arrow = new ImageIcon("arrow.gif");
orcs = new ImageIcon("orcs.jpg");
warrior = new ImageIcon("warrior.jpg");
mage = new ImageIcon("mage.jpg");
elf = new ImageIcon("elf.jpg");
dwarf = new ImageIcon("dwarf.jpg");
//Constructing JPanel
panel = new JPanel();
panel2 = new JPanel();
//Constructing JButton
b1 = new JButton("Orc",arrow);
b2 = new JButton("Warrior",arrow);
b3 = new JButton("Mage",arrow);
b4 = new JButton("Elf",arrow);
b5 = new JButton("Dwarf",arrow);
//Set the Labels of the JButton in uniform alignment
b1.setHorizontalAlignment(SwingConstants.LEFT);
b2.setHorizontalAlignment(SwingConstants.LEFT);
b3.setHorizontalAlignment(SwingConstants.LEFT);
b4.setHorizontalAlignment(SwingConstants.LEFT);
b5.setHorizontalAlignment(SwingConstants.LEFT);
Container pane = getContentPane();
//Setting the layout for panel
GridLayout grid = new GridLayout(5,1);
//Adding Button to JPanel
panel.setLayout(grid);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
//Setting layout for panel2
GridLayout grid2 = new GridLayout(1,1);
//Adding layout and label to JPanel2
panel2.setLayout(grid2);
panel2.add(images);
//Add Action on button1
b1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
images.setText(null);
images.setIcon(orcs);
}
}
);
//Add Action on button2
b2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
images.setText(null);
images.setIcon(warrior);
}
}
);
//Add Action on button3
b3.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
images.setText(null);
images.setIcon(mage);
}
}
);
//Add Action on button4
b4.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
images.setText(null);
images.setIcon(elf);
}
}
);
//Add Action on button5
b5.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
images.setText(null);
images.setIcon(dwarf);
}
}
);
//Add the panel and panel2 in the container or JFrame
pane.add(panel, BorderLayout.WEST);
pane.add(panel2, BorderLayout.EAST);
setContentPane(pane);
setVisible(true);
}
public static void main(String[] args) {
imageSlideShowJbutton issjb = new imageSlideShowJbutton();
}
}
Required Image(s):
No comments:
Post a Comment