免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
JR - 精品文章 - JAVASWING學(xué)習(xí)筆記(清華大學(xué))
++ Swing讀書(shū)筆記
1-1:Swing常用的package
PACHAGE  |
內(nèi)          容
-----------------|-------------------------------
javax.swing                      |
最常用的pachage,包含了各種swing組件的類(lèi)
javax.swing.border               |
包含與swing組件外框有關(guān)的類(lèi)
javax..swing.colorchooser        |
針對(duì)swing調(diào)色盤(pán)組件(JColorChooser)所設(shè)計(jì)的類(lèi)
javax.swing.event                |
處理由swing組件產(chǎn)生的事件,有別于AWT事件
javax.swing.filechooser          |
包含針對(duì)swing文件選擇對(duì)話(huà)框(JFileChooser)所設(shè)計(jì)的類(lèi)
----------------------------------------------
javax.swing.plaf                 |
處理swing組件外觀的相關(guān)類(lèi)
javax.swing.plaf.basic           |
javax.swing.plaf.metal           |
javax.swing.plaf.multi           |
----------------------------------------------
javax.swing.table                |
針對(duì)swing表格組件(JTable)所設(shè)計(jì)的類(lèi)
----------------------------------------------
javax.swing.text                 |
包含與swing文字組件相關(guān)的類(lèi)
javax.swing.text.html            |
javax.swing.text.html.parser     |
javax.swing.text.rtf             |
----------------------------------------------
javax.swing.tree                 |
針對(duì)swing樹(shù)關(guān)元件(JTree)所設(shè)計(jì)的類(lèi)
javax.swing.undo                 |
提供swing文字組件Redo或Undo的功能
1-2:swing的版面結(jié)構(gòu)
Swing中幾乎所有組件都是從JComponent衍生而來(lái),也就是說(shuō)這些組件都是lightweight
Component,均由純java code所編寫(xiě)面成
、Swing中以下幾個(gè)組件不是由JComponent繼承面來(lái):
JFrame(JRoot Pane)
JDialog(JRoot Pane)
JWindow(JRoot Pane)
JApplet(JRoot Pane)
以上四個(gè)組件是heavyweight Component,必須使用到native
code來(lái)畫(huà)出這四個(gè)窗口組件,因?yàn)橐诓僮飨到y(tǒng)中顯示窗口畫(huà)面,必
須使用操作系統(tǒng)的宣傳品資源,面以往的AWT組件大多使用native
code所構(gòu)造出來(lái),因此Swing中的JFrame便繼承原有AWT中的Frame
類(lèi),面不是繼承JComponent類(lèi)。同樣,JApplet是繼承原有AWT中的JApplet類(lèi),也不是繼承JComponent類(lèi)。
JFrame、JDialog、JWindow及JApplet這四個(gè)組件統(tǒng)稱(chēng)為最上層組件,因?yàn)槠溆嗟膕wing組件都必須依附在此四組件之一上才能
顯示出來(lái)。此四組件均實(shí)現(xiàn)(Implement)RootPaneContainer這個(gè)界面(Interface),此界面定義了各種容器取得與設(shè)置并不是真實(shí)的容器,它是由Glass
Pane與Layered Pane所組成(Layered Pane里擁有Content Pane與Menu
Bar,而Menu Bar可選擇使用或不使用),
我們不能在JRootPane上加入任何的組件,因?yàn)樗皇且粋€(gè)虛擬的容器,若要在最上層組件上加入組件,必須加在Layered
Pane或是
Layered Pane里的Content
Pane上。以JFrame為例,一般我們要在JFrame上加入其他組件(如JButton、JLabel等)必須先取得JFrame
的Content Pane,然后將要加入的組件放在此Content
Pane中,而不是直接就加到JFrame上。因此若要在JFrame中加入一個(gè)按鈕,不
能像以前AWT時(shí)一樣寫(xiě)成frame.add(button)的形式,而必須先取得JFrame的Content
Pane,然后將按鈕加入Content Pane中,如:
frame.getContentPane().add(button)
否則在編譯的時(shí)候?qū)⒂绣e(cuò)誤信息產(chǎn)生。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
注意:
1.組件必須加在容器中,而容器本身具有層次性的關(guān)系,就如同珠寶盒一般,大盒子里面可以放小盒子,小盒子里面還可以放更小的盒子,而珠寶就可以放在某一個(gè)盒子中,這里的珠寶就代表組件,盒子就代表容器。因此若您想在JFrame加入任何組件時(shí),必須
先取得JFrame的容器來(lái)放置這些組件,而由于JFrame、JDialog、JWindow與JApplet是顯示Swing組件的源頭,我們可以稱(chēng)它們?yōu)楦?div style="height:15px;">
組件,也就是所謂的最上層組件。
2.RootPaneContainer它是一個(gè)interface,共有5個(gè)類(lèi)實(shí)現(xiàn)(Implement)它,分別是JFrame、JAppleet、JWindow、JDialog、
JInternalFrame,其中JInternalFrame是一個(gè)lightweight
Component,它不是一個(gè)最上層組件,也就是說(shuō)JInternalFrame不能單獨(dú)顯示出來(lái),必須依附在最上層組件中,我們將在下面討論組件,而JFrame,JApplet,JWindow,JDialog均為最上層組件。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
RootPaneContainer定義了下面幾種方法:
方法
Container       getContentPane()返回contentPane
Component       getGlassPane()返回glassPane
JLayeredPane    getLayeredPane()返回layeredPane
JRootPane       getRootPane返回屬于這個(gè)組件的JRootPane
Void            setContentPane(Container
contentpane)設(shè)置ContentPane
Void            setGlassPane(Component
glassPane)設(shè)置GlassPane
Void            setLayeredPane(JLayeredPane
layeredPane)設(shè)置LayeredPane
JFrame如何取得Content Pane的實(shí)際流程,下面是一段很簡(jiǎn)單的程序代碼:
public class Simple{
Simple(){
JFrame frame=new JFrame();
Container contentPane=frame.getContentPane();
JButton button=new JButton();
contentPane.add(button);
}
}
當(dāng)我們寫(xiě)frame.getContentPane()時(shí),會(huì)返回此frame的Content
Pane,也就是一個(gè)容器組件,有了容器之后我們才能將button組件
擺進(jìn)去,此時(shí)JFrame才算擁有button組件。所以JFrame就好像是一塊空地,要在這空地上信人應(yīng)該先蓋一棟房子(容器),然后人
、家具、設(shè)備等等(組件)就能搬進(jìn)此房子中。下面的層次結(jié)構(gòu)說(shuō)明了最上層組件都含有JRootPane組件,JRootPane本身就含有容
器組件,可讓最上層組件裝入其他的組件。
|Frame------JFrame(JRoot Pane)
|
Window|Dialog-----JDialog(JRoot Pane)
|
|
|-----------JWindow(JRoot Pane)
Applet -----------JApplet(JRoot Pane)
圖示:
|Grass Pane
|
Root Pane|
|            |Content Pane
|Layered Pane|
|Menu Bar
1-3:版面管理器(Layout Menager)
|BorderLayout
|FlowLayout
|GridLayout
AWT----|CardLayout
|GridBagLayout
Swing--|BoxLayout
1-3-1:BorderLayout的使用:
BorderLayout的類(lèi)層次結(jié)構(gòu)圖:
java.lang.Object
--java.awt.BorderLayout
構(gòu)造函數(shù):BorderLayout()建立一個(gè)沒(méi)有間距的border layout
BorderLayout(int hgap,int
vgap)建立一個(gè)組件間有間距的border layout
BorderLayout將版面劃分為東、西、南、北、中
例子:BorderLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BorderLayoutDemo{
public BorderLayoutDemo(){
JFrame f=new JFrame();
Container contentPane=f.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new
JButton("EAST"),BorderLayout.EAST);
contentPane.add(new
JButton("WEST"),BorderLayout.WEST);
contentPane.add(new
JButton("SOUTH"),BorderLayout.SOUTH);
contentPane.add(new
JButton("NORTH"),BorderLayout.NORTH);
contentPane.add(new
JLabel("CENTER",JLabel.CENTER),BorderLayout.CENTER);
f.setTitle("BorderLayout");
f.pack();
f.setVisible(true);
/***read**/
/*處理關(guān)閉窗口的操作,若你沒(méi)寫(xiě)這一段,就算你已經(jīng)關(guān)閉窗口了,但程序并不會(huì)終止。
*/
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
/***read**/
}
public static void main(String[] args){
BorderLayoutDemo b=new BorderLayoutDemo();
}
}
設(shè)置組件的間距,你可以使用有間距參數(shù)的BorderLayout構(gòu)造函數(shù),也可以利用BorderLayout的setHgap(int
hgap)與
setVgap(int vgap)兩個(gè)方法來(lái)達(dá)成。
1-3-2:FlowLayout的使用:
FlowLayout的類(lèi)層次結(jié)構(gòu)圖:
java.lang.Object
--java.awt.FlowLayout
構(gòu)造函數(shù):FlowLayout()建立一個(gè)新的Flow
Layout,此FlowLayout默認(rèn)值是居中對(duì)齊,組件彼此有5單位的水平與垂直間距。
FlowLayout(int align)建立一個(gè)新的Flow
Layout,此FlowLayout可設(shè)置排列方式,組件彼此有5單位的水平與垂直
間距。
FlowLayout(int align,int hgap,int
vgap)建立一個(gè)新的Flow Layout,此FlowLayout可設(shè)置排列方式與組件間距。
FlowLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlowLayoutDemo{
public FlowLayoutDemo(){
JFrame f=new JFrame();
Container contentPane=f.getContentPane();
/*你可以使用有間距的FlowLayout構(gòu)造函數(shù),使FlowLayout的排列具有間距,并
*并可利用排列方向參數(shù)來(lái)指定靠什么方向排列,F(xiàn)lowLayout共有五種排列方式,
*依次是CENTER(默認(rèn)值),LEFT,RIGHT,LEADING,TRAILING,若我們將下面程序第13
*行改成contentPane.setLayout(new
FlowLayout(FlowLayout.LEFT));
*/
contentPane.setLayout(new FlowLayout());
contentPane.add(new JButton("first"));
contentPane.add(new JButton("second"));
contentPane.add(new JButton("third"));
contentPane.add(new JButton("fourth"));
contentPane.add(new JButton("fifth"));
contentPane.add(new JButton("Last"));
f.setTitle("FlowLayout");
file://f.pack();//必須將f.pach()去掉,否則setSize功能將沒(méi)有作用
f.setSize(400,220);
f.setVisible(true);
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public static void main(String[] args){
FlowLayoutDemo b=new FlowLayoutDemo();
}
}
1-3-3:GridLayout的使用:
GridLayout的類(lèi)層次結(jié)構(gòu)圖:
java.lang.Object
--java.awt.GridLayout
GridLayout比FlowLayout多了行和列的設(shè)置,也就是說(shuō)你要先設(shè)置GridLayout共有幾行幾列,就如同二維平面一般,然后你加
進(jìn)去的組件會(huì)先填第一行的格子,然后再?gòu)牡诙虚_(kāi)始填,依此類(lèi)扒,就像是一個(gè)個(gè)的格子一般。而且GridLayout會(huì)將所填進(jìn)去組
件的大小設(shè)為一樣。
構(gòu)造函數(shù):GridLayout()建立一個(gè)新的GridLayout,默認(rèn)值是1行1列。
GridLayout(int rows,int
cols)建立一個(gè)幾行幾列的GridLayout.
GridLayout(int rows,int cols, int hgap,int
vgap)建立一個(gè)幾行幾列的GridLayout,并設(shè)置組件的間距。
例子:GridLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo implements ActionListener{
JPanel p1,p2,p3,p4;
int i=1;
JFrame f;
public CardLayoutDemo(){
f=new JFrame();//當(dāng)做top-level組件
Container contentPane=f.getContentPane();
contentPane.setLayout(new GridLayout(2,1));
p1=new JPanel();
Button b=new Button("Change Card");
b.addActionListener(this);//當(dāng)按下"Change
Card"時(shí),進(jìn)行事件監(jiān)聽(tīng),將會(huì)有系統(tǒng)操作產(chǎn)生。
p1.add(b);                file://處理操作在52-64行.
contentPane.add(p1);
p2=new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JButton("first"));
p2.add(new JButton("second"));
p2.add(new JButton("third"));
p3=new JPanel();
p3.setLayout(new GridLayout(3,1));
p3.add(new JButton("fourth"));
p3.add(new JButton("fifth"));
p3.add(new JButton("This is the last button"));
p4=new JPanel();
p4.setLayout(new CardLayout());
p4.add("one",p2);
p4.add("two",p3);
/*要顯示CardLayout的卡片,除了用show(Container parent,String
name)這個(gè)方法外
*,也可試試first(Container),next(Container),previous(Container),last(Container)這
*四個(gè)方法,一樣可以達(dá)到顯示效果。
*/
((CardLayout)p4.getLayout()).show(p4,"one");
contentPane.add(p4);
f.setTitle("CardLayout");
f.pack();
f.setVisible(true);
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent event){
switch(i){
case 1:
((CardLayout)p4.getLayout()).show(p4,"two");
break;
case 2:
((CardLayout)p4.getLayout()).show(p4,"one");
break;
}
i++;
if (i==3) i=1;
f.validate();
}
public static void main(String[] args){
new CardLayoutDemo();
}
}
1-3-5:GridBagLayout的使用:是java中最有彈性但也是最復(fù)雜的一種版面管理器。它只有一種構(gòu)造函數(shù),但必須配合
GridBagConstraints才能達(dá)到設(shè)置的效果。
GridBagLayout的類(lèi)層次結(jié)構(gòu)圖:
java.lang.Object
--java.awt.GridBagLayout
構(gòu)造函數(shù):
GirdBagLayout()建立一個(gè)新的GridBagLayout管理器。
GridBagConstraints()建立一個(gè)新的GridBagConstraints對(duì)象。
GridBagConstraints(int gridx,int gridy,int
gridwidth,int gridheight,double weightx,double weighty,
int anchor,int fill, Insets
insets,int ipadx,int ipady)建立一個(gè)新的GridBagConstraints對(duì)象
,并指定其參數(shù)的值。
參數(shù)說(shuō)明:
gridx,gridy:設(shè)置組件的位置,gridx設(shè)置為GridBagConstraints.RELATIVE代表此組件位于之前所加入組件的右邊。
若將gridy設(shè)置為GridBagConstraints.RELATIVE代表此組件位于以前所加入組件的下面。建議定義出
gridx,gridy的位置,以便以后維護(hù)程序。表示放在幾行幾列,gridx=0,gridy=0時(shí)放在0行0列。
gridwidth,gridheight:用來(lái)設(shè)置組件所占的單位長(zhǎng)度與高度,默認(rèn)值皆為1。你可以使用GridBagConstraints.REMAINDER常
量,代表此組件為此行或此列的最后一個(gè)組件,而且會(huì)占據(jù)所有剩余的空間。
weightx,weighty:用來(lái)設(shè)置窗口變大時(shí),各組件跟著變大的比例,當(dāng)數(shù)字越大,表示組件能得到更多的空間,默認(rèn)值皆為0。
anchor:
當(dāng)組件空間大于組件本身時(shí),要將組件置于何處,有CENTER(默認(rèn)值)、NORTH、NORTHEAST、EAST、SOUTHEAST、
WEST、NORTHWEST可供選擇。
insets:設(shè)置組件之間彼此的間距,它有四個(gè)參數(shù),分別是上,左,下,右,默認(rèn)為(0,0,0,0).
ipadx,ipady:設(shè)置組件內(nèi)的間距,默認(rèn)值為0。
我們以前提過(guò),GridBagLayout里的各種設(shè)置都必須通過(guò)GridBagConstraints,因此當(dāng)我們將GridBagConstraints的參數(shù)都設(shè)置
好了之后,必須new一個(gè)GridBagConstraints的對(duì)象出來(lái),以便GridBagLayout使用。
例子:
GridBagLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutDemo{
public GridBagLayoutDemo(){
JButton b;
GridBagConstraints c;
int
gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
double weightx,weighty;
Insets inset;
JFrame f=new JFrame();
GridBagLayout gridbag=new GridBagLayout();
Container contentPane=f.getContentPane();
contentPane.setLayout(gridbag);
b=new JButton("first");
gridx=0;
gridy=0;
gridwidth=1;
gridheight=1;
weightx=10;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("second");
gridx=1;
gridy=0;
gridwidth=2;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=50;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("third");
gridx=0;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("fourth");
gridx=1;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("This is the last button");
gridx=2;
gridy=1;
gridwidth=1;
gridheight=2;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
f.setTitle("GridBagLayout");
f.pack();
f.setVisible(true);
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public static void main(String[] args){
new GridBagLayoutDemo();
}
}
<img height="1" width="1" border="0" src="http://image.360doc.com/DownloadImg/1137/66638_8" />
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
swing/swt可視化開(kāi)發(fā)工具windowbuilder免費(fèi)了 - pengo - Bl...
Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例
JFrame(框架)中添加和設(shè)置JPanel(面板)的方法
Swing理解Swing中的事件與線(xiàn)程
《Java語(yǔ)言編程基礎(chǔ)立體化實(shí)用教程》2-3 登錄窗體的實(shí)現(xiàn)
下的日期控件 DatePicker 和JXDatePicker | 基于實(shí)例代碼分步講解 一站式學(xué)習(xí)Java | how2j.cn
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服