I'm learning java.

I have one Jframe with 5 jbuttons. I want to click button1 to have a new Jframe come up. How should I define the 2nd Jframe? class? public class?

Hi!
You could define a method, let's say popUp():

private void popUp()
{
    JFrame frame = new JFrame("aName");
    frame.setSize(500, 500);
    frame.setVisible(true);
}

Now, you should have an actionPerformed method:

public void actionPerformed(ActionEvent event)
{
	if (event.getSource() == button1)	
                {
                        popUp();
                 }
}

Assuming, you have defined the following line:

button1.addActionListener(this);

Oh, and one more thing, your class has to implement the ActionListener interface.

Thanks a lot!

I'm learning java.

I have one Jframe with 5 jbuttons. I want to click button1 to have a new Jframe come up. How should I define the 2nd Jframe? class? public class?

you just need to add Action Litener to your button, and use JPanel there, if that's ok for you... :-/ i guess that's the easiest way. Here's link if you want to know how to add action listener..

<URL snipped>

you can always use http://java.sun.com/ to search for the tutorial and APIs

It compliles and runs but does not do anything when i click on the button

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
import java.io.*;
import javax.swing.JOptionPane;
import java.lang.*;
import java.math.*;

public class last_try implements ActionListener
{

 public static void main(String[] args)
  {

    JFrame f = new JFrame("What would you like to do");
    f.setSize(400, 150);
    Container content = f.getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());


    JButton button1 =  new JButton("Button 1");
    JButton button2 =  new JButton("Button 2");
    JButton button3 =  new JButton("Button 3");

    Container contentPane = f.getContentPane();
            contentPane.setLayout(new FlowLayout());
            contentPane.add(button1);
            contentPane.add(button2);
            contentPane.add(button3);


    f.addWindowListener(new ExitListener());
    f.setVisible(true);
}

public void popUp()
{
       JFrame frame = new JFrame("aName");
      frame.setSize(500, 500);
       frame.setVisible(true);

}


 public void actionPerformed(ActionEvent event)
        {

            if (event.getSource() == "Button 1")
                      {
                        JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.");
                      }

        }
 }

It compliles and runs but does not do anything when i click on the button

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
import java.io.*;
import javax.swing.JOptionPane;
import java.lang.*;
import java.math.*;

public class last_try implements ActionListener
{

 public static void main(String[] args)
  {

    JFrame f = new JFrame("What would you like to do");
    f.setSize(400, 150);
    Container content = f.getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());


    JButton button1 =  new JButton("Button 1");
    JButton button2 =  new JButton("Button 2");
    JButton button3 =  new JButton("Button 3");

    Container contentPane = f.getContentPane();
            contentPane.setLayout(new FlowLayout());
            contentPane.add(button1);
            contentPane.add(button2);
            contentPane.add(button3);


    f.addWindowListener(new ExitListener());
    f.setVisible(true);
}

public void popUp()
{
       JFrame frame = new JFrame("aName");
      frame.setSize(500, 500);
       frame.setVisible(true);

}


 public void actionPerformed(ActionEvent event)
        {

            if (event.getSource() == "Button 1")
                      {
                        JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.");
                      }

        }
 } 

end quote.

See post #2. You never added the action listeners to the buttons. You may have to rearrange/redesign some code and add those action listeners outside of the main function due to the fact that main is static.

Thanks. I tried that I just can not figure it out

I had something like this in mind:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
import java.io.*;
import javax.swing.JOptionPane;
import java.math.*;

public class last_try implements ActionListener 
{
    // put variable declarations here (i.e. code below)
    JFrame f;
    JButton button1;
    JButton button2;
    // etc. for other variables


    public last_try()   // new constructor.  No longer have "static" limitiations
    {
        // code here that uses the variables declared above
        // see code below


        f = new JFrame("What would you like to do");
        f.setSize(400, 150);
        // more code to set up GUI

        button1 = new JButton("Button 1");
        button2 = new JButton("Button 2");
        button3 = new JButton("Button 3");

        button1.addActionListener(this);  // add action listeners here
        button2.addActionListener(this);

        // more code


        // need more code to use the line below, so commented out.
        // Are you sure you really want it?
        // f.addWindowListener(new ExitListener());

        f.setVisible(true);
    }

    public static void main(String[] args) 
    {
        last_try last = new last_try();
    }

    public void popUp() 
    {
        JFrame frame = new JFrame("aName");
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) 
    {
        System.out.println("In actionPerformed");

        if (event.getSource() == button1)   // no quotes here
        {
            JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.");
        }
    }
}

Note that there are no quotes around "button1" in line 63 and that it matches line 15 and that an action listener was added to button1 in line 34. Main is very small and only calls a constructor. The constructor does not have the "static" limitations that that "main" has, so you will have fewer problems using worlds like "new". Note that the variables are declared OUTSIDE of the constructor. This makes them global and hence usable by the action listener, which they may not have been had variables like "button1" been declared in "main" or in the constructor. Try to add the code where I put the comments and see if it you can get it to work. Post back with more questions if you cannot.

my frame is working well but it freezes in the middle. What may be the reason.?

my frame is working well but it freezes in the middle. What may be the reason.?

You should start a new thread. Post the code and describe the exact problem and the circumstances under which the problems occurs. Any answer given here would be pure speculation since you haven't posted the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.