【发布时间】:2015-05-21 06:15:22
【问题描述】:
我正在尝试通过拖动 JPanel 来手动调整 JFrame 的大小。这是我所做的。目前可以,但屏幕仍然闪烁,并且调整大小不正确
我希望它发生的是,当我点击 Jpanel 并拖动它时,JFrame 应该自动调整到 X 坐标和 Y 坐标所在的位置。
public void mouseDragged(MouseEvent e) {
// Sets width, height and moved X and Y coordinates
int currentwidth = this.getWidth();
int currentheight = this.getHeight();
int newwidth = 0;
int newheight = 0 ;
int thisX = this.getX();
int thisY = this.getY();
// Calculates the moving distance
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
// Checks which component the mouse is clicked on
if(e.getComponent() == reszingbit){
// Calculates the new height and width
newwidth = 200 + xMoved;
newheight = 200 + yMoved;
// Making sure that the new height and width is not less than actual size
if(newwidth >= 200 && newheight >= 200){
this.setSize(newwidth,newheight);
}
}
200 是实际的高度和宽度。
【问题讨论】:
-
考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
标签: java swing user-interface