本文介绍了jButtons在运行时调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在运行时通过单击其边框并拖动它来调整 JButton
的大小。任何人都可以用示例代码向我解释如何使用它。
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);
}
推荐答案
嗯,这可能会为你做到这一点。添加新按钮时,只需添加 ResizableButton
。
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 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;
}
});
}
}
这篇关于jButtons在运行时调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!