View Single Post
Join Date: Jan 2008
Posts: 3,755
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help with jbutton and new jframe

 
0
  #8
Mar 13th, 2008
I had something like this in mind:

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.text.*;
  6. import java.text.DecimalFormat;
  7. import java.io.*;
  8. import javax.swing.JOptionPane;
  9. import java.math.*;
  10.  
  11. public class last_try implements ActionListener
  12. {
  13. // put variable declarations here (i.e. code below)
  14. JFrame f;
  15. JButton button1;
  16. JButton button2;
  17. // etc. for other variables
  18.  
  19.  
  20. public last_try() // new constructor. No longer have "static" limitiations
  21. {
  22. // code here that uses the variables declared above
  23. // see code below
  24.  
  25.  
  26. f = new JFrame("What would you like to do");
  27. f.setSize(400, 150);
  28. // more code to set up GUI
  29.  
  30. button1 = new JButton("Button 1");
  31. button2 = new JButton("Button 2");
  32. button3 = new JButton("Button 3");
  33.  
  34. button1.addActionListener(this); // add action listeners here
  35. button2.addActionListener(this);
  36.  
  37. // more code
  38.  
  39.  
  40. // need more code to use the line below, so commented out.
  41. // Are you sure you really want it?
  42. // f.addWindowListener(new ExitListener());
  43.  
  44. f.setVisible(true);
  45. }
  46.  
  47. public static void main(String[] args)
  48. {
  49. last_try last = new last_try();
  50. }
  51.  
  52. public void popUp()
  53. {
  54. JFrame frame = new JFrame("aName");
  55. frame.setSize(500, 500);
  56. frame.setVisible(true);
  57. }
  58.  
  59. public void actionPerformed(ActionEvent event)
  60. {
  61. System.out.println("In actionPerformed");
  62.  
  63. if (event.getSource() == button1) // no quotes here
  64. {
  65. JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.");
  66. }
  67. }
  68. }

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.
Reply With Quote