Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been trying to figure out for quite some time on how to create a basic main menu for a game that just has a couple clickable labels, but I don't know how to write them. I have created a class that extends JFrame, made a JLabel and that is it. I don't know much to do after that.

share|improve this question
3  
Sounds like a question for Google. Check out zetcode.com/tutorials/javaswingtutorial –  XiaoChuan Yu Jul 6 '13 at 4:19
add comment

2 Answers

Firstly, You may want to check google for basic tutorials. To make your class appear and work as expected, we first need to set it's size. You would do so with YourClass.setSize(x-dimension, y-dimension). Then, if you want it to close properly, you need to use YourClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE). This allows the frame to close and terminate the application that's running. finally, you set its visibilty to true, so you can see it with YourClass.setVisible(true), all the while replacing YourClass with an instance of your class. If you wish to add buttons, then before setting your frame's visibility, create a new JButton. Then, add it too your JFrame by using YourClass.add().

share|improve this answer
add comment

You should be able to do this by adding a mouse listener to the labels. E.g.

label.addMouseListener(someMouseListener).

Then you'll just have to handle the click events in the listener. If you need to do different things based on which label was clicked, then do something like this in the mouse listener's click method:

public void mouseClicked(MouseEvent e) {
    if (e.getSource() == label1) {
      ...
    } else if (e.getSource() == label2) {
      ...
    }
}

If you don't (want to) have a mouse listener that handles many sources like this, you can just add an anonymous listener to each of the labels:

label.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        ...
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.