1.避免使用float 或是double 作需要準確結果的運算,像是金錢商業方面
2.使用BigDecimal, int, long代替
3.使用new BigDecimal(String),而不是new BigDecimal(double)
4.BigDecimal.valueOf(double)不錯,但不是最好的;對非常數使用
20111117
引用jar 內的檔案資源
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<applet code="demogame.GameRun.class" archive="JAppletDemo_v0.01.jar" width=800 height=600></applet>
</body>
</html>
假設JAppletDemo_v0.01.jar 包中,程序進入點為demogame 包中的GameRun.class ,且要取得block_a.png 的圖檔,並且圖檔並沒有被放入任何package中,使用
getClass().getResource("/block_a.png")
來取得資源,注意路徑要寫正確。
Applet路徑方面
getCodeBase()
無法取得package裡的資源,返回Applet Class所在目錄的路徑。在HTML文件的"Applet"標籤中的CODEBASE屬性值計算出來,若該屬性沒有設置則返回該HTML文件所在目錄。
getDocumentBase()
返回包含Applet的HTML文件所在目錄。
很難懂。
20111022
DataInput.readUTF與writeUTF
許多的InputStream 與OutputStream 的子類實作了DataInput 介面,其中有讀寫UTF8字串的方法,
DataInput.readUTF() 與writeUTF() 方法。用位元組方式取得這些串流時,這些串流的位元組數量不是剛好等於2Byte * 字數,而是2Byte+ 2Byte * 字數,多出來的2Byte位元組是用來表示UTF字串長度的,將換算整數會得到字串長度。
DataInput.readUTF() 與writeUTF() 方法。用位元組方式取得這些串流時,這些串流的位元組數量不是剛好等於2Byte * 字數,而是2Byte+ 2Byte * 字數,多出來的2Byte位元組是用來表示UTF字串長度的,將換算整數會得到字串長度。
20111021
eclipse java 中文亂碼問題
環境:win7
IDE:eclipse Helios Service Release 2
Console預設字集: UTF-8
串流輸入預設編碼: UTF8
串流輸出預設編碼: UTF8
問題 :console所輸入的中文字串,無法正確的顯示回console,尚未使用CMD測試
1. 外部已編輯好的UTF8編碼文字檔,可以正確讀出顯示在console
2. 程式內給予初值的字串,可以正確顯示
3. console取得的字串,無法正確的存入外部文字檔,文字檔內容為亂碼
4. 以MS950編碼取得亂碼的位元組,在強制編成UTF8,部分文字可正確顯示
IDE:eclipse Helios Service Release 2
Console預設字集: UTF-8
串流輸入預設編碼: UTF8
串流輸出預設編碼: UTF8
問題 :console所輸入的中文字串,無法正確的顯示回console,尚未使用CMD測試
1. 外部已編輯好的UTF8編碼文字檔,可以正確讀出顯示在console
2. 程式內給予初值的字串,可以正確顯示
3. console取得的字串,無法正確的存入外部文字檔,文字檔內容為亂碼
4. 以MS950編碼取得亂碼的位元組,在強制編成UTF8,部分文字可正確顯示
System.out.println("系統預設編碼: "+Charset.defaultCharset());
BufferedReader reader=null;
InputStreamReader stream =new InputStreamReader(System.in);
reader = new BufferedReader(stream);
System.out.println("串流預設編碼: "+stream.getEncoding());
byte[] binary=null;
String str =null;
System.out.println(str = reader.readLine());
reader.close();
binary = str.getBytes("UTF-16");
System.out.println(new String(binary,"UTF-16"));
System.out.printf("UTF-16: ") ;
for(byte b:binary){
System.out.print(Integer.toHexString(b & 0x00ff) + " ");
}
System.out.println();
binary = str.getBytes("UTF-8");
System.out.println(new String(binary,"UTF-8"));
System.out.printf("UTF-8: ");
for(byte b:binary){
System.out.print(Integer.toHexString(b & 0x00ff) + " ");
}
System.out.println();
binary = str.getBytes("MS950");
System.out.println(new String(binary,"MS950"));
System.out.printf("MS950: ");
for(byte b:binary){
System.out.print(Integer.toHexString(b & 0x00ff) + " ");
}
//某些字串可以用ms950編碼正確顯示
System.out.println();
binary = str.getBytes("MS950");
System.out.println(new String(binary,"UTF8"));
for(byte b:binary){
System.out.print(Integer.toHexString(b & 0x00ff) + " ");
}
System.out.println();
//用全部編碼比對
for (String encoding : Charset.availableCharsets().keySet()) {
try{
binary = str.getBytes(encoding);
System.out.printf("%s %s%n",
encoding, new String(binary, "utf8"));
}catch (Exception e) {
continue;
}
}
20111020
Java 關於聲音
java sound
關於聲音
聲音檔案如何計算?
frame size是 取樣位元的大小 通常有16bit 8bit
聲道數是指 單聲、雙聲、五點一之類的
Java取得聲音串流的方法
AudioSystem類提供多種方式取得串流
以下兩種取得聲音串流AudioInputStream 物件。
關於聲音
聲音檔案如何計算?
frames * frame size * 聲道數frames指 取樣頻率44100Hz下,一秒中44100個frames
frame size是 取樣位元的大小 通常有16bit 8bit
聲道數是指 單聲、雙聲、五點一之類的
Java取得聲音串流的方法
AudioSystem類提供多種方式取得串流
以下兩種取得聲音串流AudioInputStream 物件。
AudioSystem.getAudioInputStream(InputStream is);
Audiosystem.getAudioInputStream(File file);
取得聲音串流後,可以取得其頁框長度,也可以取得其Format物件,利用Format物件來得到串流的取樣頻率、取樣位元、頁框大小,利用頁框大小與頁框長度可以算出檔案大小 //取得聲音串流的頁框長度
ais.getFrameLength();
//取得AudioFormat物件
ais.getFormat();
//取得取樣頻率
format.getSampleRate();
//取得頁框大小。回傳Byte
format.getFrameSize();
20111019
swing中的pane
任何頂級容器(JFram, JDailog, JWindow, JApplet, JInterFrame)包含JRootPane,而JRootPane包含一對同級的Glasspane和JLayeredpane。JLayeredpane又包含JContentPane與一個可選的JMenu。
在swing中所有的加入組件都會轉發到ContentPane中,所以實際上Container.add(component)是相同於Container.getContentPane.add(component)。
如何取得ContentPane?
以下三者可以得知平常加入組件的寫法其實是JVM幫忙轉發的
PS.Container.getRootPane().getLayeredPane().getComponent(0)是回傳Component,要記的轉型
在swing中所有的加入組件都會轉發到ContentPane中,所以實際上Container.add(component)是相同於Container.getContentPane.add(component)。
如何取得ContentPane?
以下三者可以得知平常加入組件的寫法其實是JVM幫忙轉發的
Container.getContentPane();
Container.getRootPane().getContentPane();
Container.getRootPane().getLayeredPane().getComponent(0);
System.out.println((Container.getRootPane().getLayeredPane().getComponent(0)==jf.getContentPane())? "true" : "fasle" );
回傳TRUE。PS.Container.getRootPane().getLayeredPane().getComponent(0)是回傳Component,要記的轉型
20111008
ImageIcon 無法取得圖片
首先,確認該負責載入圖形檔案的class檔位置
再來,確認被載入的圖形檔案的位置
最後,依照該class檔來規劃相對路徑取得目標檔案
在使用 ImageIcon 載入圖片時,如果java source使用了package管理,會無法取得用相對路徑規劃的圖形,此時可以用以下方法取得圖形
再來,確認被載入的圖形檔案的位置
最後,依照該class檔來規劃相對路徑取得目標檔案
在使用 ImageIcon 載入圖片時,如果java source使用了package管理,會無法取得用相對路徑規劃的圖形,此時可以用以下方法取得圖形
getClass().getResource("source path");
/*java source 在allsrc/ch02src底下
* 此時要取得allsrc.ch02src.images下的圖形時可以參照下列方法
*/
package allsrc.ch02src;
bgImage = loadImage(this.getClass().getResource("images/background.jpg"));
20111005
如何在執行編譯前自動存檔?
原本eclipse能夠在編譯前自動儲存修改過的檔案,在安裝oracle EE plugin 其中的設定似乎被改過了。更改回來的方法為 preferenece > run/debug > launching
20111003
Eclipse 使用 GlassFish V3.11 環境
file > new > other > server
Download add server adapter
選擇 oracle glassfish server tools > next > install > restrat eclipse
重開後設定 新增server 並設定資訊(localhost、server name)
設定 glassfish 的安裝位置,選擇installed JRE preferences
add >
glassfish使用JDK運作而不是JRE
20111001
20110917
為何在swing事件處理中 repaint 無法被調用?
1. public void actionPerformed( ActionEvent e ){
2.
3. panel.repaint();
4.
5. for( int i = 0; i < 200; i++){
6.
7. x += 50;
8. y += 50;
9.
10. panel.repaint(); //無法被調用
11.
12. try{
13.
14. Thread.currentThread().sleep( 50 );
15.
16. }// close try
17. catch( Exception ex ){
18. }// close catch
19.
20. }//close for
21.
22. }//close actionPerformed
Why does it works outside the actionPerformed and doesn't inside??
The event dispatch thread does one thing at a time.
Try starting the animation on another thread.
請開新Thread處理重繪。
因為"事件處理" 與 "重繪"都會使用EDT去完成。
每次發生"事件處理" 與 "重繪處裡"時,會將事件與重繪(兩者都是非同步處理,會立即返回!)排列到EDT的Queue當中等待EDT照順序處裡。
試想,當"事件處理"非常耗時冗長(例如長時間的迴圈或是遠端資料讀取),且"事件處理"包含著repaint請求(也就是"重繪處理"),會使GUI失去回應,且不斷的堆疊repaint在EDT的queue裡,因為EDT還在處理那段冗長的事件!根本沒空處理重繪,所以GUI整個失效。
1. public void actionPerformed( ActionEvent e ){
2. // if we are not now animating
3. animate();
4. }
5.
6. private void animate() {
7. Thread thread = new Thread(new Runnable() {
8. public void run() {
9. for( int i = 0; i < 200; i++){
10. x += 50;
11. y += 50;
12. panel.repaint();
13. try{
14. Thread.currentThread().sleep( 50 );
15. } catch( Exception ex ){
16. break;
17. }
18. }
19. }
20. thread.setPriority(Thread.NORM_PRIORITY);
21. thread.start();
22. }
swing 執行緒簡介更多有關EDT
20110902
getGraphics() 回傳 NULL
要在APPLET(init階段)裡取得Jpanel的graphics時遇到return null的問題,google後發現似乎並不能隨時取得container裡componen的graphic,至少在container為不可見時後,container裡的componet都無法取得graphics,但有別的方法可以在不可見或初始階段就先寫好繪圖。
(但其實仍是在容器可見時才執行)
(但其實仍是在容器可見時才執行)
//失敗(JApplet init) jp.getGraphics() 為null
JPanel jp=new JPanel();
jp.getGraphics().drawString("123",20,20);
//可取得
JPanel jp=new JPanel(){
public void paintComponent(Graphics g){
g.drawString("21113", 10, 200);
}
};
//在事件中也可取得
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
Graphics2D g2 =(Graphics2D) panel.getGraphics();
g2.drawRect(20,20,100,300);
}
});
20110827
利用Graphics 生成函透明背景的Image
一般的情況下Image雖然會保有透明資訊,但是經過Graphics.drawImage得到Image卻會使來源Image失去透明資訊,透過以下方法可以保留來源圖的透明資訊。這樣的好處是可以把此Image當成其他Graphics的來原圖
int width = 400;
int height = 300;
// 創建BufferedImage對象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取Graphics2D
Graphics2D g2d = image.createGraphics();
// ---------- 增加下面的代碼使得背景透明 -----------------
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
// ---------- 背景透明代碼結束 -----------------
// 畫圖
g2d.setColor(new Color(255,0,0));
g2d.setStroke(new BasicStroke(1));
g2d.draw
//釋放對象
g2d.dispose();
// 保存文件
ImageIO.write(image, "png", new File("c:/test.png"));
20110823
Graphics.drawImage()的問題
透過Graphics.drawImage()可用來繪製圖片在Image物件上,包含透明色資訊的圖檔也可以,但是透明色像素會影響前一層Graphics繪製的背景
地圖與人物分別用不同的Graphics gMapScreen,gCharactorScreen畫出來再分別放進緩衝區Graphics gOffScreen裡,先放的Image MapScreen被後放的Image CharactorScreen的透明色穿過。
解決辦法:將人物與背景用同一個Graphics繪出,地圖就會正常顯示了
地圖與人物分別用不同的Graphics gMapScreen,gCharactorScreen畫出來再分別放進緩衝區Graphics gOffScreen裡,先放的Image MapScreen被後放的Image CharactorScreen的透明色穿過。
解決辦法:將人物與背景用同一個Graphics繪出,地圖就會正常顯示了
20110720
session失效
js中使用window.location.href來指定頁面的跳轉,會被認為是一個新的html 請求,因此會清空session
window.location與document.URL的不同是一個取得物件一個取得唯獨的字串
解決辦法
From what I remember, the only way to do it would be by doing something like
window.location與document.URL的不同是一個取得物件一個取得唯獨的字串
解決辦法
From what I remember, the only way to do it would be by doing something like
window.location.href='page.php?<?php echo session_name().'='.session_id();?>'
20110703
$_FILES Error Messages Explained
- UPLOAD_ERR_OK
- Value: 0; There is no error, the file uploaded with success.
- UPLOAD_ERR_INI_SIZE
- Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
- UPLOAD_ERR_FORM_SIZE
- Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
- UPLOAD_ERR_PARTIAL
- Value: 3; The uploaded file was only partially uploaded.
- UPLOAD_ERR_NO_FILE
- Value: 4; No file was uploaded.
- UPLOAD_ERR_NO_TMP_DIR
- Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
- UPLOAD_ERR_CANT_WRITE
- Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
20110430
win7下 命令列啟動關閉mysql
啟動>
以系統管理員執行CMD,後執行 mysqld。
註:cmd下以系統管理員執行net start window service name才是正確的,直接執行,似乎是運行一個新的服務?無法登入SQL伺服器。
關閉>
任何權限都可以關閉mysql服務,cmd下執行,mysqladmin -u -p shutdown。
以系統管理員執行CMD,後執行 mysqld。
註:cmd下以系統管理員執行net start window service name才是正確的,直接執行,似乎是運行一個新的服務?無法登入SQL伺服器。
關閉>
任何權限都可以關閉mysql服務,cmd下執行,mysqladmin -u -p shutdown。
註:net stop window service name相等。
20110427
header() and session_start()
使用header()時,要寫在所有網頁輸出"內容"(echo、print...)前,因為所有的輸出都屬於內容裡的,而又在前,所以PHP會強制且你不會知道的先輸出header(),而造成錯誤。另外使用location標頭時,通常會接die()或exit,忽略後面程式。
session_start()也是如此。
function.header
範例
session_start()也是如此。
function.header
範例
20110422
JFrame 與 Panel的關係
Jframe.setContentPane(new JDesktopPane())
jframe.getContentPane.add(new JDesktopPane())
JFrame.getContentPane
得到的會是jframe它本身的contentpane
所以是在contentpane上面"加入"JDesktopPane
JFrame.setContentPane 則是利用JDesktopPane來"取代"jframe的contentpane
20110418
PHP 與 HTML如何傳遞變數
1.用form傳遞 ,$_POST['NAME']變數
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title heretitle> head> <body>
//FORM method='post',按下submit後會切換到ootest.php
//在php中以$_POST["name"]取得其值
//ex:$_POST["studname"] <form name="form1" method="post" action="ootest.php"> <input type=text name="studname"> <input type=submit name="submit" value="enter"> form> body>
html>
2. URL(http://url/xxx.php?欄位名稱=欄位值&...),$_GET["name"]變數
<form name="form1" method="get" action="ootest.php"> <input type=text name="studname"> <input type=submit name="submit" value="enter"> form>
3. $_SERVER $_COOKIE $_SESSION變數
http://www.study-area.org/phorum/index.php?topic=32453.0;wap2
http://blog.yam.com/ihy08/article/17825438
20110415
在使用JDBC時Class.forName(String)的功用
System.out.println(Class.forName("com.mysql.jdbc.Driver").getName());
註冊driver,但是不明白為什麼註解掉也能順利連結資料庫?(似乎跟IDE有關)
實際上
Class.forName("com.mysql.jdbc.Driver");
同等於:
com.mysql.jdbc.Driver driver =new com.mysql.jdbc.Driver();
或
new com.mysql.jdbc.Driver();
預先在JVM載入類別(JAVA是類別實際用到時才會在入記憶體),
所以才寫在連接前,確保DriverManager類別的靜態方法registerDriver
先註冊Driver。
註冊driver,但是不明白為什麼註解掉也能順利連結資料庫?(似乎跟IDE有關)
實際上
Class.forName("com.mysql.jdbc.Driver");
同等於:
com.mysql.jdbc.Driver driver =new com.mysql.jdbc.Driver();
或
new com.mysql.jdbc.Driver();
預先在JVM載入類別(JAVA是類別實際用到時才會在入記憶體),
所以才寫在連接前,確保DriverManager類別的靜態方法registerDriver
先註冊Driver。
20110414
Eclipse 掛zend debugger
http://down.chinaz.com/soft/12394.htm
1. Extract the Zend Debugger package.
2. Locate the ZendDebugger.so (Unix) or ZendDebugger.dll (Windows) file in the directory which
corresponds to your version of PHP (4.3.x, 4.4.x, 5.0.x, 5.1.x, 5.2.x).
2. Add the following line to your php.ini file:
3. Add the following lines to your php.ini file:
4. Copy the dummy.php file to your document root directory.
5. Restart your Web server.
要注意ZendDebugger.dll的版本,不然就算路徑正確,PHP也抓不到
php.ini添加以下內容
http://www.ibm.com/developerworks/library/i-osource13/index.html
eclipse設定
設定PHP執行檔路徑與設定檔路徑
1. Extract the Zend Debugger package.
2. Locate the ZendDebugger.so (Unix) or ZendDebugger.dll (Windows) file in the directory which
corresponds to your version of PHP (4.3.x, 4.4.x, 5.0.x, 5.1.x, 5.2.x).
2. Add the following line to your php.ini file:
Linux and Mac OS X: zend_extension=(*) The Windows non-thread safe binary is only used with Zend Core 2.0.
Windows: zend_extension_ts=
Windows non-thread safe: zend_extension=
3. Add the following lines to your php.ini file:
zend_debugger.allow_hosts=(*) hopst_ip_addresses are the IPs of the hosts which will be allowed to initiate debug sessions
zend_debugger.expose_remotely=always
4. Copy the dummy.php file to your document root directory.
5. Restart your Web server.
要注意ZendDebugger.dll的版本,不然就算路徑正確,PHP也抓不到
php.ini添加以下內容
zend_extension_ts="完整路徑(一定要填路徑)\ZendDebugger.dll"
zend_debugger.allow_hosts=localhost
zend_debugger.expose_remotely=always
http://www.ibm.com/developerworks/library/i-osource13/index.html
eclipse設定
設定PHP執行檔路徑與設定檔路徑
MySQL phpmyadmin apache PHP 安裝
>安裝MySQL http://dev.mysql.com/downloads/
資料庫位置位於\MySQL\data
安裝過程設定DB管理員帳號密碼
>安裝apache http://www.apache.org/dist//httpd/binaries/win32/
網站根目錄位於\Apache Software Foundation\Apache2.2\htdocs
設定檔\Apache Software Foundation\Apache2.2\conf
port、servername都在設定檔裡改
>安裝PHP http://php.net/downloads.php
安裝過程指定apache位置
設定檔PHP.ini
檢查一下extension是否正確
我自己會用到curl,mysql,MBSTRING
>連接PHP與apache
在APACHE的設定檔裡
加入
---
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/program files/PHP/"
LoadModule php5_module "C:/program files/PHP/php5apache2_2.dll"
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
將 php 目錄下的 libmysql.dll 複製到 Apache Server 安裝目錄下的 bin 子目錄
(少此一步驟雖然可以完成整個安裝流程,但是在藉由phpmyadmin的登入畫面時打完sql帳密必定會造成未知的瀏覽器停止)
重啟apache
>安裝phpmyadmin
解壓phpmyadmin到Apache Software Foundation\Apache2.2\htdocs底下
建一個資料夾config在Apache Software Foundation\Apache2.2\htdocs\phpmyadmin\底下連接server http://127.0.0.1/phpmyadmin/setup/index.php
(依照apache設定連接)
依照網頁內容設定phpmyadmin產生設定檔config.inc(在config底下),取出放在上層目錄後,砍掉config資料夾(避免他人更改)。
資料庫位置位於\MySQL\data
安裝過程設定DB管理員帳號密碼
>安裝apache http://www.apache.org/dist//httpd/binaries/win32/
網站根目錄位於\Apache Software Foundation\Apache2.2\htdocs
設定檔\Apache Software Foundation\Apache2.2\conf
port、servername都在設定檔裡改
>安裝PHP http://php.net/downloads.php
安裝過程指定apache位置
設定檔PHP.ini
檢查一下extension是否正確
我自己會用到curl,mysql,MBSTRING
>連接PHP與apache
在APACHE的設定檔裡
加入
---
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/program files/PHP/"
LoadModule php5_module "C:/program files/PHP/php5apache2_2.dll"
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
---
將 php 目錄下的 libmysql.dll 複製到 Apache Server 安裝目錄下的 bin 子目錄
(少此一步驟雖然可以完成整個安裝流程,但是在藉由phpmyadmin的登入畫面時打完sql帳密必定會造成未知的瀏覽器停止)
重啟apache
>安裝phpmyadmin
解壓phpmyadmin到Apache Software Foundation\Apache2.2\htdocs底下
建一個資料夾config在Apache Software Foundation\Apache2.2\htdocs\phpmyadmin\底下連接server http://127.0.0.1/phpmyadmin/setup/index.php
(依照apache設定連接)
依照網頁內容設定phpmyadmin產生設定檔config.inc(在config底下),取出放在上層目錄後,砍掉config資料夾(避免他人更改)。
20110413
20110412
20110329
多種Close a Frame
public aFrame extends Frame implements WindowListener {
public aFrame(){
addWindowListener( this );
}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosing(WindowEvent e){ System.exit(0); }
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
}
======================
public class aFrame extends Frame {
public aFrame(){
addWindowListener( new Terminate() );
}
}
class Terminate extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
======================
public class aFrame extends Frame {
public aFrame() {
addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
}
20110328
Java中JFrame.getContentPane().add(pane)與JFrame.add(pane)有什麼區別
JFrame jf = new JFrame();
(1)jf.add(panel);
(2)if.getContentPane().add(pane);
1.4JDK:
是有區別的,在添加jf.add(panel)時這panel會與rootPane平級,而非添加到RootPane底下的containPane
1.5後:
沒有區別,jf.add(panel)會轉發成if.getContentPane().add(pane)
rootPane:
當創建一個JFrame物件時,建構子會給物件默認添加一層rootPane,rootPane包含有自已的LayoutManager、GlassPane(總是為於最上層)、layeredPane管理器(負責管理menuBar 和 contentPane)、contentPane、menuBar (可有可無)
jf.setBackground(Color.blue);
jf.getContentPane().setBackground(Color.black);
這兩句代碼,看看效果(實際上上面一句並不能改變界面的背景色,下面一句才可以,因為contenPane把rootPane給擋住了,上面一句是改變了jf.rootPane的背景色,但是你眼睛看到的並不是rootPane,其實是佔據rootPane全版面的contenPane.)
另外
jf.getContentPane()==jf.getRootPane().getContentPane()
上面的比較返回的true
所以你調用jf.getContentPane().add(panel) 其實是把panel添加到rootPane上面了 這個時候panel和rootPane就不是平級的了
(1)jf.add(panel);
(2)if.getContentPane().add(pane);
1.4JDK:
是有區別的,在添加jf.add(panel)時這panel會與rootPane平級,而非添加到RootPane底下的containPane
1.5後:
沒有區別,jf.add(panel)會轉發成if.getContentPane().add(pane)
rootPane:
當創建一個JFrame物件時,建構子會給物件默認添加一層rootPane,rootPane包含有自已的LayoutManager、GlassPane(總是為於最上層)、layeredPane管理器(負責管理menuBar 和 contentPane)、contentPane、menuBar (可有可無)
jf.setBackground(Color.blue);
jf.getContentPane().setBackground(Color.black);
這兩句代碼,看看效果(實際上上面一句並不能改變界面的背景色,下面一句才可以,因為contenPane把rootPane給擋住了,上面一句是改變了jf.rootPane的背景色,但是你眼睛看到的並不是rootPane,其實是佔據rootPane全版面的contenPane.)
另外
jf.getContentPane()==jf.getRootPane().getContentPane()
上面的比較返回的true
所以你調用jf.getContentPane().add(panel) 其實是把panel添加到rootPane上面了 這個時候panel和rootPane就不是平級的了
訂閱:
文章 (Atom)












