943,731 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 316
  • Java RSS
Jun 11th, 2009
0

handeling button

Expand Post »
I've written a code for JButton handeling.The code is=
java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.Calendar;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.ActionEvent;
  6.  
  7. // Example using defaultButton and JRootPane.setDefaultButton()
  8. public class DefaultButtonExample {
  9.  
  10.  
  11. public static void main(String[] args) {
  12. // Create some buttons
  13. JButton ok = new JButton("OK");
  14. JButton cancel = new JButton("Cancel");
  15. JPanel buttonPanel = new JPanel();
  16. buttonPanel.add(ok);
  17. buttonPanel.add(cancel);
  18.  
  19. Calendar cl=Calendar.getInstance();
  20. int i=cl.get(Calendar.YEAR);
  21. int j=cl.get(Calendar.MONTH);
  22. String a="001";
  23. i=i%100;
  24. String p=Integer.toString(i);
  25. String q=Integer.toString(j);
  26.  
  27. if(p.length()==1)
  28. p=0+p;
  29. if(q.length()==1)
  30. q=0+q;
  31. String r=p+q+a;
  32. public void actionPerformed(ActionEvent ae){
  33. String str=ae.getActionCommand();
  34. if(str.equals("ok"))
  35. {
  36. int b=Integer.parseInt(a);
  37. b++;
  38. String a1=Integer.toString(b);
  39. if(a1.length()==1)
  40. a1=00+a1;
  41. if(a1.length()==2)
  42. a1=0+a1;
  43. r=p+q+a1;
  44.  
  45.  
  46.  
  47. JLabel msg = new JLabel("visitor ID="+r, JLabel.CENTER);
  48.  
  49.  
  50. // Create a frame, get its root pane, and set the "ok" button as the
  51. // default. This button will be "pressed" if we hit <ENTER> while the
  52. // frame has focus.
  53. JFrame f = new JFrame();
  54. //f.addWindowListener(new BasicWindowMonitor());
  55. JRootPane root = f.getRootPane();
  56. root.setDefaultButton(ok);
  57. // Layout and Display
  58. Container content = f.getContentPane();
  59.  
  60. content.setLayout(new BorderLayout());
  61. content.add(msg, BorderLayout.CENTER);
  62. content.add(buttonPanel, BorderLayout.SOUTH);
  63. f.setSize(200,100);
  64. f.setVisible(true);
  65.  
  66.  
  67. }
  68. }




But getting the following errors while compiling it.
Attached Thumbnails
Click image for larger version

Name:	12345.JPG
Views:	9
Size:	33.8 KB
ID:	10435  
Last edited by Tekmaven; Jun 11th, 2009 at 11:00 pm. Reason: Code Tags
Similar Threads
Reputation Points: 1
Solved Threads: 0
Light Poster
coolbuddy059 is offline Offline
45 posts
since Aug 2008
Jun 11th, 2009
0

Re: handeling button

You can't write methods in the middle of other methods.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Reading from File
Next Thread in Java Forum Timeline: SetHorizontalTextPosition on a JLabel won't work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC