首页 javaswing学习笔记(javaswing学习笔记)

javaswing学习笔记(javaswing学习笔记)

举报
开通vip

javaswing学习笔记(javaswing学习笔记)javaswing学习笔记(javaswing学习笔记) javaswing学习笔记(javaswing学习笔记) Swing + + Reading Notes 1-1:Swing commonly used package PACHAGE | Content | Javax.swing | The most commonly used pachage is the class that contains various swing components Javax.swing.border |...

javaswing学习笔记(javaswing学习笔记)
javaswing学习笔记(javaswing学习笔记) javaswing学习笔记(javaswing学习笔记) Swing + + Reading Notes 1-1:Swing commonly used package PACHAGE | Content | Javax.swing | The most commonly used pachage is the class that contains various swing components Javax.swing.border | Contains classes that are related to the swing component frame Javax..Swing.colorchooser | Classes designed for the swing palette component (JColorChooser) Javax.swing.event | Handling events generated by the swing component is different from the AWT event Javax.swing.filechooser | Contains classes designed for the swing file selection dialog box (JFileChooser) ,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,, Javax.swing.plaf | The class that handles the appearance of swing components Javax.swing.plaf.basic | Javax.swing.plaf.metal | Javax.swing.plaf.multi | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,, Javax.swing.table | Classes designed for the swing table component (JTable) ,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,, Javax.swing.text | Contains classes that are related to the swing text component Javax.swing.text.html | Javax.swing.text.html.parser | Javax.swing.text.rtf | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,, Javax.swing.tree | Classes designed for the swing tree element (JTree) Javax.swing.undo | Provides functionality for swing text components, Redo, or Undo The layout of the 12:swing Almost all of the components in Swing are derived from JComponent, that is to say, these components are lightweight component, 均由纯java code所编写面成 、swing中以下几个组件不是由jcomponent继承面来: jframe (jroot pane) jdialog (jroot pane) jwindow (jroot pane) japplet (jroot pane) 以上四个组件是heavyweight component, 必须使用到native code来画出这四个窗口组件, 因为要在操作系统中显示窗口画面, 必 须使用操作系统的宣传品资源, 面以往的awt组件大多使用native code所构造出来, 因此swing中的jframe便继承原有awt中的frame 类, 面不是继承jcomponent类.同样, japplet是继承原有awt中的japplet类, 也不是继承jcomponent类. jframe、jdialog、jwindow及japplet这四个组件统称为最上层组件, 因为其余的swing组件都必须依附在此四组件之一上才能 显示出来.此四组件均实现 (implement) rootpanecontainer这个界面 (interface), 此界面定义了各种容器取得与设置并不是真实的容器, 它是由glass pane与layered pane所组成 (layered pane里拥有content pane与menu bar, 而menu bar可选择使用或不使用) 我们不能在jrootpane上加入任何的组件, 因为它只是一个虚拟的容器, 若要在最上层组件上加入组件, 必须加在layered pane或是 layered pane里的content pane上.以jframe为例, 一般我们要在jframe上加入其他组件 (如 jbutton、jlabel等) 必须先取得jframe 的content pane, 然后将要加入的组件放在此content pane中, 而不是直接就加到jframe上.因此若要在jframe中加入一 个按钮, 不 能像以前awt时一样写成 frame.add (button) 的形式, 而必须先取 得jframe的content pane, 然后将按钮加入content pane中, 如: frame.getcontentpane ().add (button) 否则在编译的时候将有错误信息产生. & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & 注意: 1.组件必须加在容器中, 而容器本身具有层次性的关系, 就如同珠 宝盒一般, 大盒子里面可以放小盒子, 小盒子里面还可以放更小的 盒子, 而珠宝就可以放在某一个盒子中, 这里的珠宝就代表组件, 盒子就代表容器.因此若您想在jframe加入任何组件时, 必须 先取得jframe的容器来放置这些组件, 而由于jframe、jdialog、 jwindow与japplet是显示swing组件的源头, 我们可以称它们为根 组件, 也就是所谓的最上层组件. 2.rootpanecontainer它是一个interface, 共有5个类实现 (implement) 它, They are JFrame, JAppleet, JWindow, JDialog, JInternalFrame, where JInternalFrame is a lightweight Component, it is not a top-level component, that is to say JInternalFrame alone cannot be displayed at the top of the component must be attached, we will discuss in the following components, JFrame, JApplet, JWindow, JDialog are the top component. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& RootPaneContainer defines the following methods: Method Container getContentPane () returns contentPane Component getGlassPane () returns glassPane JLayeredPane getLayeredPane () returns layeredPane JRootPane getRootPane returns the JRootPane that belongs to this component Void setContentPane (Container Contentpane) set ContentPane Void setGlassPane (Component GlassPane) set GlassPane Void setLayeredPane (JLayeredPane LayeredPane) set LayeredPane JFrame how to get the actual process of Content Pane, here is a piece of very simple program code: Public class Simple{ Simple () { JFrame, frame=new, JFrame (); Container, contentPane=frame.getContentPane (); JButton, button=new, JButton (); ContentPane.add (button); } } When we write frame.getContentPane (), we return the frame of this Content Pane, that is, a container component, with a container before we can button components Put in, at this point, JFrame has the button component. So JFrame is like an open space. In this open space, people should first build a house (container), and then people Furniture, equipment, etc. (components) will be able to move into the house. The following hierarchy illustrates that the top-level component contains the JRootPane component and that the JRootPane itself contains the content A component component that allows the upper level component to load into other components. |Frame------JFrame (JRoot, Pane) | Window|Dialog-----JDialog (JRoot, Pane) | | |-----------JWindow (JRoot, Pane) Applet -----------JApplet (JRoot Pane) Graphic representation: |Grass Pane | Root Pane| | |Content Pane |Layered Pane| |Menu Bar 1-3: layout manager (Layout Menager) |BorderLayout |FlowLayout |GridLayout AWT----|CardLayout |GridBagLayout Swing--|BoxLayout The use of 1-3-1:BorderLayout: Class hierarchy diagram of BorderLayout: Java.lang.Object --java.awt.BorderLayout Constructor: BorderLayout () creates a border layout without spacing BorderLayout (int, hgap, int) Vgap) establish a border layout that is spaced between components BorderLayout divides the layout into East, West, South, North and middle Example: 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**/ Close the window / * processing operation, if you don't write this, even if you have closed the window, but the program does not terminate. * / F.addWindowListener ( New, WindowAdapter () { Public, void, windowClosing (WindowEvent, e) { System.exit (0); } } ); /***read**/ } Public, static, void, main (String[], args) { BorderLayoutDemo, b=new, BorderLayoutDemo (); } } Set the spacing of components, you can use the BorderLayout constructor with spacing parameters, or you can use BorderLayout's setHgap (int) Hgap) and SetVgap (int, vgap) two ways to achieve. The use of 1-3-2:FlowLayout: Class hierarchy diagram of FlowLayout: Java.lang.Object --java.awt.FlowLayout Constructor: FlowLayout () creates a new Flow Layout, the default value of this FlowLayout is centered, and the components have 5 units of horizontal and vertical spacing to each other. FlowLayout (int align) creates a new Flow Layout, this FlowLayout can set the arrangement, The components have 5 units of horizontal and vertical to each other Spacing. FlowLayout (int, align, int, hgap, int) Vgap) build a new Flow Layout, which FlowLayout sets the distance between the permutations and the components. 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 (); You can use / spacing FlowLayout constructor, make FlowLayout arrangement with a pitch, and * and the direction of the array can be used to specify the direction in which FlowLayout is arranged in five permutations, * followed by CENTER (default values), LEFT, RIGHT, LEADING, TRAILING, if we are going to take the following program thirteenth * line changed to 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); / / must be removed, otherwise the setSize function has no effect F.setSize (400220); 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 (); } } The use of 1-3-3:GridLayout: Class hierarchy diagram of GridLayout: Java.lang.Object --java.awt.GridLayout GridLayout has more rows and columns than FlowLayout, that is to say, you have to set GridLayout in a couple of rows and columns, just like a two-dimensional plane, and then you add The component will fill in the first row of the grid, and then start from the second line, and so on, like a grid. And GridLayout will fill in the group The size of the part is the same. Constructor: GridLayout () sets up a new GridLayout, which defaults to 1 rows and 1 columns. GridLayout(int,int行, 因为建立一个几行几列的GridLayout)。 GridLayout(int,int列行,hgap,int,int vgap)建立一个几行几列的GridLayout,并设置组件的间距。 例子:gridlayoutdemo.java 进口java AWT *; 导入java awt事件*。。; 进口javax.摆动。*; 公共课cardlayoutdemo实现ActionListener { P1,P2,P3,JPanel,P4; int = i 1; JFrame F; 公共cardlayoutdemo() { F =新jframe();/ /当做顶层组件 容器的内容块= F getcontentpane(); 内容块。setLayout(新GridLayout(2,1)); P1 =新jpanel(); 创建新的按钮(“换卡”); b.addactionlistener(本);/ /当按下”转变 卡”时,进行事件监听,将会有系统操作产生。 P1。添加(B);文件:/ /处理操作在行52-64。 添加内容块(P1); P2 =新jpanel(); P2 setLayout(新flowlayout()); P2。添加(新JButton(“第一”)); P2。添加(新JButton(二)); P2。添加(新JButton(“第三”)); P3 =新jpanel(); P3。setLayout(新GridLayout(3,1)); P3。添加(新JButton(“第四”)); P3。添加(新JButton(“第五”)); P3。添加(新JButton(“这是最后一个按钮“)); P4 =新jpanel(); P4 setLayout(新cardlayout()); 添加(“一”,P2); 添加(“2”,P3); / *要显示的卡片CardLayout,除了用显示(容器母,字符串 这个方法外名字) *,也可试试第一(容器)、下(容器),以前的(容器),最后(容器) 这 *四个方法,一样可以达到显示效果。 * / ((CardLayout)P4。getlayout())。显示(P4,“一”); 添加内容块(P4); f.settitle(“卡片布局管理器”); F. pack(); f.setvisible(真的); f.addwindowlistener( 新的windowadapter() { 公共无效windowclosing(窗口事件类e){ 系统(出口)(0); } } ); } public void actionPerformed(ActionEvent事件){ 开关(i){ 案例1: ((CardLayout)P4。getlayout())。显示(P4,“两”); 打破; 案例2: ((CardLayout)P4。getlayout())。显示(P4,“一”); 打破; } ++; 如果(i = 3)i = 1; F. validate(); } public static void main(String [] args){ 新的cardlayoutdemo(); } } 1-3-5:网格包布局管理器的使用:是中最有弹性但也是最复杂的一种版面管理器它只有一种构造函数,但必须配合java。 自才能达到设置的效果。 的类层次结构图网格包布局管理器: java.lang.Object --java.awt.gridbaglayout 构造函数: girdbaglayout()建立一个新的管理器网格包布局管理器。 gridbagconstraints()建立一个新的自对象。 自(int gridx,int格子,int gridwidth,int gridheight,双weightx,双重, int锚,int填补,插图 ipadx insets,int,int iPady)建立一个新的自对象 ,并指定其参数的值。 参数说明: gridx,格子:设置组件的位置,相对代表此组件位于之前所加入组件的右边gridx设置为自。 若将格子设置为自代表此组件位于以前所加入组件的下面建议定义出。相对。 gridx,格子的位置,以便以后维护程序。表示放在几行几列、gridx = 0,0 = 0时放在格子行0列。 gridwidth,gridheight:用来设置组件所占的单位长度与高度,默认值皆为1。你可以使用剩余常自。 量,代表此组件为此行或此列的最后一个组件,而且会占据所有剩余的空间。 weightx,沉重的:用来设置窗口变大时,各组件跟着变大的比例,当数字越大,表示组件能得到更多的空间,默认值皆为0。 锚: 当组件空间大于组件本身时,要将组件置于何处,有中心(默认值)、北东北东东南、、、、 西、西北可供选择。 插图:设置组件之间彼此的间距,它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。 ipadx,iPady:设置组件内的间距,默认值为0。 我们以前提过, Various settings in GridBagLayout must be passed through GridBagConstraints, so when we set all the parameters of GridBagConstraints After that, you must new a GridBagConstraints object so that GridBagLayout can use it. Example: 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, 格子,gridwidth,gridheight,weightx,沉重的,锚, 填充,插图,ipadx iPady); gridbag setconstraints(B,C); (b)添加内容块; B =新JButton(“第四”); gridx = 1; 格子= 1; gridwidth = 1; gridheight = 1; weightx = 1; 有分量的= 1; 锚= GridBagConstraints.CENTER; 填方= gridbagconstraints.horizontal; 插图=新的插图(0,0,0,0); ipadx = 0; iPady = 0; C =新 自(gridx,格子,gridwidth,gridheight,weightx,沉重的,锚, 填充,插图,ipadx iPady); gridbag setconstraints(B,C); (b)添加内容块; B =新JButton(“这是最后一个按钮”); gridx = 2; 格子= 1; gridwidth = 1; gridheight = 2; weightx = 1; 有分量的= 1; 锚= GridBagConstraints.CENTER; 填方= gridbagconstraints.horizontal; 插图=新的插图(0,0,0,0); ipadx = 0; iPady = 50; C =新 自(gridx,格子,gridwidth,gridheight,weightx,沉重的,锚, 填充,插图,ipadx iPady); gridbag setconstraints(B,C); (b)添加内容块; f.settitle(“网格包布局管理器”); F. pack(); f.setvisible(真的); f.addwindowlistener( 新的windowadapter() { 公共无效windowclosing(窗口事件类e){ 系统(出口)(0); } } ); } public static void main(String [] args){ 新的gridbaglayoutdemo(); } }
本文档为【javaswing学习笔记(javaswing学习笔记)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_014457
暂无简介~
格式:doc
大小:53KB
软件:Word
页数:0
分类:生活休闲
上传时间:2017-09-30
浏览量:36