![]() |
| ||
| need help with jbutton and new jframe 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? |
| ||
| Re: need help with jbutton and new jframe Hi! You could define a method, let's say popUp(): private void popUp()Now, you should have an actionPerformed method: public void actionPerformed(ActionEvent event)Assuming, you have defined the following line: button1.addActionListener(this);Oh, and one more thing, your class has to implement the ActionListener interface. |
| ||
| Re: need help with jbutton and new jframe Thanks a lot! |
| ||
| Re: need help with jbutton and new jframe Quote:
<URL snipped> you can always use http://java.sun.com/ to search for the tutorial and APIs |
| ||
| Re: need help with jbutton and new jframe 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."); } } } |
| ||
| Re: need help with jbutton and new jframe Quote:
|
| ||
| Re: need help with jbutton and new jframe Thanks. I tried that I just can not figure it out |
| ||
| Re: need help with jbutton and new jframe 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. |
| ||
| Re: need help with jbutton and new jframe my frame is working well but it freezes in the middle. What may be the reason.? |
| ||
| Re: need help with jbutton and new jframe Quote:
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. |
| All times are GMT -4. The time now is 3:15 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC