PDA

View Full Version : Please Help Andrew


Annemarie
1st March 2005, 21:58
any of you out there know java well enough to help me?

I have created some buttons within my program and want to be able to click on them and get a resposnse. So i need an actionListener or something. However every piece of code i have found off the net or in books seems to throw me an error message of one kind or another.

ButtonHandler handler = new ButtonHnadler();

won't work. Tho in the book im using it appears to work fine for them.

addMouseListener()

is not working either saying it doesn't recognise the method i'm trying to access with it

do any of you have any snippets of code for using an actionListener? I just can't get anything to work

M3ta7h3ad
2nd March 2005, 03:07
lol I made a gui for my first... er first year uni project.

Shall get the code for you if I can find it on my uni space. One second..

Here's a simple gui app I made in order to understand how to do it before developing my coursework.

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

public class GuiTest{
public static void main(String args[]){

//Construct components

JFrame f = new JFrame("Button Example");
JButton redButton = new JButton("red");
JButton blueButton = new JButton("blue");
final JLabel l = new JLabel("Some Text");
l.setForeground(Color.red);

//Add To Container

f.getContentPane().setLayout(new GridLayout (2,1));
f.getContentPane().add (l);
f.getContentPane().add (redButton);
f.getContentPane().add (blueButton);

//Handle Events

redButton.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
l.setForeground(Color.red);
}
});
blueButton.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
l.setForeground(Color.blue);
}
});

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}

Andy King Of All
2nd March 2005, 09:55
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class DiceScreen
{

private GridLayout layout;
public JPanel panel;

public DiceScreen()
{

JFrame frame = new JFrame();
final JPanel panel = new JPanel();
layout = new GridLayout(4,6);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(900,600);
panel.setLayout(layout);
frame.getContentPane().add(panel);

JButton dice1 = new Dice();
panel.add(dice1);

frame.setVisible(true);

ButtonHandler handler = new ButtonHandler();
dice3.addActionListener( handler );
dice4.addActionListener( handler );


}

private class ButtonHandler implements ActionListener
{
// handle button event
public void actionPerformed( ActionEvent event )
{
System.exit(0);


} // end method actionPerformed
} // end private inner class ButtonHandler


}


I have this other class called Dice which is also a JBUtton but the label on that button is a dice face. I want to be able to click on the button and it will call the Dice method and run it again changing the dice face.

I used System.exit to test that its working, and it is. But i just don't know how to update the button. In effect i want to change the label on it by calling the Dice method each time its clicked on.

Not only can i not update the button but it keeps telling me it can't see the variable for the dice.

M3ta7h3ad
2nd March 2005, 11:01
not able to find the variable?. Variables have to be in the same class, unless your declaring them globals I think.

If you mean it cant find the dice method?, its because you have put it in another class and not specified the class that its in.

You could always just put

import dice.*;

at the top and that may solve all your problems, even the variables.

M3ta7h3ad
2nd March 2005, 11:03
Its been about a year since I have done any java, so I maybe a bit scratchy :) lol. But if it still screws up I will try to help you as much as possible :)

Andy King Of All
2nd March 2005, 15:33
Its been about a year since I have done any java, so I maybe a bit scratchy :) lol. But if it still screws up I will try to help you as much as possible :)


Thanks :)

I'm off to see the lecturer in the morning, he should be able to give me tips on this, as right now i'm in above my head and sinking further. The deadline is Monday, so i do have a bit of time to do it.