Thursday, January 26, 2012

I'm trying to make a jeopardy game using JAVA language.?

Can someone please tell me how do I make the buttons dissapear from the screen after the user has clicked them? THanks!I'm trying to make a jeopardy game using JAVA language.?
That's not much information, but in general if you are using a JButton, you can do setVisible(false) to hide the button. This is an example. Save it as Example.java and compile and run it. When you click the button, it will vanish. Just close the window to quit.



import javax.swing.*;

import java.awt.event.*;

public class Example {

public static void main(String[] args) {

JFrame frame = new JFrame("Example");

final JButton button = new JButton("Click Me!");

button.addActionListener(new ActionListener() {

public void actionPerformed(Event event) {

button.setVisible(false);

}

});

frame.setDefaultCloseOperation(WindowCon鈥?br>
frame.getContentPane().add(button);

frame.pack();

frame.setVisible(true);

}

}



Hope this helps!



EDIT:

Okay, so you are using AWT. That's not much different. Here is the same example, but using AWT only (no Swing classes).



import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Frame;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class Example {

public static void main(String[] args) {

Frame frame = new Frame();

frame.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

final Button button = new Button("Click Me!");

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

button.setVisible(false);

}

});

frame.add(button, BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

}



As for the idea of using an image, that's fine, but it requires that you have the images. Further, you can use images as the label for a button, so you can do this with buttons. I think it is simpler to just use setVisible(false) to hide the GUI items you want to hide, however.



Hope this helps!I'm trying to make a jeopardy game using JAVA language.?
Instead of using a JButton, consider using a JLabel which can have an optional image associated with it.



When you click the JLabel the first time, set it to have the image with the question.



After the question is answered, set it to have a blank image associated with it.



If you have more questions post more details.

No comments:

Post a Comment