![23使用布局管理器ppt課件_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/19/82ca8bbd-8322-46a8-9a0a-a276f9ab4d68/82ca8bbd-8322-46a8-9a0a-a276f9ab4d681.gif)
![23使用布局管理器ppt課件_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/19/82ca8bbd-8322-46a8-9a0a-a276f9ab4d68/82ca8bbd-8322-46a8-9a0a-a276f9ab4d682.gif)
![23使用布局管理器ppt課件_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/19/82ca8bbd-8322-46a8-9a0a-a276f9ab4d68/82ca8bbd-8322-46a8-9a0a-a276f9ab4d683.gif)
![23使用布局管理器ppt課件_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/19/82ca8bbd-8322-46a8-9a0a-a276f9ab4d68/82ca8bbd-8322-46a8-9a0a-a276f9ab4d684.gif)
![23使用布局管理器ppt課件_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/19/82ca8bbd-8322-46a8-9a0a-a276f9ab4d68/82ca8bbd-8322-46a8-9a0a-a276f9ab4d685.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、2.3 運用規(guī)劃管理器主講教師:董婷一、知識才干目的1. 了解容器的概念及層次;2. 掌握常用規(guī)劃管理器的運用;3. 運用規(guī)劃管理器設計規(guī)劃美觀的GUI程序 二、工程14 電子日歷 顯示2019年的電子日歷 1.程序代碼import java.util.Calendar;import java.util.*;import java.awt.*;import java.awt.event.*;import java.applet.*;public class CalendarMainClass public static void main(String args) CalendarFrame
2、frame=new CalendarFrame(); frame.setBounds(100,100,360,300); /設置窗體位置大小 frame.setVisible(true); /設置窗體可見 frame.validate(); frame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); class CalendarBean/創(chuàng)建用于判別年月日之間關系的類CalendarBean String day; int year=2019,mo
3、nth=0; public void setYear(int year) /設置年份的方法 this.year=year; public int getYear() /獲得年份的方法 return year; public void setMonth(int month) /設置月份的方法 this.month=month; public int getMonth() /獲得月份的方法 return month; public String getCalendar() String a=new String42; Calendar 日歷=Calendar.getInstance(); 日歷.s
4、et(year,month-1,1); int 星期幾=日歷.get(Calendar.DAY_OF_WEEK)-1; int day=0; if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) day=31; if(month=4|month=6|month=9|month=11) day=30; if(month=2) /判別月份能否為2月 if(year%4=0)&(year%100!=0)|(year%400=0)/判別能否為閏年 day=29; else day=28; for(int i=星期幾,n=1;i星期幾
5、+day;i+) ai=String.valueOf(n); n+; return a; class CalendarFrame extends Frame implements ActionListener/創(chuàng)建日歷界面類CalendarFrame Label labelDay=new Label42; Button titleName=new Button7; String name=日,一,二,三,四,五,六; Button nextMonth,previousMonth; int year=2019,month=2; CalendarBean calendar; Label showM
6、essage=new Label(,Label.CENTER); public CalendarFrame() Panel pCenter=new Panel(); /創(chuàng)建pCenter面板 pCenter.setLayout(new GridLayout(7,7); /將pCenter的規(guī)劃設置為7行7列的GridLayout規(guī)劃 for(int i=0;i7;i+) titleNamei=new Button(namei); pCenter.add(titleNamei); for(int i=0;i42;i+) labelDayi=new Label(,Label.CENTER); pC
7、enter.add(labelDayi); calendar=new CalendarBean(); calendar.setYear(year); calendar.setMonth(month); String day=calendar.getCalendar(); for(int i=0;i12) month=1; calendar.setMonth(month); String day=calendar.getCalendar(); for(int i=0;i42;i+) labelDayi.setText(dayi); else if(e.getSource()=previousMo
8、nth) /單擊上月按鈕時刷新日期 month=month-1; if(month1) month=12; calendar.setMonth(month); String day=calendar.getCalendar(); for(int i=0;i42;i+) labelDayi.setText(dayi); showMessage.setText(當前是:+calendar.getYear()+年+calendar.getMonth()+月); 2.相關知識 流規(guī)劃FlowLayout 呈程度放置,直到同一條線上再也沒有適宜的控件 對齊方式由 align 屬性確定。能夠的值為: LE
9、FT RIGHT CENTER LEADING TRAILING 方法方法主要功能主要功能FowLayout()生成一個默認的流式布局FlowLayout(int alignment)可以設定每一行組件的對齊方式FlowLayout(int alignment,int horz,int vert)可以設定對齊方式以及通過參數(shù)horz和vert分別設定組件的水平和垂直間距void addLayoutComponent(Stringname, Componentcomp)將指定的組件添加到布局中void setAlignment(intalign)設置此布局的對齊方式void setHgap(in
10、thgap)設置組件之間以及組件與Container 的邊之間的水平間隙void setVgap(intvgap)設置組件之間以及組件與Container 的邊之間的垂直間隙int getAlignment()獲取此布局的對齊方式int getHgap()獲取組件之間以及組件與Container 的邊之間的水平間隙int getVgap()獲取組件之間以及組件與Container 的邊之間的垂直間隙 邊境規(guī)劃BorderLayout 對容器組件進展安排,并調整其大小,使其符合以下五個區(qū)域:南、北、東、西和中間區(qū)域方法方法主要功能主要功能BorderLayout( )構造一個組件之間沒有間距的新
11、邊框布局BorderLayout (inthgap, intvgap)構造一個具有指定組件間距的邊框布局void addLayoutComponent(Componentcomp, Objectconstraints)使用指定的約束對象將指定組件添加到布局中void setHgap(inthgap)設置組件之間的水平間隙void setVgap(intvgap)設置組件之間的垂直間隙int getHgap()獲取組件之間的水平間隙int getVgap()獲取組件之間的垂直間隙 網(wǎng)格規(guī)劃GridLayout以矩形網(wǎng)格方式對容器的組件進展布置 容器被分成大小相等的矩形,一個矩形中放置一個組件 方
12、法方法主要功能主要功能GridLayout ( )生成一個單列的網(wǎng)格布局。默認無間隙。GridLayout(int row,int col)生成一個設定行數(shù)和列數(shù)的網(wǎng)格布局GridLayout(int row,int col,int horz,int vert)可以設置組件之間的水平和垂直間隔void addLayoutComponent(Stringname, Componentcomp)將具有指定名稱的指定組件添加到布局void setColumns(intcols)將此布局中的列數(shù)設置為指定值void setRows(introws)將此布局中的行數(shù)設置為指定值int getColumn
13、s()獲取此布局中的列數(shù)int getRows()獲取此布局中的行數(shù) 卡片規(guī)劃CardLayout將容器中的每個組件看作一張卡片方法方法主要功能主要功能CardLayout()創(chuàng)建一個間距大小為 0 的新卡片布局CardLayout(inthgap, intvgap)創(chuàng)建一個具有指定水平間距和垂直間距的新卡片布局void addLayoutComponent(Componentcomp, Objectconstraints)將指定的組件添加到此卡片布局的內部名稱表void next(Container parent)翻轉到指定容器的下一張卡片void previous(Container pa
14、rent)翻轉到指定容器的前一張卡片void first(Container parent)翻轉到容器的第一張卡片void last(Container parent)翻轉到容器的最后一張卡片void show(Container parent,String name)翻轉到使用addLayoutComponent添加到此布局的具有指定name的組件三、工程15 運用網(wǎng)格袋規(guī)劃 運用網(wǎng)格袋規(guī)劃管理器,將窗體中的控件進展如圖規(guī)劃設計 1.程序代碼import java.awt.*;import java.awt.event.*;import javax.swing.*;class GBC ext
15、ends GridBagConstraints private static final long serialVersionUID = 6657272964185905015L;/設置網(wǎng)格座標參數(shù)public GBC(int x, int y) this.gridx = x;this.gridy = y;/設置網(wǎng)格座標和占的網(wǎng)格數(shù)public GBC(int gridx, int gridy, int gridwidth, int gridheight) this.gridx = gridx;this.gridy = gridy;this.gridwidth = gridwidth;this
16、.gridheight = gridheight; public GBC setAnchor(int anchor) this.anchor = anchor;return this;public GBC setFill(int fill) this.fill = fill;return this; /設置單元網(wǎng)格大小public GBC setWeight(double weightx, double weighty) this.weightx = weightx;this.weighty = weighty;return this; /設置網(wǎng)格之間的相互間隔public GBC setIn
17、set(int distance) this.insets = new Insets(distance, distance, distance, distance);return this; /設置網(wǎng)格之間的相互間隔public GBC setInset(int top, int left, int bottom, int right) this.insets = new Insets(top, left, bottom, right);return this; /設置網(wǎng)格的初始位置public GBC setIpad(int ipadx, int ipady) this.ipadx = ip
18、adx;this.ipady = ipady;return this;public class GridBagLayoutTest extends JFrame /* 序列化序號private static final long serialVersionUID = 6763277356654198370L; public GridBagLayoutTest() this.setTitle(“GridBagLayoutTest); / 窗體標題 this.setSize(400, 150); / 窗體大小 / 設置窗體居中顯示在屏幕上Dimension size = Toolkit.getDe
19、faultToolkit().getScreenSize();this.setLocation(int) (size.getWidth() - this.getWidth() / 2,(int) (size.getHeight() - this.getHeight() / 2);/ 初始化窗口面板this.initial();/ 設置窗體為不可改動大小this.setResizable(false);/ 設置窗體為顯示this.setVisible(true); /初始化窗體上的面板public void initial() JPanel panel = new JPanel();panel.
20、setLayout(new GridBagLayout(); / 設置一切控件所占網(wǎng)格的大小,位置,座標,對齊方式 panel.add(new JScrollPane(new JTextArea(), new GBC(0, 0, 2, 3).setWeight(10, 0).setFill(GBC.BOTH);panel.add(new JButton(按鈕一), new GBC(2, 0, 2, 1).setWeight(5, 0).setFill(GBC.BOTH);panel.add(new JButton(按鈕二), new GBC(2, 1).setWeight(5, 0).setF
21、ill(GBC.BOTH);panel.add(new JButton(按鈕三), new GBC(3, 1).setWeight(5, 0).setFill(GBC.BOTH);panel.add(new JComboBox(new String 組合框 ), new GBC(2, 2, 2, 1).setWeight(5, 0).setFill(GBC.BOTH);panel.add(new JTextField(單行文本框), new GBC(0, 3, 4, 1).setWeight(5, 0).setFill(GBC.BOTH);this.setLayout(new BorderLa
22、yout();this.getContentPane().add(panel, BorderLayout.CENTER); public static void main(String args) / 匿名內部類,為窗口注冊事件new GridBagLayoutTest().addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0);); 2.相關知識GridBagLayout類 方法方法GridBagLayout()void setConstraints(Compo
23、nentcomp, GridBagConstraintsconstraints)主要功能主要功能創(chuàng)建網(wǎng)格袋規(guī)劃管理器設置此規(guī)劃中指定組件的約束條件GridBagConstraints類 方法方法主要功能主要功能GridBagConstraints() 創(chuàng)建一個 GridBagConstraint 對象,將其一切字段都設置為默許值GridBagConstraints(intgridx, intgridy, intgridwidth, intgridheight, doubleweightx, doubleweighty, intanchor, intfill, Insetsinsets, int
24、ipadx, intipady)創(chuàng)建一個 GridBagConstraints 對象,將其一切字段都設置為傳入?yún)?shù)成員變量成員變量含義含義static int RELATIVE指定此組件為其行或列中的倒數(shù)第二個組件,或者讓此組件緊跟在以前添加的組件之后s t a t i c i n t REMAINDER指定此組件是其行或列中的最后一個組件int fill當組件的顯示區(qū)域大于它所懇求的顯示區(qū)域的大小時運用此字段int gridheight指定在組件顯示區(qū)域的一列中的單元格數(shù)int gridwidth指定組件顯示區(qū)域的某一行中的單元格數(shù)int gridx指定包含組件的顯示區(qū)域開場邊的單元格,其中
25、行的第一個單元格為 gridx=0int gridy指定位于組件顯示區(qū)域的頂部的單元格,其中最上邊的單元格為 gridy=0成員變量成員變量含義含義int anchor當組件小于其顯示區(qū)域時運用此字段。它可以確定在顯示區(qū)域中放置組件的位置 double weightx指定如何分布額外的程度空間 double weighty指定如何分布額外的垂直空間。 Insets insets指定組件的外部填充,即組件與其顯示區(qū)域邊緣之間間距的最小量。 int ipadx指定組件的內部填充,即給組件的最小寬度添加多大的空間。 int ipady指定組件的內部填充,即給組件的最小高度添加多大的空間。 fill NONE:不調整組件大小。 HORIZONTAL:加寬組件,使它在程度方向上得以填滿其顯示區(qū)域,但是不改動高度。 VERTICAL:加高組件,使它在垂直方向上得以填滿其顯示區(qū)域,但是不改動寬度。 BOTH:使組件完全填滿其顯示區(qū)域 anchor 能夠的值有兩種:相對和絕對 相對值的解釋是相對于容器的組件方向屬性 PAGE_START、PAGE_END、LINE_START、LINE_END、FIRST_LINE_START、F
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人教版七年級數(shù)學下冊8.2.1.2《代入消元法(2)》聽評課記錄
- 新版華東師大版八年級數(shù)學下冊《18平行四邊形》聽評課記錄30
- 珍稀郵票贈與合同(2篇)
- 生物識別技術開發(fā)合同(2篇)
- 蘇人版道德與法治九年級上冊2.1《參與公共生活》聽課評課記錄
- 《青銅器與甲骨文》聽課評課記錄2(新部編人教版七年級上冊歷史)
- 晉教版地理七年級下冊《9.1 東南亞──兩洲兩洋的“十字路口”》聽課評課記錄2
- 蘇科版數(shù)學七年級上冊第四章 一元一次方程-應用教聽評課記錄
- 湘教版數(shù)學八年級下冊2.2.2《平行四邊形的判定定理》聽評課記錄2
- 湘教版九年級數(shù)學上冊第1章反比例函數(shù)1.3反比例函數(shù)的應用聽評課記錄
- 5《這些事我來做》(說課稿)-部編版道德與法治四年級上冊
- 2025年度高端商務車輛聘用司機勞動合同模板(專業(yè)版)4篇
- 2025年福建福州市倉山區(qū)國有投資發(fā)展集團有限公司招聘筆試參考題庫附帶答案詳解
- 2025年人教版新教材數(shù)學一年級下冊教學計劃(含進度表)
- GB/T 45107-2024表土剝離及其再利用技術要求
- 2025長江航道工程局招聘101人歷年高頻重點提升(共500題)附帶答案詳解
- 2025年黑龍江哈爾濱市面向社會招聘社區(qū)工作者1598人歷年高頻重點提升(共500題)附帶答案詳解
- 2025年國新國際投資有限公司招聘筆試參考題庫含答案解析
- 2025年八省聯(lián)考四川高考生物試卷真題答案詳解(精校打印)
- 《供電營業(yè)規(guī)則》
- 執(zhí)行總經(jīng)理崗位職責
評論
0/150
提交評論