Results 1 to 3 of 3

Thread: Open JFrame

  1. #1
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default Open JFrame

    Hi,
    I got a problem. I want my program too start with Text and a start button. When the start button is called it should open a new JFrame with the game. Here is the main class:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GregerSnake extends JFrame{
    private Board field = new Board();
    private JButton[] button = new JButton[4];
    private String[] buttonText ={"New Game","Menu","Com", "Cancel"};
    private JPanel u;
    private JLabel o, p;      //o är titeln
    
        public GregerSnake(){
    
            u = new JPanel();
            u.setBackground(Color.lightGray);
    
            o = new JLabel("GregerSnake");
            o.setFont(new Font("Arial", Font.BOLD, 40));
            u.add(o);
    
            MyListener myL = new MyListener();
    
            for(int i=0;i<button.length;i++){
                button[i] = new JButton();
                button[i].setText(buttonText[i]);
                button[i].addActionListener(myL);
                u.add(button[i]);
            }
    
            Container c = getContentPane();
            c.add(u, BorderLayout.NORTH);
            c.add(field, BorderLayout.CENTER);
            field.startGame();
        }
    
        class MyListener implements ActionListener{
            public void actionPerformed(ActionEvent ae){
                field.requestFocus();
                if(ae.getSource() == button[0]){
                    field.newGame();
                }
                else if(ae.getSource() == button[1]){
                //    field.startMenu();
                }
                else if(ae.getSource() == button[2]){
                    field.killCom();
                }
                else if(ae.getSource() == button[3]){
                    System.exit(0);
                }
            }
        }
    
        public static void main(String[] args) {
            GregerSnake gt = new GregerSnake();
    	gt.setVisible(true);
            gt.setSize(586, 585);
            gt.setTitle("GregerSnake");
    	gt.setDefaultCloseOperation(EXIT_ON_CLOSE);
            gt.setResizable(false);
            gt.setLocationRelativeTo(null);
        }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Rather than open a new Window, why not swap JPanels with a CardLayout and show your game in the center of the current JFrame?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    I want my program too start with Text and a start button. When the start button is called it should open a new JFrame with the game.
    Why don't you take a look at the games that come installed on you OS. I know Windows comes with many games, like Hearts, Solitaire etc. Look at the UI of those games and design you game based on that. I'm sure those game designers know more than you or I do, so follow there design.

    The basic design that I've noticed is to use Menus to start/stop/configure a game.

Similar Threads

  1. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  2. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  3. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  4. Can't open netbeans 6.1
    By hisouka in forum NetBeans
    Replies: 3
    Last Post: 09-09-2008, 11:04 AM
  5. How to close an open JFrame window from a jsp page?
    By kasisaiganesh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-27-2008, 06:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •