我想制作一个可以关闭/移除的可拖动且可调整大小的面板。拖动面板工作正常,但我的调整大小代码中存在一些错误。它工作但不是很好。也许有人可以帮助我解决它。问题是设置标签的正确位置“关闭”和“调整大小”。
感谢您的帮助
public class PanelDraggable extends JPanel {
private Point pointPressed;
int width1;
int height1;
int headerHeight;
int closeDim;
JLabel close;
JLabel resize;
JPanel cs;
JLabel header;
public PanelDraggable( int x, int y, int width, int height, String lblName) {
// draggable.setCursor(draggable.getCursor());
// setCursor(new Cursor(Cursor.HAND_CURSOR));
width1 = width; height1 = height;
closeDim = 15;
headerHeight = 20;
setBounds(x, y, width, height);
this.setLayout(null);
setBorder(new LineBorder(new Color(147, 147, 255), 3, true));
this.setOpaque(true);
//header
header = new JLabel(lblName);
this.add(header);
header.setBounds(0, 0, width-(closeDim + 2), headerHeight);
header.setBorder(new LineBorder(new Color(147, 147, 255), 3, true));
header.setBackground(new Color(147, 147, 255));
header.setOpaque(true);
MouseInputAdapter mouseAdapter = new HeaderMouseHandler();
header.addMouseMotionListener(mouseAdapter);
header.addMouseListener(mouseAdapter);
//close Button
close = new JLabel("cx");
close.setIcon(new ImageIcon("icons/close_16x16.png"));
close.setBounds( (width- (closeDim + 2)) , 2, closeDim, closeDim);
close.setCursor(new Cursor(Cursor.HAND_CURSOR));
this.add(close);
//Container
cs = new JPanel();
cs.setBounds(0, headerHeight, width, (height-headerHeight) );
cs.setOpaque(false);
cs.setBackground(Color.red);
this.add(cs);
//Resise
resize = new JLabel("re");
resize.setBounds( (width-closeDim), (height-closeDim) , closeDim, closeDim);
resize.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
this.add(resize);
MouseInputAdapter resizeMouseAdapter = new ResizeMouseHandler();
resize.addMouseMotionListener(resizeMouseAdapter);
resize.addMouseListener(resizeMouseAdapter);
}
private class HeaderMouseHandler extends MouseInputAdapter {
public void mouseDragged(final MouseEvent e) {
Point pointDragged = e.getPoint();
Point loc = getLocation();
loc.translate(pointDragged.x - pointPressed.x,
pointDragged.y - pointPressed.y);
setLocation(loc);
}
public void mousePressed(final MouseEvent e) {
pointPressed = e.getPoint();
}
}
private class ResizeMouseHandler extends MouseInputAdapter {
public void mouseDragged(final MouseEvent e) {
Point pointDragged = e.getPoint();
int w = width1 + pointDragged.x;
int h = height1 + pointDragged.y;
setSize(w , h);
cs.setSize(w, (h-headerHeight) );
header.setSize(w -(closeDim + 2), headerHeight);
//Point loc = getLocation();
//loc.translate(pointDragged.x - pointPressed.x,
//pointDragged.y - pointPressed.y);
//System.out.println("p: "+Integer.toString((pointDragged.x - pointPressed.x)) + " / " + Integer.toString((pointDragged.y - pointPressed.y)));
//System.out.println("n: "+Integer.toString(w) + " / " + Integer.toString(h));
//System.out.println("o: "+Integer.toString(width1) + " / " + Integer.toString(height1));
Point resizeloc = new Point();
resizeloc.translate(w-closeDim, h-closeDim);
resize.setLocation(resizeloc);
//Point closeloc = new Point();
//closeloc.translate((w - (closeDim + 2)) , 2 );
//close.setLocation(closeloc);
revalidate();
repaint();
}
public void mousePressed(final MouseEvent e) {
pointPressed = e.getPoint();
}
}
}
答案 0 :(得分:0)
现在解决了。问题是面板的尺寸(我对标签新位置的引用)不是变量。
int w = width1 + pointDragged.x;
int h = height1 + pointDragged.y;