Help with this bloody button?

Subscribe to Help with this bloody button? 2 posts

avatar for LunarLady LunarLady 7 posts
Flag Post

I’m working on this one button and this single topic has taken up over of month of my time. I can make the button, I can display button but I can’t make it do anything when clicked.

I’ve tried

package role.play.generator;
import java.util.Random;
import java.awt.;
import javax.swing.
;
public class RolePlayGenerator {
public static void main(String[] args) {
//Make the Window
JFrame frame = new JFrame(“Random Role-Play Generator”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// I’m not wuite sure what this does \/
Random randomgenerator = new Random();
// Random Selector code
int hairsel = randomgenerator.nextInt(5);
int clothsel = randomgenerator.nextInt(4);
// Arrays
String[] clothtypes = {"Dress",“Skirt”,“Robe”,"Pants"};
String[] haircolours = {"Blue", “Red”, “Blonde”,“Black”,"Purple"};
//Height Generator
double height = Math.floor(Math.random()9+58);
// Variable section
String Haircolour="Hair Colour: " + haircolours[hairsel] + " “;
String Clothing=”Clothing: " + clothtypes[clothsel] + " ";
String Height = "Height: “height” Inches “;
String Display = Height + Clothing + Haircolour;
// Create Labels
JLabel HeightLabel = new JLabel(Display,SwingConstants.CENTER);
HeightLabel.setPreferredSize(new Dimension(400,100));
JButton button = new JButton();
button.setText(”Regenerate");
// Button Commands
button.addMouseListener(new MouseListener(){
hairsel = randomgenerator.nextInt(5);
clothsel = randomgenerator.nextInt(4);
height = Math.floor(Math.random()
9+58);
Height = “Height=”+height+" Inches “;
Haircolour=”Hair Colour: " + haircolours[hairsel] + " “;
Clothing=”Clothing: " + clothtypes[clothsel] + " ";
Height = "Height: “height” Inches ";
Display = Height + Clothing + Haircolour;
})
frame.getContentPane().add(HeightLabel, BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}

It keeps saying it requires “MouseListener” and it find “anonymous MouseListener”

Any help would be greatly appreciated.

 
avatar for Anomandaris Anomandaris 36 posts
Flag Post

Within your “new MouseListener()” declaration you need to override public void mouseClicked(e:MouseEvent) and put your code in there.

See the oracle docs for an example
http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html