java中窗體的創(chuàng)建!
一般來講,在java中要完成一個(gè)Frame或者JFrame的顯示,需要以下步驟,通常都將主類(定義為public的類)繼承于Frame或者JPanel。
(一)如果是繼承自Frame,則:
設(shè)置標(biāo)題:setTitle("Your Title");
設(shè)置大小:setSize(int width,int height)或者pack()
使窗口顯示:setVisible(true)
使窗口居中顯示:setLocationRelativeTo(null)
使窗口的關(guān)閉動(dòng)作有效:
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
(二)如果是繼承自JPanel,則:
設(shè)置標(biāo)題:setTitle("Your Title");
設(shè)置外觀:JFrame.setDefaultLookAndFeelDecorated(true);
(一)如果是繼承自Frame,則:
設(shè)置標(biāo)題:setTitle("Your Title");
設(shè)置大小:setSize(int width,int height)或者pack()
使窗口顯示:setVisible(true)
使窗口居中顯示:setLocationRelativeTo(null)
使窗口的關(guān)閉動(dòng)作有效:
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
(二)如果是繼承自JPanel,則:
設(shè)置標(biāo)題:setTitle("Your Title");
設(shè)置外觀:JFrame.setDefaultLookAndFeelDecorated(true);
得到內(nèi)容面板的內(nèi)容:JComponent jc=new 主類名();
設(shè)置內(nèi)容不透明:jc.setOpaque(true);
設(shè)置內(nèi)容面板:setContentPane(jc);
設(shè)置大小:setSize(int width,int height)或者pack()
使窗口顯示:setVisible(true)
使窗口居中顯示:setLocationRelativeTo(null)
使窗口的關(guān)閉動(dòng)作有效:setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
設(shè)置內(nèi)容面板:setContentPane(jc);
設(shè)置大小:setSize(int width,int height)或者pack()
使窗口顯示:setVisible(true)
使窗口居中顯示:setLocationRelativeTo(null)
使窗口的關(guān)閉動(dòng)作有效:setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此外,還有另外一個(gè)設(shè)置窗口大小的函數(shù)是setPreferredSize(new Dimension(int width,int height)),但是調(diào)用該函數(shù)后必須再調(diào)用pack()函數(shù)才行。而且,該函數(shù)比setSize函數(shù)的優(yōu)先級(jí)高,如果同時(shí)設(shè)置了setPreferredSize和setSize兩個(gè)函數(shù),那么setSize函數(shù)將不發(fā)揮作用。
posted on 2007-12-20 23:45 so true 閱讀(1194) 評(píng)論(0) 編輯 收藏 所屬分類: Java