日期:2014-05-20  浏览次数:20727 次

Swing实现鼠标动态调整面板大小
现有一个JPanel中包含两个JScrollPanel,希望通过鼠标拖拽动态地调整两个JScrollPanel的大小,已经实现了鼠标调整功能,但是鼠标响应不是很灵敏,需要拖拽多次才能成功,而且光标经常改变了之后变不回来。请问有什么方法提高界面响应的准确度与灵敏度?十分感谢!

Java code
import javax.swing.JFrame;
import java.awt.Cursor;
import java.awt.Dimension;
/**
 * <p>MouseAdjustFrame.java</p>
 * <p>Created on May 6, 2009, 2:02:45 PM</p>
 * <p>Copyright (c) 2007-2009. CUCKOO Workgroup, USTC, P.R.China</p>
 * @author Ren Jian
 */
public class MouseAdjustFrame extends JFrame {
    private boolean resizeable = false;
    /** Creates new form MouseAdjustFrame */
    public MouseAdjustFrame() {
        this.setPreferredSize(new java.awt.Dimension(640, 480));
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        mainPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mouse Adjust");
        mainPanel.setLayout(new java.awt.BorderLayout());
        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Panel2"));
        mainPanel.add(jPanel2, java.awt.BorderLayout.CENTER);
        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Panel1"));
        jScrollPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jScrollPane1MousePressed(evt);
            }
        });
        jScrollPane1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jScrollPane1MouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jScrollPane1MouseMoved(evt);
            }
        });
        jList1.setBackground(new java.awt.Color(255, 255, 204));
        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);
        mainPanel.add(jScrollPane1, java.awt.BorderLayout.NORTH);
        getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
        pack();
    }// </editor-fold>
    private void jScrollPane1MousePressed(java.awt.event.MouseEvent evt) {                                          
        int height = evt.getY() - jScrollPane1.getY();
        if ((height < jScrollPane1.getHeight() + 3) && (height > jScrollPane1.getHeight() - 3)) {
            resizeable = true;
        } else {
            resizeable = false;
        }
    }                                         
    private void jScrollPane1MouseMoved(java.awt.event.MouseEvent evt) {                                        
        int height = evt.getY() - jScrollPane1.getY();
        if ((height < jScrollPane1.getHeight() + 3) && (height > jScrollPane1.getHeight() - 3)) {
            this.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
        } else {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }                                       
    private void jScrollPane1MouseDragged(java.awt.event.MouseEvent evt) {                                          
        if (resizeable) {
            jScrollPane1.setSize(new Dimension(jScrollPane1.getWidth(), evt.getY() - jScrollPane1.getY()));
            jScrollPane1.validate();
            jPanel2.setLocation(mainPanel.getX(), evt.getY());
            jPanel2.setSize(new Dimension(jPanel2.getWidth(), mainPanel.getHeight() - evt.getY()));
        }
    }                                         
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MouseAdjustFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration
}