我有一些代码可以动态调整聊天面板的大小,但它不会根据鼠标移动。发生的情况是鼠标移动的速度快于面板调整大小的速度。例如,我希望它是这样的,在任何应用程序中,当您单击两个窗口之间的边框并拖动它时,鼠标与您单击的部分保持一致,但目前这种情况没有发生。这是我的代码。如果您需要更多,请告诉我
public void mouseDragged(MouseEvent e) {
if(getCursor().getType() == Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR).getType()) {
owner.adjust((int)MouseInfo.getPointerInfo().getLocation().getY());
System.out.println("vertpanel: "+(int)MouseInfo.getPointerInfo().getLocation().getY());
}
}
public void adjust(int adjustment) {
int height = this.getHeight();
System.out.println((((double)(adjustment))/height)+":"+(((double)(height-adjustment))/height));
output.setHeightPercent((((double)(adjustment))/height));
output.componentResized(new ComponentEvent(this, 1));
input.setHeightPercent((((double)(height-adjustment))/height));
input.componentResized(new ComponentEvent(this, 2));
}
有一个主面板,一个聊天面板,其中有两个较小的面板,一个聊天输入和一个聊天输出
最佳答案
无法根据您的代码准确判断您在做什么。
我建议您不要手动设置输出和输入组件的尺寸。您应该让布局管理器确定如何在调整父容器大小时调整每个组件的大小。
因此,在调整大小代码中,您需要在调整大小时在父容器上调用 revalidate()。
查看Resizing Components 。只要使用 setAutoLayout(true)
,您就应该能够使用 ComponentResizer
类。
关于java - 调整大小代码与鼠标移动不对应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012046/