counting mouse button clicks
This is my program so far:
It complies an runs, but the textfield ends up being as big as the entire frame, and when I click the button, nothing happens.
Code java:
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class ButtonStart extends Frame
{
private int mouseclicked=0;
TextField objTextField;
public static void main(String args[])
{
ButtonStart BS= new ButtonStart();
}
public ButtonStart()
{
Frame objFrame;
Button objButton;
TextField objTextField;
objFrame=new Frame("Clicking Buttons");
objButton= new Button("Click me!");
objTextField= new TextField("0");
objTextField.setBounds(90, 50, 100, 100);
objButton.setBounds(10, 30, 60, 60);
objFrame.addMouseListener(new MyMouseListener());
objFrame.setSize(300,300);
objFrame.setVisible(true);
objFrame.add(objButton);
objFrame.add(objTextField);
objFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
int mouseclicked= me.getClickCount();
objTextField.setText("Mouse clicked this many times:" + mouseclicked );
}
}
}
Anyone have any idea of how I could make this program work? Thanks!
Re: counting mouse button clicks
When posting code, make sure you use the highlight tags to preserve formatting. I added them for you this time. Also, do you really not use any indentations?
The problem with the click count is that you're misunderstanding what MouseEvent.getClickCount() returns. If you want to keep track of the total number of times the mouse has been clicked, you'll have to increment a variable yourself.
As for your layout issues, here are some questions:
Why are you using Frame instead of JFrame, and TextField instead of JTextField?
Why are you extending Frame?
What layout are you using?
Re: counting mouse button clicks
You could just add 1 to an integer variable every time the mousePressed method is called.
Re: counting mouse button clicks
Quote:
Originally Posted by
Shazer2
You could just add 1 to an integer variable every time the mousePressed method is called.
That's a bit misleading, IMHO. Adding one to the integer he's using currently will simply be 1 + getClickCount(), which is usually very small. What he wants to do is increment a variable that has scope outside of the method.
Re: counting mouse button clicks
Quote:
when I click the button, nothing happens
Another possible explanation for nothing happening is that you are NOT overriding the method. Since Adapters provide all the methods in the interface, if you mistype the method name, the compiler is happy to add a new method.
Add an @Override statement before any methods you add to an Adapter to let the compiler check it for you
Re: counting mouse button clicks
Yeah, now that I'm looking at it a little more closely, you've got a few wonky things going on before you even get to your logic error.
I really recommend switching to Swing instead of AWT, if only for the sanity of people who might answer your questions.
I'd also recommend using a layout- setting the bounds explicitly is usually more trouble than its worth.
Finally, you might want to make your requirements a little more exact- what exactly do you want to count? The number of times the user clicks the button? Or anywhere in the frame? Or something else?