• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setBounds() does not work????

 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Hey!!
Why is setBounds() not working in the following applet:
**************************************************
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class mouseevent1 extends Applet implements MouseListener, MouseMotionListener
{

Panel p1;
public void init()
{
p1=new Panel();
p1.setBounds(50, 50, 200, 300);
p1.setBackground(Color.red);
p1.addMouseListener(this);
p1.addMouseMotionListener(this);
add(p1);
}

public void mouseMoved(MouseEvent e)
{
showStatus("Mouse Moved at : "+e.getWhen());
}

public void mouseDragged(MouseEvent e)
{
showStatus("Mouse Dragged at : "+e.getWhen());
}

public void mouseClicked(MouseEvent e)
{
showStatus("Mouse Clicked at : "+e.getX()+", "+e.getY());
}


public void mouseEntered(MouseEvent e)
{
showStatus("Mouse Entered at : "+e.getPoint());
}

public void mouseExited(MouseEvent e)
{
showStatus("Mouse Exiting at : "+e.getPoint());
}

public void mousePressed(MouseEvent e)
{
showStatus("Mouse Pressed at : "+e.getPoint());
e.translatePoint(200, 200);
showStatus("Mouse Point Translated to : "+e.getPoint());
}

public void mouseReleased(MouseEvent e)
{
showStatus("Mouse Released from : "+e.getX()+", "+e.getY());
}
}
/*
<applet code="mouseevent1" width=500 height=500></applet>
*/

**************************************************

Is is showing a square in red color, with the size of half a dice, and is placed in the upper("North"), center position of the applet.
I wish to expand this red square to a considerable size to test my mouse Events.
Also, how to use the getClickCount() method. If I use this method and then start clicking the mouse button, it shows the click counts, if they are done in a small time frame. If the time frame is over, it calculates the click counts from 0 again. So any help on that will be good.
Please Help!!!
Bye,
Tualha Khan
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this to the Applet forum where you are likely to get a better response.
Bill
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tualha,
To expand your Panel, simply change the Applet's layout manager to BorderLayout and place your Panel in the center. This will make your Panel the size of the Applet.

I also edited the mouseClicked() event to show the number of times the mouse was clicked at that location using getClickCount(). If you move the mouse between clicks, it starts over with the counting.
Hope this helps.
Stephanie
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tualha,
setBounds() will only really work if you set the layout to null (i.e. setLayout( null ); ). Otherwise, the layout manager will override the components boundary setting and, in most cases, the prefered min/max as well. Unfortunately, this means you have to do all of your layout and resize handling by hand (if you want to have fine-grained control over you components).
Sean
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!!!
Bye,
Tualha Khan
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic