I want to resize the JButton
at runtime by clicking on its border and draging it. Can anyone explain me how to do it with a sample code.
public void mouseDragged(MouseEvent E)
{
Point point= E.getPoint();
//JButton get = floor_plan.dynamicButtons.get(E.getComponent());
JButton get=(JButton) E.getComponent();
int height = get.getHeight();
int width = get.getWidth();
开发者_高级运维int X=E.getXOnScreen();
int Y=E.getYOnScreen();
if(floor_plan.resize==1)
if (floor_plan.isHeld) {
System.out.println(X);
System.out.println(Y);
get.setPreferredSize(
new Dimension(floor_plan.grabbedDimension.width -
(floor_plan.grabbedPoint.x - point.x),
floor_plan.grabbedDimension.height -
(floor_plan.grabbedPoint.y - point.y)));
get.setBounds(new Rectangle(get.getLocation(), get.getPreferredSize()));
return;
}
System.out.println("height:"+height);
System.out.println("width:"+width);
get.setBounds(X-240,Y-125,height,width);
}
Well, this might do it for you. When adding the new button, simply add a ResizableButton
instead.
Note, it will resize no matter where you click on it.
public class ResizableButton extends JButton {
private Point grabbedPoint;
private Dimension grabbedDimension;
private boolean isHeld = false;
public ResizableButton(String name) {
super(name);
addMouseListener(clickListener);
addMouseMotionListener(moveListener);
}
private MouseMotionListener moveListener = new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (isHeld) {
Point newP = e.getPoint();
setPreferredSize(new Dimension(grabbedDimension.width
- (grabbedPoint.x - newP.x), grabbedDimension.height
- (grabbedPoint.y - newP.y)));
setBounds(new Rectangle(getLocation(), ResizableButton.this
.getPreferredSize()));
}
}
};
private MouseListener clickListener = new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
isHeld = false;
}
@Override
public void mousePressed(MouseEvent e) {
isHeld = true;
grabbedPoint = e.getPoint();
grabbedDimension = ((JButton) e.getSource()).getSize();
}
};
}
Below is code for a button that moves or re-sizes based on whether or not the Alt button is pressed.:
public class MovableResizableButton extends JButton {
private boolean isHeld;
private Point pointClicked;
private Dimension startingSize;
public MovableResizableButton(String name) {
super(name);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (isHeld) {
Dimension newSize = getPreferredSize();
Point newPoint = getLocation();
if ((e.getModifiersEx() & InputEvent.ALT_DOWN_MASK) == InputEvent.ALT_DOWN_MASK) {
newSize = new Dimension(startingSize.width - (pointClicked.x - e.getPoint().x),
startingSize.height -(pointClicked.y - e.getPoint().y));
}else {
Point startPoint = getLocation();
newPoint = new Point(startPoint.x - (pointClicked.x - e.getPoint().x),
startPoint.y - (pointClicked.y - e.getPoint().y));
}
setPreferredSize(newSize);
setBounds(new Rectangle(newPoint, getPreferredSize()));
}
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
pointClicked = e.getPoint();
startingSize = getSize();
isHeld = true;
}
@Override
public void mouseReleased(MouseEvent e) {
isHeld = false;
}
});
}
}
- Attach a MouseListener to the button
- Attach a MouseMotionListener to the button
- On mousePressed you register the location of the MouseEvent
- On mouseDragged you register the new location of the MouseEvent you take the delta of the two events and add that to the size of the button using setSize/setPreferredSize/whatever they are called
try
http://tips4java.wordpress.com/2009/09/13/resizing-components/
and for moving components
http://tips4java.wordpress.com/2009/06/14/moving-windows/
精彩评论