The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Creating a new window

Posted by Hiran on November 27, 2001 at 11:11 PM

How do I create a new window? I have to create the solitaire game (school project), and one of the requirements is that a user should be able to have the option of entering an URL which specifies the layout of the cards at the beginning of the game. I use a menu bar and menu items to allow the user to specify that they want to start a new game with an URL address. Then, when they click on the corresponding menu item, I have a class that implements action listener and adds a text field to get the URL. Except, I don't think that'll work, since the window is filled with JPanels. So I think the best way would be to create a new window where I add the JTextField. Is this the best way? And if so, how do I do that? Here's the code so far (some of it might be irrelevent to this particular problem, but I've included it just in case it's needed):

public class UserInterface extends CloseableFrame
{ // Make 'true' to enable some debugging print statements.
public static final boolean DEBUG = false;

// The default size of the Frame containing the cards
public static final int DEFAULT_FRAME_WIDTH = 775;
public static final int DEFAULT_FRAME_HEIGHT = 725;

/** Use this to find the card fast. We use the function suit * 13 + rank to
find the card in this array -- or simply call cardNum(..) */
private CardComponent[] cards = null;

private JPanel[] piles = new JPanel[Constants.NUM_PILES];
private JPanel stockAndDisPanel = new JPanel();
private JPanel layoutPanel = new JPanel();
private JPanel foundationPanel = new JPanel();

private JMenuBar solitaireMenuBar;
private JMenu solitaireMenuOne;
private JMenu solitaireMenuOneSubMenuOne;
private JMenuItem solitaireMenuItem;

private JTextField inputURL = new JTextField(30);

/** The application logic is encoded in this class. */
private Solitaire app;

private JPanel cardPanel = new JPanel();
/** Construct a new user interface. This one is private so it can only
be instantiated from this class -- for testing -- or from the public
constructor (below)
@param testingThisClass if true, then don't create the application. */
public UserInterface(boolean testingThisClass)
{ super();

//Create the menubar
solitaireMenuBar = new JMenuBar();

//Create the menu(s)
solitaireMenuOne = new JMenu("File");

//Create the submenu(s) and add them to the corresponding menu
solitaireMenuOneSubMenuOne = new JMenu("New Game");

//Create the menu items in the first menu and add them
solitaireMenuItem = new JMenuItem("1) Start a new game");
solitaireMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.CTRL_MASK));
solitaireMenuItem.addActionListener(new NewGameListener());
solitaireMenuItem = new JMenuItem("2) Start a game based on an URL");
solitaireMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.CTRL_MASK));
solitaireMenuItem.addActionListener(new NewURLGameListener());

//Add the menu bar and all the menus and sub menus.

// Don't create the app if we're just testing this class. This can't
// be combined with the call to newGame because createPiles and createCards
// need the reference to the app.
if (!testingThisClass)
{ app = new Solitaire(this);



// Don't create the app if we're just testing this class
if (!testingThisClass)
{ app.newGame();

/** Adds the text field to the UI to get the URL for a new game setup. */
private void addTextField()
{ this.add(inputURL);
inputURL.addActionListener(new NewInputURLListener());

/** Define a listener to start a new game. */
private class NewGameListener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ app.newGame();

/** Define a listener to start a new game based on an URL. */
private class NewURLGameListener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ addTextField();

/** Define a listener for the text field that gets the URL. */
private class NewInputURLListener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ try
{ //URL setupFile = new URL(inputURL.getText());
URL setupFile = new URL("");
} catch (MalformedURLException e2)

Thnx in advance for the help.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us