Results 1 to 3 of 3
Thread: Trying to make a GUI program
- 11-04-2008, 11:06 PM #1
Trying to make a GUI program
Hey, I'm trying to make a game but this is my first time dealing with GUI components. I want a main menu on the game with four buttons. The buttons are :Infantry Battle", "Tank Battle", "Mixed Battle", and "Exit Wars". However, I am having trouble making the buttons show up on the frame and they are not working. The book I am looking at isn't making much sense to me. Can anyone help me? Here's my code:
import java.awt.*;
import java.awt.event.*;
import jpb.*;
public class War
{
public static void main(String[] args)
{
Frame f = new Frame("Main Menu");
f.setSize(400, 400);
f.setVisible(true);
f.setLocation(300, 300);
}
}
// Frame class
class ButtonTestFrame extends Frame
{
public ButtonTestFrame(String title)
{
super(title);
setLayout(new FlowLayout());
Button exit = new Button("Exit Wars");
add(exit);
exit.addActionListener(new ButtonListener());
// Attach window listener
addWindowListener(new ButtonListener());
}
// Listener for button
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
}
}
:confused:
- 11-04-2008, 11:12 PM #2
I think you need Button exit = new Button(f,"Exit Wars");
Generally when you define a GUI object you need to tell what it's being attached to, in this case the Frame f.
also what method is add? I think you need f.add(exit).
- 11-04-2008, 11:37 PM #3
Java Code:import java.awt.*; import java.awt.event.*; class ButtonTestFrameRx extends Frame { public ButtonTestFrameRx(String title) { super(title); setLayout(new FlowLayout()); Button exit = new Button("Exit Wars"); add(exit); exit.addActionListener(new ButtonListener()); // Attach window listener // An ActionListener won't do as an argument here. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { quit(); } }); } // Listener for button class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { quit(); } } private void quit() { System.exit(0); } public static void main(String[] args) { Frame f = new ButtonTestFrameRx("Main Menu"); f.setSize(400, 400); f.setLocation(300, 300); f.setVisible(true); } }
Similar Threads
-
Is it possible to make a Phone call program using java?
By fireball2008 in forum New To JavaReplies: 2Last Post: 05-08-2008, 06:20 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
i need help to make this program
By masaka in forum New To JavaReplies: 2Last Post: 03-23-2008, 01:26 PM -
Attempting to make a Law of Sines program
By taco89 in forum New To JavaReplies: 1Last Post: 02-14-2008, 04:10 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks