button color

I consider myself a beginner in programming so I need a little help if that is possible卋elow is the code of my program ?i have designed 100 buttons by using 2D array method?

But the problem that I抦 having is I can抰 figure out how to change the colour of any of the buttons when I click it

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.JButton;

publicclass RobotPathextends JFrame

implements ActionListener{

JPanel buttonPanel, robotPathPanel, directionPanel;

JMenuBar robotMenuBar;

JMenu fileMenu, helpMenu;

JMenuItem exitItem, aboutItem, istructionsItem;

JButton tbtForward, tbtRotate, tbtClear, tbtExit;

JButton[][] tbtPath;

int row = 10, col = 10;

int x = 0, y = 0;

publicstaticvoid main (String[] args){

RobotPath frame =new RobotPath();

frame.setTitle("Robot Path Emulator - Java Application");//frame title

frame.setBounds(250, 90,// setting the position

750, 600);//setting the size

frame.createGUI();

frame.setResizable(false);

frame.setVisible(true);//displaying the window

}

privatevoid createGUI(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container window = getContentPane();

window.setLayout(new BorderLayout() );// Set border layout manager

//set the menu bar

robotMenuBar =new JMenuBar();

setJMenuBar(robotMenuBar);

// file menu, with Exit

fileMenu =new JMenu("File");

exitItem =new JMenuItem("Exit");

fileMenu.add(exitItem);

exitItem.addActionListener(this);

robotMenuBar.add(fileMenu);

// help menu, with about, instructions

helpMenu =new JMenu("Help");

aboutItem =new JMenuItem("About");

helpMenu.add(aboutItem);

aboutItem.addActionListener(this);

istructionsItem =new JMenuItem("Istructions");

helpMenu.add(istructionsItem);

istructionsItem.addActionListener(this);

robotMenuBar.add(helpMenu );

//JPanel for the buttons

buttonPanel =new JPanel();

buttonPanel.setLayout(new GridLayout(20, 1));

tbtForward =new JButton("Forward");//Creating buttons: Forward,

buttonPanel.add(tbtForward);//Rotate, Clear, Exite

tbtForward.addActionListener(this);

tbtRotate =new JButton("Rotate");

buttonPanel.add(tbtRotate);

tbtRotate.addActionListener(this);

tbtClear =new JButton("Clear");

buttonPanel.add(tbtClear);

tbtClear.addActionListener(this);

tbtExit =new JButton("Exit");

buttonPanel.add(tbtExit);

tbtExit.addActionListener(this);

//JPanel for the Robot Path Emulator

robotPathPanel =new JPanel();

robotPathPanel.setBackground(Color.white);

JButton[][] tbtPath =new JButton[row][col];// Array to store buttons

for (int x = 0 ; x < row ; x++)

{

for (int y = 0 ; y < col ; y++)

{

tbtPath [x] [y] =new JButton ("(" + x +"," + y +")");

robotPathPanel.add(tbtPath[x][y]);

tbtPath [x] [y].setBackground (Color.green);

tbtPath [x] [y].addActionListener (this);

}

}

//JPanel for the directions

directionPanel =new JPanel();

window.add("East", buttonPanel);

window.add("Center", robotPathPanel);

window.add("South", directionPanel);

}

publicvoid actionPerformed(ActionEvent e){

if(e.getSource() == exitItem){

System.exit(0);

}

if(e.getSource() == tbtExit){

System.exit(0);

}

}

}

[6607 byte] By [nicegyala] at [2007-10-2 18:16:54]
# 1
In action performed you get a source object right? Type cast that into a JButton and then change it's colour. Easy as pie :)
Phibreda at 2007-7-13 19:37:07 > top of Java-index,Other Topics,Java Game Development...