Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 28th, 2007, 5:48 PM   #1
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
Applet / Button problem.

I have a 200 x 200 applet with 3 buttons. My script generates a random number between 0-2 that is used as an index to my array (which contains the variable names of my buttons). I want it so that when the user clicks the button that was randomly chosen out of my array, it turns red, else it turns blue. I know how to detect if the random button was click, but I don't know to to then change the color of that button.
Script:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Game extends Applet implements ActionListener {
    private Button b1, b2, b3, rndB;
    private int randNum = (int)(Math.random()*3);
    private String buttonArray[] = {"b1", "b2", "b3"};
    private String randButton = buttonArray[randNum];
    
    public void init() {
        setBackground(Color.WHITE);
        
        b1 = new Button("b1");
        b1.setBackground(Color.BLACK);
        b1.addActionListener(this);
        add(b1);
        
        b2 = new Button("b2");
        b2.setBackground(Color.BLACK);
        b2.addActionListener(this);
        add(b2);
        
        b3 = new Button("b3");
        b3.setBackground(Color.BLACK);
        b3.addActionListener(this);
        add(b3);
        
        setSize(200, 200);
    }
        
    public void actionPerformed(ActionEvent e) {
        Object test;
        if (e.getActionCommand()==randButton) {
            test = e.getSource();
            test.setBackground(Color.RED);
        } else {
            test.setBackground(Color.BLUE);
        }
    }
}
__________________
meh...
TCStyle is offline   Reply With Quote
Old Apr 29th, 2007, 11:10 AM   #2
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 3 Serinth is on a distinguished road
did you try making an event listener class with a button object and then pass your button to that event listener and then in actionPerformed do the setBackground.
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple java applet problem anant_tickoo Java 2 Aug 9th, 2006 4:13 AM
Java Applet Problem Ross11988 Java 6 Apr 24th, 2006 9:45 AM
Problem Associated with Vector Source code buggytoast Java 3 Apr 2nd, 2006 5:41 AM
IE and dynamic button creation MegaArcon JavaScript and Client-Side Browser Scripting 5 Dec 6th, 2005 8:31 AM
problem with JDBC for talking to Microsoft Access database using a Java Applet captainK Java 4 Mar 20th, 2005 11:01 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC