当用户点击JFrame的一角以调整大小并拖动鼠标时,JFrame会根据用户拖动时鼠标的当前位置重新绘制。你怎么能听到这些事件?
以下是我目前所尝试的内容:
public final class TestFrame extends JFrame {
public TestFrame() {
this.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
// This is only called when the user releases the mouse button.
System.out.println("componentResized");
}
});
}
// These methods do not appear to be called at all when a JFrame
// is being resized.
@Override
public void setSize(int width, int height) {
System.out.println("setSize");
}
@Override
public void setBounds(Rectangle r) {
System.out.println("setBounds A");
}
@Override
public void setBounds(int x, int y, int width, int height) {
System.out.println("setBounds B");
}
}
如何在鼠标周围拖动时,如何确定和约束用户调整窗口大小的方式(基于窗口的当前宽高比)?
答案 0 :(得分:39)
您可以添加组件侦听器并实现componentResized函数:
JFrame component = new JFrame("My Frame");
component.addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent evt) {
Component c = (Component)evt.getSource();
//........
}
});
编辑:显然,对于JFrame,componentResized事件被挂钩到mouseReleased事件。这就是释放鼠标按钮时调用该方法的原因。
实现您想要的一种方法是添加一个覆盖JFrame整个区域的JPanel。然后将componentListener添加到JPanel(即使在鼠标仍在拖动时也会调用componentResized for JPanel)。调整框架大小后,面板也会调整大小。
我知道,这不是最优雅的解决方案,但它有效!
答案 1 :(得分:7)
您可能需要覆盖validate
之类的内容(不要忘记调用超级)。当然,如果您使用窗口系统配置拖动轮廓,那么仍然可能无效。
答案 2 :(得分:6)
另一种解决方法(与Alex非常相似,但更直接一点)是从JFrame
的根窗格中收听事件:
public final class TestFrame extends JFrame {
public TestFrame() {
this.getRootPane().addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
// This is only called when the user releases the mouse button.
System.out.println("componentResized");
}
});
}
}
根据其他实施细节,可能会更改根窗格。如果这是可能的话,那么你可以覆盖setRootPane()
并处理它。由于setRootPane()
受到保护并且仅从构造函数中调用,因此您不太可能需要这样做。
答案 3 :(得分:-2)
public class MouseDrag extends Component implements MouseListener,
MouseMotionListener {
/** The Image we are to paint */
Image curImage;
/** Kludge for showStatus */
static Label status;
/** true if we are in drag */
boolean inDrag = false;
/** starting location of a drag */
int startX = -1, startY = -1;
/** current location of a drag */
int curX = -1, curY = -1;
// "main" method
public static void main(String[] av) {
JFrame f = new JFrame("Mouse Dragger");
Container cp = f.getContentPane();
if (av.length < 1) {
System.err.println("Usage: MouseDrag imagefile");
System.exit(1);
}
Image im = Toolkit.getDefaultToolkit().getImage(av[0]);
// create a MouseDrag object
MouseDrag j = new MouseDrag(im);
cp.setLayout(new BorderLayout());
cp.add(BorderLayout.NORTH, new Label(
"Hello, and welcome to the world of Java"));
cp.add(BorderLayout.CENTER, j);
cp.add(BorderLayout.SOUTH, status = new Label());
status.setSize(f.getSize().width, status.getSize().height);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// "Constructor" - creates the object
public MouseDrag(Image i) {
super();
curImage = i;
setSize(300, 200);
addMouseListener(this);
addMouseMotionListener(this);
}
public void showStatus(String s) {
status.setText(s);
}
// Five methods from MouseListener:
/** Called when the mouse has been clicked on a component. */
public void mouseClicked(MouseEvent e) {
}
/** Called when the mouse enters a component. */
public void mouseEntered(MouseEvent e) {
}
/** Called when the mouse exits a component. */
public void mouseExited(MouseEvent e) {
}
// And two methods from MouseMotionListener:
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
// System.err.println("mouse drag to " + p);
showStatus("mouse Dragged to " + p);
curX = p.x;
curY = p.y;
if (inDrag) {
repaint();
}
}
public void paint(Graphics g) {
int w = curX - startX, h = curY - startY;
Dimension d = getSize();
g.drawImage(curImage, 0, 0, d.width, d.height, this);
if (startX < 0 || startY < 0)
return;
System.err.println("paint:drawRect @[" + startX + "," + startY
+ "] size " + w + "x" + h);
g.setColor(Color.red);
g.fillRect(startX, startY, w, h);
}
}