944,126 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1112
  • Java RSS
Nov 3rd, 2009
0

Java ActionListener Question

Expand Post »
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; Nov 3rd, 2009 at 12:12 pm. Reason: added code
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek_till_itMHZ is offline Offline
18 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Java ActionListener Question
Quote ...
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
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 3rd, 2009
0
Re: Java ActionListener Question
Or keep them in a List.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Connecting to a mysql database
Next Thread in Java Forum Timeline: mutlipication table in java program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC