I've written a code for JButton handeling.The code is=

import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

// Example using defaultButton and JRootPane.setDefaultButton()
public class DefaultButtonExample  {


public static void main(String[] args) {
// Create some buttons
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
JPanel buttonPanel = new JPanel();
buttonPanel.add(ok);
buttonPanel.add(cancel);

Calendar cl=Calendar.getInstance();
  int i=cl.get(Calendar.YEAR);
  int j=cl.get(Calendar.MONTH);
  String a="001";
  i=i%100;
  String p=Integer.toString(i);
  String q=Integer.toString(j);

  if(p.length()==1)
  p=0+p;
  if(q.length()==1)
  q=0+q;
  String r=p+q+a;
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if(str.equals("ok"))
{
 int b=Integer.parseInt(a);
 b++;
 String a1=Integer.toString(b);
 if(a1.length()==1)
  a1=00+a1;
 if(a1.length()==2)
  a1=0+a1;
  r=p+q+a1;



JLabel msg = new JLabel("visitor ID="+r, JLabel.CENTER);


// Create a frame, get its root pane, and set the "ok" button as the
// default. This button will be "pressed" if we hit <ENTER> while the
// frame has focus.
JFrame f = new JFrame();
//f.addWindowListener(new BasicWindowMonitor());
JRootPane root = f.getRootPane();
root.setDefaultButton(ok);
// Layout and Display
Container content = f.getContentPane();

content.setLayout(new BorderLayout());
content.add(msg, BorderLayout.CENTER);
content.add(buttonPanel, BorderLayout.SOUTH);
f.setSize(200,100);
f.setVisible(true);


}
}

But getting the following errors while compiling it.

You can't write methods in the middle of other methods.

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.