Results 1 to 2 of 2
  1. #1
    garnachito is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Call a class from another class

    Hi guys!

    So, I've this code in one file called one.java

    Java Code:
    import javax.swing.JOptionPane;
    
    public class one
    {
    	public static void main(String[]args)
    	{
    		int Valor,i;
    		String Salida="";
    		
    		for(i=1;i<=20;i++)
    		{
    			Valor= 1 + (int) (Math.random() * 6);
    			Salida+= Valor + "     ";
    			
    			if(i%5==0)
    			{
    			Salida+="\n";
    			}
    		}
    	JOptionPane.showMessageDialog(null,Salida,"20 Nos. Aleatorios del 1 al 6",JOptionPane.INFORMATION_MESSAGE);
    	System.exit(0);
    	}
    }
    And what I need is to call this program from a button created in a second file called two.java how can I call it ?

    Java Code:
        public void actionPerformed(ActionEvent e)
        {
        	if (e.getSource ()==boton1)
        	{
        		// I need to create a code here to call the code in one.java
        	}
       }
    Thanks in advance !!! :)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Call a class from another class

    The way you have arranged your code, the only thing to call is the main( ... ) method; you can do this:

    Java Code:
    public void actionPerformed(ActionEvent e) {
       if (e.getSource () == boton1)
          one.main(null);
    }
    But I think you need to give it a second thought ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  2. how do i call a class from main class
    By revanger in forum New To Java
    Replies: 7
    Last Post: 07-20-2010, 05:32 PM
  3. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  4. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  5. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 03:49 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
  •