| | |
need help with jbutton and new jframe
![]() |
•
•
Join Date: Jun 2007
Posts: 59
Reputation:
Solved Threads: 3
Hi!
You could define a method, let's say popUp():
Now, you should have an actionPerformed method:
Assuming, you have defined the following line:
Oh, and one more thing, your class has to implement the ActionListener interface.
You could define a method, let's say popUp():
Java Syntax (Toggle Plain Text)
private void popUp() { JFrame frame = new JFrame("aName"); frame.setSize(500, 500); frame.setVisible(true); }
Java Syntax (Toggle Plain Text)
public void actionPerformed(ActionEvent event) { if (event.getSource() == button1) { popUp(); } }
Java Syntax (Toggle Plain Text)
button1.addActionListener(this);
•
•
•
•
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?
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
Last edited by happygeek; Jun 18th, 2007 at 6:05 am. Reason: Keep it on the site please rather than referring users to other forums for an answer
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Hang out place of novice and intermediate programmers
•
•
Join Date: Mar 2008
Posts: 6
Reputation:
Solved Threads: 0
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.");
}
}
}
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.");
}
}
}
•
•
Join Date: Jan 2008
Posts: 3,803
Reputation:
Solved Threads: 501
•
•
•
•
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.");
}
}
}
•
•
Join Date: Jan 2008
Posts: 3,803
Reputation:
Solved Threads: 501
I had something like this in mind:
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.
JAVA Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jan 2008
Posts: 3,803
Reputation:
Solved Threads: 501
•
•
•
•
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.
![]() |
Similar Threads
- java and using observable class (Java)
- how do i set background as an image with Jframe? ... and other stuff (Java)
- Passing control (Java)
- Printing JPanel containing JLable, JTable, JButton, JTextPane, JTextArea (Java)
- line up JTextField GUI java & write to file (Java)
- help with database assignment (Java)
- Memory buttons problem (Java)
Other Threads in the Java Forum
- Previous Thread: Inter Thread Communication!
- Next Thread: Could somebody help me please with java program?
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






