没有边框可以使用setUndecorated(true)来实现,可是这样以后就没有办法拖动边界了,怎样才能既没有边框,又能拖动边界?

解决方案 »

  1.   

    你既然要拖大小,何必没有边框呢,我一般用到没边框的都是些类似于软件开头的LOGO,或者一些只有关闭按纽的窗口,如软件的About
      

  2.   

    我要实现的是类似于excel批注的功能,要求没有边框,又要拖动大小
      

  3.   

    look this:import java.awt.*;
    import java.awt.event.*;public class FrameTest {
      static Point origin = new Point();
      public static void main (String args[]) {
        final Frame frame = new Frame();
        frame.setUndecorated(true);
        frame.addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            origin.x = e.getX();
            origin.y = e.getY();
          }
        });
        frame.addMouseMotionListener(new MouseMotionAdapter() {
          public void mouseDragged(MouseEvent e) {
            Point p = frame.getLocation();
            frame.setLocation(
              p.x + e.getX() - origin.x, 
              p.y + e.getY() - origin.y);
          }
        });
        frame.setSize(300, 300);
        Button b1 = new Button("Maximize");
        b1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
          }
        });
        Button b2 = new Button("Iconify");
        b2.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // Preserve maximizing
            frame.setExtendedState(Frame.ICONIFIED 
               | frame.getExtendedState());
          }
        });
        Button b3 = new Button("Normal");
        b3.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            frame.setExtendedState(Frame.NORMAL);
          }
        });
        Button b4 = new Button("Close");
        b4.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.exit(0);
          }
        });
        frame.setLayout(new GridLayout(5,1));
        frame.add(b1);
        frame.add(b2);
        frame.add(b3);
        frame.add(b4);
        frame.show();
      }
    }
      

  4.   

    dialog.setUndecorated(UIManager.getLookAndFeel().getSupportsWindowDecorations());
            JDialog.setDefaultLookAndFeelDecorated(laf.getSupportsWindowDecorations());
      

  5.   

    楼上的兄弟,laf变量是自己建立的吗?
    具体怎样实现,还请详细说明!谢谢
      

  6.   

    请看我的blog里的:[原创]Swing技巧8:完美的LookAndFeel解决方案 http://blog.csdn.net/dyhml/archive/2005/02/17/291182.aspx
      

  7.   

    frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    这种方法能实现dialog没有头,但是其rootpane仍然有头。
    我只需拖动大小,不需拖动位置,所以不要头部,怎样实现?
      

  8.   

    鼠标监听器的部分是从javax.swing.plaf.metal.MetalRootPaneUI类里面抄来的, 为了缩短长度把注释删掉了import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.security.PrivilegedExceptionAction;import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.event.MouseInputListener;public class ResizableNoTitleDialog
    extends JDialog
    {
    public ResizableNoTitleDialog(Dialog owner, boolean modal) throws HeadlessException
    {
    super(owner, modal);
    initDialog();
    } public ResizableNoTitleDialog(Frame owner, boolean modal) throws HeadlessException
    {
    super(owner, modal);
    initDialog();
    } private void initDialog()
    {
    setUndecorated(true); MouseInputListener listener = new MouseInputHandler();
    addMouseListener(listener);
    addMouseMotionListener(listener); getRootPane().setBorder(createBorder());
    } protected Border createBorder()
    {
    return BorderFactory.createCompoundBorder(
    BorderFactory.createRaisedBevelBorder(), 
    BorderFactory.createEmptyBorder(5, 5, 5, 5));
    }
    private static final int CORNER_DRAG_WIDTH = 16;
    private static final int BORDER_DRAG_THICKNESS = 5; private static final int[] cursorMapping = new int[] { Cursor.NW_RESIZE_CURSOR, Cursor.NW_RESIZE_CURSOR,
    Cursor.N_RESIZE_CURSOR, Cursor.NE_RESIZE_CURSOR, Cursor.NE_RESIZE_CURSOR, Cursor.NW_RESIZE_CURSOR, 0, 0, 0,
    Cursor.NE_RESIZE_CURSOR, Cursor.W_RESIZE_CURSOR, 0, 0, 0, Cursor.E_RESIZE_CURSOR, Cursor.SW_RESIZE_CURSOR, 0, 0,
    0, Cursor.SE_RESIZE_CURSOR, Cursor.SW_RESIZE_CURSOR, Cursor.SW_RESIZE_CURSOR, Cursor.S_RESIZE_CURSOR,
    Cursor.SE_RESIZE_CURSOR, Cursor.SE_RESIZE_CURSOR }; private class MouseInputHandler
    implements MouseInputListener
    {
    private int dragCursor; private int dragOffsetX; private int dragOffsetY; private int dragWidth; private int dragHeight; private Cursor lastCursor; private final PrivilegedExceptionAction getLocationAction = new PrivilegedExceptionAction()
    {
    public Object run() throws HeadlessException
    {
    return MouseInfo.getPointerInfo().getLocation();
    }
    }; public void mousePressed(MouseEvent ev)
    {
    Point dragWindowOffset = ev.getPoint(); dragOffsetX = dragWindowOffset.x;
    dragOffsetY = dragWindowOffset.y;
    dragWidth = getWidth();
    dragHeight = getHeight();
    dragCursor = getCursor(calculateCorner(dragWindowOffset.x, dragWindowOffset.y));
    } public void mouseReleased(MouseEvent ev)
    {
    if (dragCursor != 0 && !isValid()) {
    validate();
    getRootPane().repaint();
    } dragCursor = 0;
    } public void mouseMoved(MouseEvent ev)
    {
    int cursor = getCursor(calculateCorner(ev.getX(), ev.getY())); if (cursor != 0) {
    setCursor(Cursor.getPredefinedCursor(cursor));
    }
    else {
    setCursor(lastCursor);
    }
    } private void adjust(Rectangle bounds, Dimension min, int deltaX, int deltaY, int deltaWidth, int deltaHeight)
    {
    bounds.x += deltaX;
    bounds.y += deltaY;
    bounds.width += deltaWidth;
    bounds.height += deltaHeight;
    if (min != null) {
    if (bounds.width < min.width) {
    int correction = min.width - bounds.width;
    if (deltaX != 0) {
    bounds.x -= correction;
    }
    bounds.width = min.width;
    }
    if (bounds.height < min.height) {
    int correction = min.height - bounds.height;
    if (deltaY != 0) {
    bounds.y -= correction;
    }
    bounds.height = min.height;
    }
    }
    if (dragCursor != 0 && !isValid()) {
    validate();
    getRootPane().repaint();
    }
    } public void mouseDragged(MouseEvent ev)
    {
    Window w = (Window) ev.getSource();
    Point pt = ev.getPoint(); if (dragCursor != 0) {
    Rectangle r = w.getBounds();
    Rectangle startBounds = new Rectangle(r);
    Dimension min = w.getMinimumSize(); switch (dragCursor) {
    case Cursor.E_RESIZE_CURSOR:
    adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
    break;
    case Cursor.S_RESIZE_CURSOR:
    adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
    break;
    case Cursor.N_RESIZE_CURSOR:
    adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
    break;
    case Cursor.W_RESIZE_CURSOR:
    adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
    break;
    case Cursor.NE_RESIZE_CURSOR:
    adjust(r, min, 0, pt.y - dragOffsetY, pt.x + (dragWidth - dragOffsetX) - r.width, -(pt.y - dragOffsetY));
    break;
    case Cursor.SE_RESIZE_CURSOR:
    adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, pt.y + (dragHeight - dragOffsetY)
    - r.height);
    break;
    case Cursor.NW_RESIZE_CURSOR:
    adjust(r, min, pt.x - dragOffsetX, pt.y - dragOffsetY, -(pt.x - dragOffsetX), -(pt.y - dragOffsetY));
    break;
    case Cursor.SW_RESIZE_CURSOR:
    adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), pt.y + (dragHeight - dragOffsetY) - r.height);
    break;
    default:
    break;
    }
    if (!r.equals(startBounds)) {
    w.setBounds(r);
    if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
    w.validate();
    getRootPane().repaint();
    }
    }
    }
    } public void mouseEntered(MouseEvent ev)
    {
    Window w = (Window) ev.getSource();
    lastCursor = w.getCursor();
    mouseMoved(ev);
    } public void mouseExited(MouseEvent ev)
    {
    Window w = (Window) ev.getSource();
    w.setCursor(lastCursor);
    } public void mouseClicked(MouseEvent ev)
    {
    } private int calculateCorner(int x, int y)
    {
    Insets insets = getInsets();
    int xPosition = calculatePosition(x - insets.left, getWidth() - insets.left - insets.right);
    int yPosition = calculatePosition(y - insets.top, getHeight() - insets.top - insets.bottom); if (xPosition == -1 || yPosition == -1) {
    return -1;
    }
    return yPosition * 5 + xPosition;
    } private int getCursor(int corner)
    {
    if (corner == -1) {
    return 0;
    }
    return cursorMapping[corner];
    } private int calculatePosition(int spot, int width)
    {
    if (spot < BORDER_DRAG_THICKNESS) {
    return 0;
    }
    if (spot < CORNER_DRAG_WIDTH) {
    return 1;
    }
    if (spot >= (width - BORDER_DRAG_THICKNESS)) {
    return 4;
    }
    if (spot >= (width - CORNER_DRAG_WIDTH)) {
    return 3;
    }
    return 2;
    }
    } public static void main(String[] args) throws Exception
    {
    System.setProperty("sun.awt.noerasebackground", "true");
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); JScrollPane sp = new JScrollPane(new JTree()); JFrame f = new JFrame();
    f.setSize(500, 500);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true); JDialog d = new ResizableNoTitleDialog(f, false);
    d.getContentPane().add(sp, BorderLayout.CENTER);
    d.setSize(200, 200);
    d.setLocationRelativeTo(f);
    d.setVisible(true);
    }
    }