action listener and linking classes Please help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nt5515
    New Member
    • Feb 2008
    • 10

    action listener and linking classes Please help!

    i've just started using java and im trying to develop a GUI that will play a game. i have a start screen with some Jbuttons. The button represent 3 sizes of the game grid, when these are pressed i want a a new frame to open ith the game grid. i've already writen the class containing the game grid, and need to know how to use the actionlistener to relate the initial class to the new class. Also depending on which button is pressed, will change the variable grid size. i imagine this would just be done with an if statement, but i cant try it until i know how to link the classes.

    Any suggestions would be welcome
    Thanx
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    What do you mean by "link classes"? What if one class mentions the other? Would that suffice?

    Comment

    • nt5515
      New Member
      • Feb 2008
      • 10

      #3
      Originally posted by BigDaddyLH
      What do you mean by "link classes"? What if one class mentions the other? Would that suffice?
      i mean if i had a button in the initial frame class how do i get it to open a new grid frame with a variable size? on the first screen there is three buttons each of which will open a new frame with a grid size depending on which button is pressed.

      heres the code for the first window..the A B and C buttons will open new frames.

      Code:
       import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.net.*;
      import javax.swing.event.*;
      
      class myscene extends JFrame implements ActionListener
      {
      
         public static void main (String[] args)
         {
            myscene frame = new myscene();
            frame.display();
         }
      
         void display()
         {
            setTitle("The World's Most Amazing Game of Pairs to Ever Have Existed");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
            setSize(600,250);
            setMinimumSize(new Dimension (550, 200));
            JPanel panel1 = new JPanel();
            JPanel panel2 = new JPanel();
            add(panel1);
            add(panel2, BorderLayout.SOUTH);
            panel1.setBackground(Color.black);
            panel2.setBackground(Color.black);
            JButton A = new JButton("New 4x3 Game");
            JButton B = new JButton("New 4x4 Game");
            JButton C = new JButton("New 5x4 Game");
            JButton Exit = new JButton("Exit Game");   
            panel2.add(A);
            panel2.add(B);
            panel2.add(C);
            panel2.add(Exit);
            Exit.setActionCommand("exit");
            Exit.addActionListener(this);
            A.setActionCommand("A");
            A.addActionListener(this);
      
            URL address = myscene.class.getResource("pairs.jpg");
            ImageIcon image = new ImageIcon(address);
            JLabel icon = new JLabel(image);
            panel1.add(icon);
            
         }
      
          protected void Exit()
          {
              System.exit(0);
          }
      
          protected void New()
          {
              
          }
          public void actionPerformed(ActionEvent event)
          {
              String source = event.getActionCommand();
          
          if(source.equals("A"))
          {
              New()
          }
          if(source.equals("exit"))
          {
              Exit();
          }
        }
      
      }
      I've started to write the New() method which should open the new frame but i dont know what to say

      I've also started the code for the grid window i intended to make the JPanel array Variable by setting the size as ints x and y dependant on which button is pressed.
      this is the loop i used to assign a JPanel to each part of the grid

      Code:
       
          getContentPane().setLayout(new GridLayout(5,5));
      
          JPanel[][] q = new JPanel[5][5];
      
          for (int i=0; i<5; i++)
      
          {
      
              for (int j=0; j<5; j++)
      
              {
      
              q[i][j] = new JPanel();
      
              getContentPane().add(q[i][j]);
      
              }
      
          }
      the loop is in a different class called 'Filenew', how would i go about calling this class from the 'myscene; class?

      Comment

      • nomad
        Recognized Expert Contributor
        • Mar 2007
        • 664

        #4
        Originally posted by nt5515
        i mean if i had a button in the initial frame class how do i get it to open a new grid frame with a variable size? on the first screen there is three buttons each of which will open a new frame with a grid size depending on which button is pressed.

        heres the code for the first window..the A B and C buttons will open new frames.

        the loop is in a different class called 'Filenew', how would i go about calling this class from the 'myscene; class?
        Try look at actionPerformed ();
        and
        ActionListerner ();

        all your class should be in the same package
        nomad

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by nomad
          all your class should be in the same package
          Where does that restriction come from?

          kind regards,

          Jos

          Comment

          • nt5515
            New Member
            • Feb 2008
            • 10

            #6
            hey thank guys, i managed to do it here wot i did
            Code:
            public void actionPerformed(ActionEvent event)
                {
                    String source = event.getActionCommand();
                
                if(source.equals("A"))
                {
            	newGame(4, 3);
                }
                if (source.equals("B"));
                {
             	newGame(4, 4);
                }
                if(source.equals("C"));
                {
            	newGame(5, 4);
                }
                if(source.equals("exit"))
                {
                    Exit();
                }
              }
            
               public void newGame(int x, int y) {
            	closeGame();
                    gameFrame = new Filenew(x, y);
            	gameFrame.display();
               }
            the new game method opens a new frame from the class 'Filenew' then two intergers are passed as it arguments which set the size of the grid in the new frame.
            Thanks for all the suggestions!

            Comment

            Working...