****************************************************************************************************** 讓程序啟動時關聯文件,在main函數中寫以下函數 File audioFile = new File(args[0]);//把入口類main函數的args傳遞給一個文件 SimpleAudioPlayer player = new SimpleAudioPlayer(audioFile);//把文件調用
****************************************************************************************** 輸入提示框 String s=JOptionPane.showInputDialog(null,"查找","請輸入字符串"); ********************************************************************************************** new ServerSocket(8080, 1, InetAddress.getByName("127.0.0.1"));將socket綁定到某個地址 SocketAddress s=new InetSocketAddress("219.136.215.246",56610); ********************************************************************************** 在任意的數據庫查詢語句中輸入"sp_databases",可以得到sqlserver2000數據庫的數據庫結果集 ************************************************************************************* JEDITORPANE 獲得網頁顯示出來的文字 try{ System.out.println(jEditorPane1.getDocument().getText(0,jEditorPane1.getDocument().getLength())); }catch(Exception r){} ************************************************************************************* 設置光標位置 jEditorPane1.setCaretPosition(rr); rr為int型 ************************************************************************************* 監聽鍵盤組合按下 if(e.getKeyCode()==KeyEvent.VK_C&&e.isControlDown()){ System.out.println("hello"); } ***************************************************************************************** 在一個類內可以設置系統屬性名和值可在類中讀取 System.setProperty("hello","123"); System.out.println(System.getProperty("hello")); ************************************************************************ java更改應用程序圖標的辦法 利用ResScope打開程序資源 選中程序圖標 然后導入 32*32*256 的圖標代替原圖標就可以了 ******************************************************************** 可以使得滾動條自動置于底部 jta.setCaretPosition(jta.getText().length()); *********************************************************************** 例子:用Dos定位到目錄下并選中文件 explorer /n, /root, c:/windows/system32,/select, c:/windows/system32/notepad.exe ******************************************************************* 使用dos打開一個文件直接輸入文件路徑就可以了 ************************************************************** 讓滾動條滾到自動最后記錄 yourJScrollPane.getVerticalScrollBar().setValue(999999); **************************************************************************************** Jsp分頁如下: <%@ page language="java" import="java.util.*,java.sql.*" %> <%@ page contentType="text/html;charset=gb2312"%> <jsp:useBean id="cn" scope="page" class="myConnection.Conn" /><!--引用數據庫操作的bean,自己完成,這里不再贅述--> <% int curpage=1;//當前頁 int page_record=20;//每頁顯示的記錄數 //用下面的方法(sql查詢完成,速度快) curpage=Integer.parseInt(request.getParameter("page"));//獲取傳遞的值,需要顯示的頁 ResultSet rs=cn.rsexecuteQuery("select top "+page_record+" * from tablename where id not in (select top "+(curpage*page_record)+" id from tablename order by id desc) order by id desc"); //本查詢語句得到的是所要顯示的1000頁的20條記錄,大致思路為??子查詢排除需要顯示的記錄前的所有記錄,父查詢則對余下的記錄進行降序排列 while(rs.next) { out.println(rs.getInt("id").toString()); } rs.close(); %> *************************************************************************************** 刪除D盤所有的_desktop.ini文件的Dos命令 del D:/_desktop.ini /f/s/q/a" ********************************************************************************* 求數組最大值 int[] array={3,5,9,4,8}; int maxNum=array[0]; for(int i=1;i<array.length;i++) { maxNum=java.lang.Math.max(array[i],maxNum); } System.out.println(maxNum); ************************************************************************************* 我的方法求最小值 int[] s={54,54,68,47,58,32,17,56,6,654}; int t=0; for(int i=0;i<s.length-1;i++){ t=(s[i]<=s[i+1])?s[i]:s[i+1]; s[i+1]=t; } System.out.println(t); ****************************************************************************** 設置窗體圖標 frame.setIconImage(Toolkit.getDefaultToolkit().createImage("h:/h1.png")); ********************************************************************************** 獲取一個文件的大文件圖標 This example uses an unsupported class - - sun.awt.shell.ShellFolder - - and therefore will not work in all virtual machines. // Create a File instance of the file File file = new File("filename.txt");
try { sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file);
// Get large icon Icon icon = new ImageIcon(sf.getIcon(true), sf.getFolderType()); } catch (FileNotFoundException e) { } ****************************************************************************** 去除一個組件 jPanel1.remove(jLabel1); jPanel1.repaint(); ******************************************************************************* 使用Frame的.pack();函數時,如果不想改變當前窗體大小 底層的窗格調用contentPane.setPreferredSize(contentPane.getSize());