0

EDIT: I have fixed the problem. Thank you all for your help.

I am pretty new to programming, and have been struggling through an online course. I am now working on my final project, which is to: Write a program to count the number of mouse clicks on a button within a frame. The code I have seems to be WAY off. Keep in mind that this is an applet. Here is the program:

import java.awt.*;
import java.awt.event.*;
import java.awt.MouseAdapter;

public class finalproject1
{
    TextField objTextField1;

    public static void main(String[] args)
    {
        finalproject1 p1 = new finalproject1();
    }

    public finalproject1
    {
        Frame f = new Frame("Mouse Clicks");
        objTextField1 = new TextField("Click the button",200);
        objTextField1.setBounds(220,140,200,40);
        Button button1 = new Button("Click here");
        button1.setBounds(200,200,140,140);
        button1.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent evt)
            {
                if(evt.getClickCount() == 3)
                {
                    objTextField1TextField1.setText("Triple click");
                }
                else if(evt.getClickCount() ==2)
                {   
                    objTextField1.setText("Double click");
                }
            });
        }
        f.add(button1); 
        f.add(objTextField1);
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
        f.setSize(800,800);     
        f.setVisible(true);     
    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
  • 4
    Elaborate. What does "Way off" even mean? What are you errors? – user1231232141214124 May 27 '14 at 22:57
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 28 '14 at 08:02

2 Answers2

0

First of all, I'd recommend you fix your tabbing to make sure you're looking at scoping correctly. In addition, there are some other quirks here that could be messing things up -- this really ought not to even compile.

It looks like you're trying to define a constructor, but you haven't added parentheses. This:

public finalproject1 { ... }

should become this:

public finalproject1() { ... }

It's also convention to name classes in camelcase, so FinalProject1 would be a better name.

Your paren placement is also off in this code:

button1.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent evt) {
        if(evt.getClickCount() == 3) {
            objTextField1TextField1.setText("Triple click");
        } else if(evt.getClickCount() ==2) {   
            objTextField1.setText("Double click");
        }
    }); // This ");" should be one brace down from where it is.
}

I can't help much more without knowing what you mean by "WAY off." Can you elaborate?

Mac O'Brien
  • 2,407
  • 18
  • 22
0
  • parentheses are missing to the constructor : public finalproject1() { ... }
  • ");" at the end of method mouseClicked must be at the end of method addMouseListener.
  • object objTextField1TextField1 is not declared. It should be objTextField1 instead.
  • class name must start with upper case (Java convention)
Reno
  • 51
  • 1