Java图形用户界面第7章.ppt

上传人:max****ui 文档编号:6359914 上传时间:2020-02-23 格式:PPT 页数:88 大小:436.55KB
返回 下载 相关 举报
Java图形用户界面第7章.ppt_第1页
第1页 / 共88页
Java图形用户界面第7章.ppt_第2页
第2页 / 共88页
Java图形用户界面第7章.ppt_第3页
第3页 / 共88页
点击查看更多>>
资源描述
第7章 图形用户界面程序设计 本章主要内容 图形用户界面 GUI Java的基本绘图Swing组件设计布局管理器 一 图形用户界面 GUI GUI是用图形的方式 借助于菜单 按钮等标准界面元素和鼠标操作 帮助用户方便地向计算机系统发出指令 启动操作 并将系统运行的结果也以同样的方式显示给用户的技术 AWT AWT AbstractWindowToolkit 译为抽象窗口工具包 它是Java语言为建立图形用户界面提供的API 其主要功能包括 用户界面组件事件处理模型图形和图像工具布局管理器 Swing Java最初作为网络编程工具 提供的AWT功能比较弱 只能用来支持必要的用户界面组件 随着Java被越来越多的业内人士认可 应用范围的迅猛扩展 Sun公司意识到需要对AWT的设计进行修改 以适应于更加广泛的开发目的 于是就诞生了Swing Swing是继AWT之后 更加丰富 功能更加强大的GUI工具包 它构成了JFC JavaFoundationClass 的图形用户界面功能的核心部分 Swing与AWT的主要区别 AWT是基于同位体 Peer 的体系结构 这种设计策略严重限制了用户界面中可以使用的组件种类及功能 成为一个致命的缺憾 而Swing不需要本地提供同位体 这样可以给设计者带来更大的灵活性 有利于增强组件的功能 在AWT中 有一部分代码是用C编写的 而Swing是100 的纯Java 增强了应用程序的与环境无关性 Swing与AWT的主要区别 Swing具有控制外观 Pluggablelookandfeel 的能力 即允许用户自行定制桌面的显示风格 比如 更换配色方案 让窗口系统更加适应用户的习惯和需要 而AWT组件完全依赖于本地平台 增加了裁剪板 鼠标提示 拖放和打印等功能 与图形用户界面有关的包 java awt与绘图相关的类java event与事件处理相关的类javax swing与Swing组件相关的类 二 Java的基本绘图 绘图类Graphics颜色类Color字体类Font 1 绘图类Graphics Java的图形处理主要由AWT中的Graphics类实现 该类支持两种类型的绘图功能 利用绘图成员方法绘图和显示图象 Graphics是一个抽象类 在这个类中封装了所有的绘图操作 Graphics类中的绘图成员方法 voiddrawLine intstartX intstartY intendX intendY voiddrawRect intx inty intwidth intheight voidfillRect intx inty intwidth intheight voiddrawRoundRect intx inty intwidth intheight intarcWidth intarcHeight voidfillRoundRect intx inty intwidth intheight intarcWidth intarcHeight voiddraw3DRect intx inty intwidth intheight booleanraised voidfill3DRect intx inty intwidth intheight booleanraised voiddrawOval intx inty intwidth intheight voidfillOval intx inty intwidth intheight Graphics类中的绘图成员方法 voiddrawArc intx inty intwidth intheight intstartAngle intendAngle voidfillArc intx inty intwidth intheight intstartAngle intendAngle voiddrawString Stringstr intx inty voiddrawPloygon int xPoints int yPoints intnumPoints voidfillPloygon int xPoints int yPoints intnumPoints 利用Graphics类提供的成员方法绘图的基本步骤 创建绘图类对象调用Graphics提供的成员方法绘图 创建绘图类对象的方法 在Swing组件中 通过paintComponent 成员方法的参数获得绘图类对象利用组件的getGraphics 成员方法显式地创建绘图对象 并用dispose 释放 importjava awt importjavax swing publicclassGraphicsDemoextendsJPanel publicstaticvoidmain String agrs JFramefrm newJFrame 绘图应用范例 frm getContentPane add newGraphicsDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 380 200 frm setVisible true publicvoidpaintComponent Graphicsg 刷新面板时 系统自动调用这个成员方法super paintComponent g g drawString 绘图对象 5 40 g drawLine 5 50 150 50 g drawRect 5 55 50 50 g fillRect 5 110 50 50 g drawRoundRect 60 55 50 50 30 30 g fillRoundRect 60 110 50 50 30 30 g drawOval 115 55 50 50 g fillOval 115 110 50 50 g drawArc 100 5 100 100 0 45 g fillArc 155 5 100 100 0 45 intx vals 170 170 210 250 210 inty vals 60 100 100 80 60 g drawPolygon x vals y vals x vals length for inti 0 i 5 i y vals i 55 g fillPolygon x vals y vals x vals length g fill3DRect 260 5 100 70 true g fill3DRect 260 90 100 70 false 2 2D绘图方式 利用Graphics类中提供的方法实现绘图 其功能十分有限 从JDK1 2版本以后 Java提供了一种2D库 它体现了面向对象设计方法的思想 其中 包含了强大的绘图功能 Graphics与Graphics2D的主要区别 Graphics类以成员方法的形式封装了绘制各种图形的工具 而Graphics2D将绘制的所有图形用相应的类对象标识 这是它们两个的主要区别 与Graphics相比 Graphics2D提供的几何图形控制 坐标转换 颜色管理和文本布局的功能更加强大 使用起来更加灵活 Graphics2D类 利用Java2D绘制图形时所需要的绘图工具都包含在Graphics2D类对象中 通常将此称为图形设备文本 graphicscontext 无论希望在组件表面绘制什么图形 都要拥有一个图形设备文本对象 通过它实现绘制直线 曲线 各种几何图形 填充几何图形等一系列绘图操作 获取Grphics2D对象的方式 Graphics2D是Graphics的子类 它们都是抽象类 因此不能够直接创建这两个类的对象 它们的创建过程完全由相应的组件控制 在Swing组件中 通过paintComponent 成员方法的参数获得绘图类对象 Graphics2D类中提供的成员方法 draw Shapeshape 使用图形设备文本的当前属性绘制由shape带入的图形 fill Shapeshape 使用图形设备文本的当前属性填充由shape带入的图形 drawString Stringtext 使用图形设备文本的当前属性绘制文本 drawImage 使用图形设备文本的当前属性显示图象 在java awt geom包中定义了一系列实现Shape接口的几何图形类 Line2D这是一个抽象类 定义了由两个端点确定的直线 在这个类中包含了两个内部类 Line2D Float和Line2D Double 它们分别实现用float和double类型的用户坐标定义直线端点的功能 Ellipse2D这是一个定义椭圆的抽象类 其中包含了两个内部类 Ellipse2D Float和Ellipse2D Double 它们分别实现用float和double类型的数值描述用户坐标点的功能 椭圆由左上角位置和封闭它的矩形宽度和高度确定 Graphics2D应用举例 importjava awt importjava awt geom importjavax swing publicclassDrawTest publicstaticvoidmain String args DrawFrameframe newDrawFrame frame setDefaultCloseOperation JFrame EXIT ON CLOSE frame show importjava awt importjava awt geom importjavax swing publicclassDrawFrameextendsJFrame publicDrawFrame setTitle DrawTest setSize DEFAULT WIDTH DEFAULT HEIGHT DrawPanelpanel newDrawPanel ContainercontentPane getContentPane contentPane add panel publicstaticfinalintDEFAULT WIDTH 400 publicstaticfinalintDEFAULT HEIGHT 400 importjava awt importjava awt geom importjavax swing publicclassDrawPanelextendsJPanel publicvoidpaintComponent Graphicsg Graphics2Dg2 Graphics2D g Rectangle2Drect newRectangle2D Double 100 100 200 150 g2 draw rect Ellipse2Dellipse newEllipse2D Double ellipse setFrame rect g2 draw ellipse g2 draw newLine2D Double 100 100 300 250 doublecenterX rect getCenterX doublecenterY rect getCenterY doubleradius 150 Ellipse2Dcircle newEllipse2D Double circle setFrameFromCenter centerX centerY centerX radius centerY radius g2 draw circle 3 颜色类 Java主要使用RGB模式描述颜色特性 R G B各用一个字节表示 取值范围为0 255 类库提供Color类描述颜色 Color类提供的静态常量Color red Color类提供的成员方法 Color red green blue publicColorbrighter publicColordarker publicintgetRed publicintgetGreen publicintgetBlue 设置颜色的成员方法 在Component类中 提供了设置组件前景颜色和背景颜色的成员方法 setForeground Colorcolor setBackground Colorcolor 在组件中绘图时 设置 获取绘图颜色的成员方法 setColor Colorcolor getColor Colorcolor importjavax swing importjava awt geom publicclassLineArtTest publicstaticvoidmain String args JFramefrm newJFrame LineArtDemo frm getContentPane add newLineArtDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 300 200 frm setVisible true 设置绘图颜色 publicclassLineArtDemoextendsJPanel privateintmNumberOfLines privateColormColor publicLineArtDemo this publicLineArtDemo Stringstr super mNumberOfLines 25 mColor newColor 3 mColor 0 newColor 255 0 0 mColor 1 newColor 0 255 0 mColor 2 newColor 0 0 255 publicvoidpaintComponent Graphicsg Dimensiond getSize super paintComponent g Graphics2Dg2 Graphics2D g for inti 0 i mNumberOfLines i doubleratio double i double mNumberOfLines g2 setColor mColor i mColor length g2 draw newLine2D Double 0 int ratio d height int ratio d width d height importjava awt importjava awt event importjavax swing importjava awt geom publicclassColorDemoextendsJPanel JPanelpanel JButtonbutton1 JButtonbutton2 Colorcolor publicColorDemo color Color orange setLayout newBorderLayout setPreferredSize newDimension 300 200 button1 newJButton 增量 button1 addActionListener newActionListener publicvoidactionPerformed ActionEvente color color brighter repaint button2 setEnabled true button1 setEnabled false 改变颜色明暗 button1 setEnabled false button2 newJButton 减暗 button2 addActionListener newActionListener publicvoidactionPerformed ActionEvente color color darker repaint button1 setEnabled true button2 setEnabled false panel newJPanel panel setBackground Color darkGray panel add button1 panel add button2 add panel BorderLayout SOUTH publicvoidpaintComponent Graphicsg super paintComponent g g setColor color g fillRect 10 10 this getWidth 20 this getHeight panel getHeight 20 publicstaticvoidmain Stringagrs JFramefrm newJFrame 颜色应用范例 frm getContentPane add newColorDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 300 200 frm setVisible true 4 字体类 在AWT中有两个与字体相关的类 Font 字体类 FontMetrics 字体信息类 决定字体外观的三个属性 字体 字体风格 字体大小 设置字体的基本步骤 创建Font对象调用组件中的setFont 成员方法设置字体 Font构造方法的格式 Font 字体名 字体风格 字体大小 publicstaticfinalintPLAIN 0 粗体publicstaticfinalintBOLD 1 斜体publicstaticfinalintITALIC 2 普通 importjava awt importjavax swing publicclasssetFontDemoextendsJPanel publicvoidpaintComponent Graphicsg super paintComponent g Graphics2Dg2 Graphics2D g Fontf1 newFont TimesNewRoman Font BOLD 16 Fontf2 newFont 黑体 Font PLAIN 20 Fontf3 newFont 楷体 GB2312 Font ITALIC 24 setSize 200 160 g2 setFont f1 g2 drawString TimesRomanBOLD24 10 40 g2 setFont f2 g2 drawString 黑体PLAIN36 10 90 g2 setFont f3 g2 drawString 楷体 GB2312ITALIC48 10 130 publicstaticvoidmain String args JFramefrm newJFrame 字体设置范例 frm getContentPane add newsetFontDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 380 200 frm setVisible true 三 Swing组件 Swing组件包含容器和普通组件 容器 用来放置其他组件的组件组件 放置在容器中的用户界面元素在Java语言中 所有的容器和组件都用相应的类描述 因此 在设计图形用户界面之前一定要充分地了解相关的类定义 1 容器 常用的Swing容器主要有两类 顶层容器 包含其他组件和容器的容器 每个应用程序都应该有一个顶层容器 主要有 JApplet JFrame和JDialog 通用容器 指中间层容器 主要有JPanel JScrollPane和JToolBar 顶层容器的类结构 使用顶层容器需要注意的地方 每个GUI组件必须位于一个且仅一个容器中 每个顶层容器包含一个内容窗格 Contentpane 所有的组件必须放置在内容窗格中 可以在顶层容器中添加菜单栏 它将位于顶层窗格的约定位置 顶层容器 内容窗格和菜单的位置关系 JFrame容器 构造方法 JFrame JFrame Stringtitle 设置点击关闭窗口按钮的操作效果voidsetDefaultCloseOperation int DO NOTHING ON CLOSEHIDE ON CLOSEDISPOSE ON CLOSEEXIT ON CLOSE 设置窗口大小voidsetSize intwidth intheight voidsetSize Dimensionsize 获取内容窗格ContainergetContentPane 将窗口设置为最佳大小pack importjava awt importjava awt event importjavax swing publicclassTryJFrame publicstaticvoidmain Stringagrs JFrameframe newJFrame JFrame应用举例 frame setDefaultCloseOperation JFrame EXIT ON CLOSE JLabellabel newJLabel JFrame应用举例 label setPreferredSize newDimension 175 100 frame getContentPane add label BorderLayout CENTER frame pack frame setVisible true JPanel类 通用容器 JPanel容器又称为面板容器 其主要特点有 不能作为顶层容器 可以嵌套放置 组件直接放置在容器中 importjavax swing publicclassJPanelDemo publicJPanelDemo JButtonbutton newJButton Hello JPanelpanel newJPanel panel setBackground Color orange panel setPreferredSize newDimension 300 200 panel add button JFramefrm newJFrame JPanel应用范例 frm getContentPane add panel frm pack frm setVisible true publicstaticvoidmain String args JPanelDemopanelDemo newJPanelDemo 2 Swing组件 JComponent是所有Swing组件的父类 它增加了以下功能 支持组件的可插入观感支持工具提示当组件被拖拽时 支持列表或树的自动滚动支持扩展组件类 以创建自己需要的组件 1 标签组件 JLabel 标签组件的主要用途是显示提示信息 JLabel即可以显示文字 也可以显示图标 publicJLabel Stringtext Iconicon inthorizontalAlignment publicJLabel Stringtext inthorizontalAlignment publicJLabel Stringtext publicJLabel Iconimage inthorizontalAlignment publicJLabel Iconimage publicJLabel publicStringgetText publicvoidsetText Stringtext publicIcongetIcon publicvoidsetIcon Iconicon publicintgetVerticalAlignment publicvoidsetVerticalAlignment intalignment publicintgetHorizontalAlignment publicvoidsetHorizontalAlignment intalignment publicintgetVerticalTextPosition publicvoidsetVerticalTextPosition inttextPosition publicintgetHorizontalTextPosition importjava awt importjavax swing publicclassJLabelDemoextendsJFrame publicJLabelDemo super 标签组件应用范例 Iconicon newImageIcon map gif JLabeljLabel1 newJLabel 标签组件1 JLabeljLabel2 newJLabel 标签组件2 icon SwingConstants LEFT JLabeljLabel3 newJLabel jLabel3 setText 标签组件3 jLabel3 setIcon icon jLabel3 setHorizontalTextPosition SwingConstants CENTER jLabel3 setVerticalTextPosition SwingConstants BOTTOM jLabel3 setToolTipText Label3 ContainercontentPane getContentPane contentPane setLayout newFlowLayout contentPane add jLabel1 contentPane add jLabel2 contentPane add jLabel3 setSize 200 100 show publicstaticvoidmain String args JLabelDemofrm newJLabelDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 2 按钮组件 复选框 单选框 按钮 使用按钮组件的基本步骤 创建按钮对象将按钮对象添加到容器中设置响应单击按钮事件的操作 主要的成员方法 booleanisSelected voidsetSelected booleanb StringgetText voidsetText Stringtext IcongetIcon voidsetIcon Iconicon IcongetDisableIcon voidsetDisabledIcon Iconicon JButton JButton类定义了最常见的按钮 用来响应用户的某项操作请求 JButton JButton Iconicon JButton Stringtext isDefaultButton setDefaultButton Buttondefault importjava awt importjava awt event importjavax swing AbstractButton importjavax swing publicclassTryButtonextendsJPanelimplementsActionListener protectedJButtonb1 b2 b3 publicTryButton b1 newJButton Disablemiddlebutton b1 setVerticalTextPosition AbstractButton CENTER b1 setHorizontalTextPosition AbstractButton CENTER b1 setMnemonic KeyEvent VK D b1 setActionCommand disable b2 newJButton Middlebutton b2 setVerticalTextPosition AbstractButton BOTTOM b2 setHorizontalTextPosition AbstractButton CENTER b2 setMnemonic KeyEvent VK M b3 newJButton Enablemiddlebutton b3 setMnemonic KeyEvent VK E b3 setActionCommand enable b3 setEnabled false b1 addActionListener this b3 addActionListener this b1 setToolTipText Todisablethemiddlebutton b2 setToolTipText Doesnothingwhenyouclickit b3 setToolTipText Enablethemiddlebutton add b1 add b2 add b3 publicvoidactionPerformed ActionEvente if disable equals e getActionCommand b2 setEnabled false b1 setEnabled false b3 setEnabled true else b2 setEnabled true b1 setEnabled true b3 setEnabled false publicstaticvoidmain String args JFrameframe newJFrame TryButton frame setDefaultCloseOperation JFrame EXIT ON CLOSE TryButtonnewContentPane newTryButton frame setContentPane newContentPane frame pack frame setVisible true JCheckBox 在Swing中 复选按钮用JCheckBox类实现 与JButton相同 可以为之设置文本串和图像 通常 将多个复选按钮组合成一组 对于处于一组中的复选按钮 每一时刻既可以选择一项 又可以选择多项 JRadioButton组件 JRadioButton JRadioButton Icon JRadioButton String JRadioButton String Icon JRadioButton Icon boolean JRadioButton String boolean JRadioButton String Icon boolean 通常与ButtonGroup结合使用 3 文本框 文本框是接收用户输入的一种组件 主要的成员方法 StringgetText voidsetText Stringtext booleanisEditable voidsetEditable booleaneditable intgetColumns voidsetColumns intcol importjava awt importjava awt event importjavax swing publicclassJTextFieldDemoextendsJFrame JTextFieldjText JPasswordFieldjPassword publicJTextFieldDemo JLabeljLabel1 newJLabel 用户 JLabeljLabel2 newJLabel 密码 jText newJTextField 20 jPassword newJPasswordField 20 JButtonjButton newJButton 确定 jButton addActionListener newActionListener publicvoidactionPerformed ActionEvente jButton actionPerformed e JPanelcontentPane JPanel getContentPane contentPane setLayout newFlowLayout contentPane add jLabel1 contentPane add jText contentPane add jLabel2 contentPane add jPassword contentPane add jButton setSize 300 150 setTitle 文本输入框范例 show privatevoidjButton actionPerformed ActionEvente Stringuser jText getText Stringpword String copyValueOf jPassword getPassword JOptionPane showMessageDialog getParent 用户 user n 密码 pword publicstaticvoidmain String args JTextFieldDemofrm newJTextFieldDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 四 布局管理器 对于不同的操作平台 显示文字 图形的方式可能有所不同 如果显式地指定一个组件的绝对位置和大小 有可能结果不可预测 为此 Java语言提供了布局管理器来管理放置在容器内部的各组件大小及排列布局 利用setLayout 设置布局管理器每种容器都有一种默认的布局管理器若不使用布局管理器 setLayout NULL 常用的布局管理器 FlowLayout 流程式 布局管理器BorderLayout 边框式 布局管理器CardLayout 卡片式 布局管理器GridLayout 网格式 布局管理器 1 FlowLayout布局管理器 FlowLayout自左向右 自上向下地布置容器中所包含的GUI组件 JPanel默认的布局管理器就是FlowLayout构造方法FlowLayout FlowLayout intalign 默认中对齐FlowLayout intalign inthgap intvgap importjava awt importjava awt event importjavax swing publicclassFlowLayoutDemoextendsJFrame publicFlowLayoutDemo super FlowLayout布局管理器 ContainercontentPane getContentPane contentPane setLayout newFlowLayout contentPane add newJButton 1 contentPane add newJButton 2 contentPane add newJButton 3 contentPane add newJButton 4 contentPane add newJButton 5 contentPane add newJButton 6 contentPane add newJButton 7 contentPane add newJButton 8 setSize 320 240 setVisible true setResizable false publicstaticvoidmain String args FlowLayoutDemofrm newFlowLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 2 BorderLayout布局管理器 BorderLayout将整个容器划分为5个区域 North 上部 South 下部 East 右侧 West 左侧 和Center 中部 JFrame的内容窗格的默认布局管理器是BorderLayout 构造方法 BorderLayout BorderLayout inthgap intvgap 如果使用BorderLayout布局容器中的组件 需要明确地指出组件放置的位置add newButton Button BorderLayout SOUTH 否则默认为中间区域 importjava awt importjava awt event importjavax swing publicclassBorderLayoutDemoextendsJFrame publicBorderLayoutDemo super BorderLayout布局管理器 ContainercontentPane getContentPane contentPane add newJButton 南 BorderLayout SOUTH contentPane add newJButton 北 BorderLayout NORTH contentPane add newJButton 中间 BorderLayout CENTER contentPane add newJButton 西 BorderLayout WEST contentPane add newJButton 东 BorderLayout EAST setSize 320 240 setVisible true publicstaticvoidmain String args BorderLayoutDemofrm newBorderLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 3 CardLayout布局管理器 CardLayout布局管理器提供了一种基于卡片式的布局管理器 它并没有可视化地安排组件 它所产生的效果就像一副扑克牌一样叠在一起 每次显示时只显示最上面的一张牌 需要显示下一张时 只需把最上面的一张牌放到最底下即可 构造方法 CardLayout CardLayout inthgap intvgap 常用的几个方法voidfirst Container voidlast Container voidnext Container voidprevious Container importjava awt importjava awt event importjavax swing publicclassCardLayoutDemoextendsJFrame JPanelpnlCommandArea newJPanel JPanelpnlDisplayArea newJPanel CardLayoutcardlayout1 newCardLayout JButtonbtnFirst newJButton 第一个 JButtonbtnPrevious newJButton 前一个 JButtonbtnNext newJButton 后一个 JButtonbtnLast newJButton 最后一个 ContainercontentPane publicCardLayoutDemo super CardLayout布局管理器范例 contentPane getContentPane contentPane add pnlCommandArea BorderLayout NORTH contentPane add pnlDisplayArea BorderLayout CENTER pnlDisplayArea setLayout cardlayout1 JPanelpnlFirst newJPanel pnlFirst setBackground Color blue pnlDisplayArea add first pnlFirst pnlFirst add newJLabel ThisisthefirstPanel JPanelpnlSecond newJPanel pnlSecond setBackground Color red pnlDisplayArea add second pnlSecond pnlSecond add newJLabel ThisisthesecondPanel JPanelpnlThird newJPanel pnlThird setBackground Color yellow pnlDisplayArea add third pnlThird pnlThird add newJLabel ThisisthethirdPanel JPanelpnlFourth newJPanel pnlFourth setBackground Color green pnlDisplayArea add fourth pnlFourth pnlFourth add newJLabel ThisisthefourthPanel btnFirst addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e btnPrevious addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e btnNext addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e btnLast addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e pnlCommandArea add btnFirst pnlCommandArea add btnPrevious pnlCommandArea add btnNext pnlCommandArea add btnLast setSize 400 300 setVisible true publicstaticvoidmain String args CardLayoutDemofrm newCardLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE privatevoidprocessAction ActionEvente JButtonbtnEvent JButton e getSource if btnEvent equals btnFirst cardlayout1 first pnlDisplayArea elseif btnEvent equals btnLast cardlayout1 last pnlDisplayArea elseif btnEvent equals btnPrevious cardlayout1 previous pnlDisplayArea elseif btnEvent equals btnNext cardlayout1 next pnlDisplayArea 4 GridLayout布局管理器 GridLayout提供了一种基于网格的布局管理方式 网格的行数和列数可以在创建GridLayout对象时指定 另外 容器中每个组件将占据大小完全相同的一个网格 向网格中布局GUI组件有两种方法 使用默认的顺序采用add Componentcomp intindex 构造方法 GridLayout GridLayout introws intcols GridLayout introws intcols inthgap intvgap importjava awt importjava awt event importjavax swing publicclassGridLayoutDemoextendsJFrame publicGridLayoutDemo super GridLayout布局管理器应用范例 ContainercontentPane getContentPane contentPane setLayout newGridLayout 2 4 contentPane add newJButton 1 contentPane add newJButton 2 contentPane add newJButton 3 contentPane add newJButton 4 contentPane add newJButton 5 contentPane add newJButton 6 contentPane add newJButton 7 contentPane add newJButton 8 publicstaticvoidmain String args GridLayoutDemofrm newGridLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 320 240 frm setVisible true 不使用布局管理器 importjava awt importjava awt event importjavax swing publicclassNullLayoutDemoextendsJFrame JLabellblUserName newJLabel 用户名称 JTextFieldtxtUserName newJTextField JLabellblUserPasswd newJLabel 用户密码 JPasswordFieldtxtUserPasswd newJPasswordField JButtonbtnLogin newJButton 登录 JButtonbtnReset newJButton 重设 publicNullLayoutDemo super 不使用布局管理器的应用范例 ContainercontentPane getContentPane contentPane setLayout null lblUserName setBounds newRectangle 45 38 67 34 txtUserName setBounds newRectangle 115 39 210 33 lblUserPasswd setBounds newRectangle 43 86 66 26 txtUserPasswd setBounds newRectangle 115 84 210 33 btnLogin setBounds newRectangle 78 150 86 30 btnReset setBounds newRectangle 193 150 86 30 contentPane add lblUserName contentPane add txtUserName contentPane add lblUserPasswd contentPane add txtUserPasswd contentPane add btnLogin contentPane add btnReset publicstaticvoidmain String args NullLayoutDemofrm newNullLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 400 240 frm setVisible true 运行结果 上机作业 设计 计算器 界面设计 万年日历 界面
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!