Forum Stats

  • 3,826,586 Users
  • 2,260,667 Discussions
  • 7,897,010 Comments

Discussions

counting mouse button clicks

866915
866915 Member Posts: 3
edited Jun 6, 2011 8:19PM in Java Programming
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.

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!
Tagged:

Answers

  • 823824
    823824 Member Posts: 18
    will this solve your problem?

    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
    {
    TextField objTextField;
    int count =0;

    public static void main(String args[])
    {
    ButtonStart BS= new ButtonStart();
    }

    public ButtonStart()
    {
    Frame objFrame;
    Button objButton;
    final TextField objTextField;
    objFrame=new Frame("Clicking Buttons");
    objFrame.setLayout(new BorderLayout());
    final Label l1 = new Label("");
    objButton= new Button("Click me!");
    objTextField= new TextField(100);
    objTextField.setBounds(90, 50, 100, 100);
    objButton.setBounds(10, 30, 60, 60);
    objFrame.add(l1,BorderLayout.CENTER);
    objButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    count++;
    String s = Integer.toString(count);
    l1.setText(s);
    }
    });

    objFrame.setSize(300,300);
    objFrame.setVisible(true);
    objFrame.add(objButton,BorderLayout.NORTH);
    objFrame.add(objTextField,BorderLayout.SOUTH);
    objFrame.addWindowListener(new WindowAdapter()

    {
    public void windowClosing(WindowEvent we)
    {
    System.exit(0);
    }
    });
    }
    }
  • 866915
    866915 Member Posts: 3
    THANK YOU SO MUCH YOU HAVE NO IDEA! :D
This discussion has been closed.