473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

action listener and linking classes Please help!

10
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
Feb 25 '08 #1
5 1722
BigDaddyLH
1,216 Expert 1GB
What do you mean by "link classes"? What if one class mentions the other? Would that suffice?
Feb 25 '08 #2
nt5515
10
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.

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5. import javax.swing.event.*;
  6.  
  7. class myscene extends JFrame implements ActionListener
  8. {
  9.  
  10.    public static void main (String[] args)
  11.    {
  12.       myscene frame = new myscene();
  13.       frame.display();
  14.    }
  15.  
  16.    void display()
  17.    {
  18.       setTitle("The World's Most Amazing Game of Pairs to Ever Have Existed");
  19.       setDefaultCloseOperation(EXIT_ON_CLOSE);
  20.       setVisible(true);
  21.       setSize(600,250);
  22.       setMinimumSize(new Dimension (550, 200));
  23.       JPanel panel1 = new JPanel();
  24.       JPanel panel2 = new JPanel();
  25.       add(panel1);
  26.       add(panel2, BorderLayout.SOUTH);
  27.       panel1.setBackground(Color.black);
  28.       panel2.setBackground(Color.black);
  29.       JButton A = new JButton("New 4x3 Game");
  30.       JButton B = new JButton("New 4x4 Game");
  31.       JButton C = new JButton("New 5x4 Game");
  32.       JButton Exit = new JButton("Exit Game");   
  33.       panel2.add(A);
  34.       panel2.add(B);
  35.       panel2.add(C);
  36.       panel2.add(Exit);
  37.       Exit.setActionCommand("exit");
  38.       Exit.addActionListener(this);
  39.       A.setActionCommand("A");
  40.       A.addActionListener(this);
  41.  
  42.       URL address = myscene.class.getResource("pairs.jpg");
  43.       ImageIcon image = new ImageIcon(address);
  44.       JLabel icon = new JLabel(image);
  45.       panel1.add(icon);
  46.  
  47.    }
  48.  
  49.     protected void Exit()
  50.     {
  51.         System.exit(0);
  52.     }
  53.  
  54.     protected void New()
  55.     {
  56.  
  57.     }
  58.     public void actionPerformed(ActionEvent event)
  59.     {
  60.         String source = event.getActionCommand();
  61.  
  62.     if(source.equals("A"))
  63.     {
  64.         New()
  65.     }
  66.     if(source.equals("exit"))
  67.     {
  68.         Exit();
  69.     }
  70.   }
  71.  
  72. }
  73.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2.     getContentPane().setLayout(new GridLayout(5,5));
  3.  
  4.     JPanel[][] q = new JPanel[5][5];
  5.  
  6.     for (int i=0; i<5; i++)
  7.  
  8.     {
  9.  
  10.         for (int j=0; j<5; j++)
  11.  
  12.         {
  13.  
  14.         q[i][j] = new JPanel();
  15.  
  16.         getContentPane().add(q[i][j]);
  17.  
  18.         }
  19.  
  20.     }
  21.  
the loop is in a different class called 'Filenew', how would i go about calling this class from the 'myscene; class?
Feb 26 '08 #3
nomad
664 Expert 512MB
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
Feb 26 '08 #4
JosAH
11,448 Expert 8TB
all your class should be in the same package
Where does that restriction come from?

kind regards,

Jos
Feb 26 '08 #5
nt5515
10
hey thank guys, i managed to do it here wot i did
Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent event)
  2.     {
  3.         String source = event.getActionCommand();
  4.  
  5.     if(source.equals("A"))
  6.     {
  7.     newGame(4, 3);
  8.     }
  9.     if (source.equals("B"));
  10.     {
  11.      newGame(4, 4);
  12.     }
  13.     if(source.equals("C"));
  14.     {
  15.     newGame(5, 4);
  16.     }
  17.     if(source.equals("exit"))
  18.     {
  19.         Exit();
  20.     }
  21.   }
  22.  
  23.    public void newGame(int x, int y) {
  24.     closeGame();
  25.         gameFrame = new Filenew(x, y);
  26.     gameFrame.display();
  27.    }
  28.  
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!
Feb 26 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Cherrish Vaidiyan | last post by:
sir, I have a small error in Listener configuration.I have two system with a database in each. I am using Red Hat 9 and Oracle 9i. so i shall anme the database and system. system 1 - node2 ...
1
by: Orgad | last post by:
I wrote a simple program (in which I reused a data structure), but it refuses to link. Can anyone here please help me? Notice that Region2D.cpp IS in the compilation, and the "unresolved"...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
0
by: gasturbtec | last post by:
please help im new at access programming and i just got this project dropped in my lap because the old programmer quit. i've been doing ok so far but now i need to add code to an existing database...
4
by: Gary Hughes | last post by:
Hi all, sometime I posted a problem in here where I was getting the following error from the linker in VS C++ 2003. Linking... GCClass.obj : error LNK2022: metadata operation failed (80131188)...
6
by: Steve Teeples | last post by:
I have been perplexed by how to best treat an event that spans different classes. For example, I have a form which a user inputs data. I want to broadcast that data via an event to another...
9
by: finerrecliner | last post by:
i'm trying to make an action listener that will display an alert box when the user clicks anywhere on the page once, and then go back to normal mouse behavior. i have this: <html><script...
4
by: Richard | last post by:
First question - let's get this out of the way since it might be the solution to all my woes: Does it make sense to have a .cpp file for a class that is declared as having pure virtual functions...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.