Java ActionListener Question

Reply

Join Date: Oct 2009
Posts: 12
Reputation: geek_till_itMHZ is an unknown quantity at this point 
Solved Threads: 0
geek_till_itMHZ geek_till_itMHZ is offline Offline
Newbie Poster

Java ActionListener Question

 
0
  #1
19 Days Ago
I have a Java Program that I need to create that will show five random images selected form my image folder of 50 images. upon clicking a button the program should select 5 new random images. I have most of it figured out except for my ActionListener. Is it possible to implement an ActionListener that will simply run my main class over again thereby "restarting" the program and selecting 5 new images?

Any help would be greatly appreciated

Heres what I have so far


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CardDisplay extends JFrame {

//Creat JButton
JButton jbtNew = new JButton("New Cards");

//Initialize Variables
int number1 = (int)(Math.random()*54+1);
int number2 = (int)(Math.random()*54+1);
int number3 = (int)(Math.random()*54+1);
int number4 = (int)(Math.random()*54+1);
int number5 = (int)(Math.random()*54+1);

private ImageIcon card1 = new ImageIcon(number1 + ".png");
private ImageIcon card2 = new ImageIcon(number2 + ".png");
private ImageIcon card3 = new ImageIcon(number3 + ".png");
private ImageIcon card4 = new ImageIcon(number4 + ".png");
private ImageIcon card5 = new ImageIcon(number5 + ".png");



//Construct Layout
public CardDisplay() {
setLayout(new GridLayout(1,5));
add(new JLabel(card1));
add(new JLabel(card2));
add(new JLabel(card3));
add(new JLabel(card4));
add(new JLabel(card5));

//Create Panel
JPanel panel = new JPanel();
panel.add(jbtNew);
add(panel); //add Panel

//Register Listener
ListenerClass listener = new ListenerClass();
jbtNew.addActionListener(listener);


}
//Main Method
public static void main(String[] args) {

CardDisplay frame = new CardDisplay();
frame.setSize(800, 200);
frame.setTitle("Card Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static class ListenerClass implements ActionListener{
public void actionPerformer(ActionEvent e) {



}
}
}
Last edited by geek_till_itMHZ; 19 Days Ago at 12:12 pm. Reason: added code
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
19 Days Ago
add(new JLabel(card1));
use varibles for labels
first declare in proper place: JLabel label1;
In constructor create new instance of label: label1 = new JLabel(card1);
add this label ...
///
In method actionPerformed your labels stays avilable to set new icons
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,424
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #3
19 Days Ago
Or keep them in a List.
Reply With Quote Quick reply to this message  
Reply

Tags
java

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC