java tips
  ApTxpH0CpnLS 2023年11月13日 23 0


1 当前路径:
System.getProperty("user.dir");
启示:注意System的属性。
2 更多路径问题:
http://www.blogjava.net/ericwang/archive/2005/12/16/24301.aspx

3 Vector.toArray() 的使用和Object[]

 boolean

add(E

          Appends the specified element to the end of this Vector.

toArray(T[] a)

          Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.

<T> T[]


import org.htmlparser.NodeFilter; 
   

Vector 
   < 
   NodeFilter 
   > 
    filters  
   = 
     
   new 
    Vector();
filters.add( NodeFilter(aObject));
filters.add( NodeFilter(bObject));
..
NodeFilter[] reFilters  
   = 
     
   new 
    NodeFilter[ filters.size()];
reFilters  
   = 
    filters.toArray(reFilters);

下面的代码,不能实现Object[]到NodeList[]的转换,原因待考,

似乎Object[]比较特别。

java tips_import

import     java.util.Vector;

    public 
     
   class 
    TestObjectArrayCast ... 
   ... 
   {

    /** *//** *//** *//**
     * @param args
     */
    public static void main(String[] args) ......{
        // TODO Auto-generated method stub

        Vector v = new Vector();
        v.add("1");
        v.add("2");
             //toArray()返回Object[]
        String[] c =(String[]) v.toArray();//java.lang.ClassCastException
        System.out.println("c:"+c);

    }

}

4 文件读取基本技巧

http://www.roseindia.net/java/example/java/io/java-read-file-line-by-line.shtml

5 javabean的property

http://mindprod.com/jgloss/properties.html#JAVABEAN

JavaBean Properties

Property has a second meaning. In JavaBeans, components have associated persistent objects. You can modify various fields in those objects to configure them. The accessible fields of a JavaBean are called properties . They are accessible via public get/set methods. There need not be an actual field, just the get/set methods that simulate one. These are a completely separate mechanism.

6 JTree中使用弹出菜单

http://www.java-tips.org/java-se-tips/javax.swing/have-a-popup-attached-to-a-jtree.html

import 
    javax.swing. 
   * 
   ;
 
   import 
    javax.swing.tree. 
   * 
   ;
 
   import 
    java.awt.event. 
   * 
   ;
 
   import 
    java.awt. 
   * 
   ;
 
   import 
    java.util. 
   * 
   ;

 
   public 
     
   class 
    TreeWithPopup  
   extends 
    JPanel  
   ... 
   {
    
    DefaultMutableTreeNode root, node1, node2, node3;
    
    public TreeWithPopup() ...{
        MyJTree tree;
        root = new DefaultMutableTreeNode("root", true);
        node1 = new DefaultMutableTreeNode("node 1", true);
        node2 = new DefaultMutableTreeNode("node 2" , true);
        node3 = new DefaultMutableTreeNode("node 3", true);
        root.add(node1);
        node1.add(node2);
        root.add(node3);
        setLayout(new BorderLayout());
        tree = new MyJTree(root);
        add(new JScrollPane((JTree)tree),"Center");
    }
    
    public Dimension getPreferredSize()...{
        return new Dimension(300, 300);
    }
    
    public static void main(String s[])...{
        JFrame frame = new JFrame("Tree With Popup");
        TreeWithPopup panel = new TreeWithPopup();
        
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setForeground(Color.black);
        frame.setBackground(Color.lightGray);
        frame.getContentPane().add(panel,"Center");
        
        frame.setSize(panel.getPreferredSize());
        frame.setVisible(true);
        frame.addWindowListener(new WindowCloser());
    }
} 
   

 
   class 
    WindowCloser  
   extends 
    WindowAdapter  
   ... 
   {
    public void windowClosing(WindowEvent e) ...{
        Window win = e.getWindow();
        win.setVisible(false);
        System.exit(0);
    }
} 
   

 
   class 
    MyJTree  
   extends 
    JTree  
   implements 
    ActionListener 
   ... 
   {
    JPopupMenu popup;
    JMenuItem mi;
    
    MyJTree(DefaultMutableTreeNode dmtn) ...{
        super(dmtn);
        // define the popup
        popup = new JPopupMenu();
        mi = new JMenuItem("Insert a children");
        mi.addActionListener(this);
        mi.setActionCommand("insert");
        popup.add(mi);
        mi = new JMenuItem("Remove this node");
        mi.addActionListener(this);
        mi.setActionCommand("remove");
        popup.add(mi);
        popup.setOpaque(true);
        popup.setLightWeightPopupEnabled(true);
        
        addMouseListener(
                new MouseAdapter() ...{
            public void mouseReleased( MouseEvent e ) ...{
                if ( e.isPopupTrigger()) ...{
                    popup.show( (JComponent)e.getSource(), e.getX(), e.getY() );
                }
            }
        }
        );
        
    }
    public void actionPerformed(ActionEvent ae) ...{
        DefaultMutableTreeNode dmtn, node;
        
        TreePath path = this.getSelectionPath();
        dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
        if (ae.getActionCommand().equals("insert")) ...{
            node = new DefaultMutableTreeNode("children");
            dmtn.add(node);
            // thanks to Yong Zhang for the tip for refreshing the tree structure.
            ((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);
        }
        if (ae.getActionCommand().equals("remove")) ...{
            node = (DefaultMutableTreeNode)dmtn.getParent();
            // Bug fix by essam
            int nodeIndex=node.getIndex(dmtn); // declare an integer to hold the selected nodes index
            dmtn.removeAllChildren();          // remove any children of selected node
            node.remove(nodeIndex);            // remove the selected node, retain its siblings
            ((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);       }
    }
}

7 JFrame.getFrames()静态方法

返回应用程序创建的Frame对象数组,对于Applet返回该applet可以访问的Frame数组。

8 让Hashtable具有排序的能力

How to sort a Java Hashtable.

http://www.basis.com/support/kb/kb01074.html



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  D04qQxYJZ4A6   2023年11月19日   24   0   0 mysqljava读写分离
  bYdMetjCLs2g   2023年11月19日   23   0   0 tomcatjava
  UP4ONKOBnkdD   2023年11月28日   22   0   0 java
  9JCEeX0Eg8g4   2023年12月10日   30   0   0 应用程序javaApache
  P3nxyT0LRuwj   2023年11月19日   26   0   0 javawar包jar
  KRsXEGSB49bk   2023年11月27日   28   0   0 javaApache
  xwGmYGXf1w4S   2023年11月22日   42   0   0 tomcatjavaApache