Java17第十四章2-界面设计swing.ppt

上传人:max****ui 文档编号:6358020 上传时间:2020-02-23 格式:PPT 页数:49 大小:7.46MB
返回 下载 相关 举报
Java17第十四章2-界面设计swing.ppt_第1页
第1页 / 共49页
Java17第十四章2-界面设计swing.ppt_第2页
第2页 / 共49页
Java17第十四章2-界面设计swing.ppt_第3页
第3页 / 共49页
点击查看更多>>
资源描述
第十四章2 图形用户界面设计 回顾与作业点评 事件处理机制布局设计 本章任务 掌握Swing容器和组件 知识要点 Swing容器和组件 14 1抽象窗口工具集 AWT 用于设计与用户交互的图形界面14 1 1AWT组件和容器 AWT AbstractWindowingToolkit 中文译为抽象窗口工具包 是Java提供的用来建立和设置Java的图形用户界面的基本工具 AWT由Java中的java awt包提供 里面包含了许多可用来建立与平台无关的图形用户界面 GUI 的类 这些类又被称为组件 components 1 Component和Containercomponent是一个具有图形表示能力的对象 可在屏幕上显示 并可与用户进行交互 典型图形用户界面中的按钮 复选框和滚动条都是组件示例 Container 一般的AWT容器对象 是一个可包含其他AWT组件的组件 14 2Swingj介绍Swing是基于AWT的一组GUI组件的集合 其组件与操作系统无关 引入了许多新的特性和能力 1 JFrame类2 基本图形组件类JPanel JButton等14 3Swing组件应用1 JOptionPane组件 是标准对话框 Swing是在AWT基础上开发出的图形界面包 14 3 2菜单组件JMenuBar 菜单栏JMenu 菜单JMenuItem 菜单项JChecBoxMenuItem 带复选框的菜单项组件MyNotedpad javaJPopupMenu 弹出式菜单组件 importjava awt Color importjavax swing importjava awt event MouseAdapter importjava awt event MouseEvent publicclassPopupMenuTest staticJPopupMenupopup null publicstaticvoidmain String args JFrameframe newJFrame frame setBounds 200 300 200 300 frame setTitle 我的第一个小板凳 设置不允许调整窗口的大小frame setResizable false frame setBackground Color ORANGE popup newJPopupMenu popup add 徐明华 popup add 张燕君 popup add 徐诗佳 popup add 王子昊 popup setName 右击菜单 frame add popup 在窗体上加右击菜单frame addMouseListener newMouseAdapter publicvoidmouseReleased MouseEvente if e getButton MouseEvent BUTTON3 popup show e getComponent e getX e getY 添加事件监听器frame setVisible true 显示窗体 2 JColorChooser 颜色选择器 用于允许用户操作和选择颜色的控制器窗格 importjava awt importjava awt event importjavax swing publicclassJColorChooserDemoimplementsActionListener publicstaticvoidmain String args JFrameframe newJFrame JColorChooserDemo frame setDefaultCloseOperation JFrame EXIT ON CLOSE MyPanelpanel newMyPanel Panel面板对象frame getContentPane add panel frame pack 自动调整大小frame show 窗体显示 publicvoidactionPerformed ActionEvente classMyPanelextendsJPanelimplementsActionListener privateJButtonbutton rgb red green blue ButtonprivateColorcolor newColor 0 0 0 颜色对象publicMyPanel button newJButton GetColor rgb newJButton RGB red newJButton Red green newJButton Green blue newJButton Blue 面板上有五个按钮button addActionListener this 添加事件setPreferredSize newDimension 550 250 setLayout newFlowLayout FlowLayout CENTER 5 5 setBackground color 设置背景色add button add rgb add red 添加到面板上add green add blue publicvoidactionPerformed ActionEvente color JColorChooser showDialog this ChooseColor color setBackground color 从新设置背景button setText Getagain 重新设置button显示字rgb setText RGB color getRGB 设置得到RGBred setText Red color getRed 设置得到的红色green setText Green color getGreen blue setText Blue color getBlue 3 界面基本组件JLabelJTextFieldJPasswordFieldJTextieldJTextAreaJButtonJCheckBoxJRadioButtonJComboxJProgresBarSwingReigester java4 JProgresBar进度条 importjava awt BorderLayout importjava awt event ActionEvent importjava awt event ActionListener importjavax swing classBarThreadextendsThread privateintDELAY 100 privateJProgressBarprogressBar 进度条类privateJButtonbutton privatebooleanflag true publicBarThread JProgressBarbar JButtonbutton progressBar bar this button button publicvoidrun 线程体button setEnabled false intmaximum progressBar getMaximum 最大值while flag try Thread sleep DELAY 休眠100 catch InterruptedExceptionignoredException progressBar setValue progressBar getValue 1 if progressBar getValue maximum 如果拿到值 最大值flag false button setEnabled true publicclassJProgressBarTest publicstaticvoidmain Stringargs finalJProgressBaraJProgressBar newJProgressBar 0 100 aJProgressBar setStringPainted true finalJButtonaJButton newJButton Start 按钮aJButton addActionListener newActionListener publicvoidactionPerformed ActionEvente if aJProgressBar getValue aJProgressBar getMaximum aJProgressBar setValue 0 事件如果达到最大值 Threadstepper newBarThread aJProgressBar aJButton stepper start JFrameframe newJFrame ProgressBars frame setDefaultCloseOperation JFrame EXIT ON CLOSE frame add aJProgressBar BorderLayout NORTH frame add aJButton BorderLayout SOUTH frame setSize 300 100 frame setVisible true 5JToolBar工具条 importjava awt BorderLayout URL importjavax swing publicclassJToolBarTest publicstaticvoidmain String args JToolBarbar newJToolBar JButtonbutton1 button2 button3 URLurl Thread currentThread getContextClassLoader getResource left jpg URLurl2 Thread currentThread getContextClassLoader getResource center jpg URLurl3 Thread currentThread getContextClassLoader getResource right jpg button1 newJButton newImageIcon url button1 setToolTipText 向前 button2 newJButton newImageIcon url2 button2 setToolTipText 向上 button3 newJButton newImageIcon url3 button3 setToolTipText 向后 bar add button1 bar add button2 bar add button3 bar setFloatable true JFrameframe newJFrame frame setTitle JToolBar工具条 frame add bar BorderLayout NORTH frame add newJTextArea BorderLayout CENTER frame setSize 400 400 frame setDefaultCloseOperation JFrame EXIT ON CLOSE frame setVisible true publicclassSwingReigsterextendsJFrame publicSwingReigster this init publicvoidinit this setTitle 用户注册 this setBounds 100 100 340 500 this createUI 改变感观 try UIManager setLookAndFeel newWindowsClassicLookAndFeel catch UnsupportedLookAndFeelExceptione TODOAuto generatedcatchblock e printStackTrace this setVisible true this setDefaultCloseOperation EXIT ON CLOSE publicvoidcreateUI JPanelpanel newJPanel 添加边框Borderborder BorderFactory createEtchedBorder EtchedBorder LOWERED TitledBordertBorder BorderFactory createTitledBorder border 注册面板 TitledBorder CENTER TitledBorder TOP panel setBorder tBorder panel setLayout null this add panel 名JLabelnameLbl newJLabel 输入姓名 nameLbl setBounds 10 50 60 25 panel add nameLbl JTextFieldnameTxt newJTextField nameTxt setBounds 80 50 120 22 panel add nameTxt 密码JLabelpwdLbl newJLabel 输入密码 pwdLbl setBounds 10 80 60 25 panel add pwdLbl JPasswordFieldpwd newJPasswordField pwd setBounds 80 80 120 22 panel add pwd 性别JLabelgenderLbl newJLabel 性别 genderLbl setBounds 10 110 60 25 panel add genderLbl ButtonGroupgroup newButtonGroup JRadioButtonfRdo newJRadioButton 男 true fRdo setBounds 80 110 50 25 group add fRdo JRadioButtonmRdo newJRadioButton 女 false mRdo setBounds 140 110 50 25 group add mRdo panel add fRdo panel add mRdo 学历JLabelediLbl newJLabel 学历 ediLbl setBounds 10 140 60 25 panel add ediLbl 下拉列表 JComboBoxedicbo newJComboBox edicbo addItem 高中 edicbo addItem 大专 edicbo addItem 本科 edicbo addItem 其它 edicbo setBounds 80 140 80 22 panel add edicbo 爱好JLabelhobbyLbl newJLabel 爱好 hobbyLbl setBounds 10 170 60 25 panel add hobbyLbl 复选框JCheckBoxhobbyChk newJCheckBox 睡觉 hobbyChk setBounds 80 170 60 25 panel add hobbyChk JCheckBoxhobbyChk2 newJCheckBox 运动 true hobbyChk2 setBounds 150 170 60 25 panel add hobbyChk2 JCheckBoxhobbyChk3 newJCheckBox 爬山 hobbyChk3 setBounds 220 170 60 25 panel add hobbyChk3 自我介绍 JLabelintrLbl newJLabel 自我介绍 intrLbl setBounds 10 200 60 25 panel add intrLbl Swing中 文本域默认是不带滚动条的 必须放置在JScrollPane中 才会滚动条JTextAreaintrArea newJTextArea JScrollPanescrollPane newJScrollPane intrArea JScrollPane VERTICAL SCROLLBAR AS NEEDED JScrollPane HORIZONTAL SCROLLBAR AS NEEDED scrollPane setBounds 20 230 240 110 panel add scrollPane 放置图片JLabelimgLbl newJLabel imgLbl setBounds 5 350 324 47 panel add imgLbl URLurl Thread currentThread getContextClassLoader getResource logo2 png URLurl this getClass getResource 517 jpg Iconicon newImageIcon url imgLbl setIcon icon 按钮JButtonbtn newJButton 提交 btn setBounds 50 420 70 22 btn addActionListener newActionListener publicvoidactionPerformed ActionEventarg0 参数1 父组件参数2 提示信息参数3 标题信息参数4 指定按钮类型参数5 指定图标类型返回值 户所选选项的整数 intoption JOptionPane showConfirmDialog null 你确定提交吗 提交提示 JOptionPane OK CANCEL OPTION JOptionPane WARNING MESSAGE if option JOptionPane OK OPTION System out println ok submit else System out println no cancel panel add btn JButtonbtn2 newJButton 重置 btn2 setBounds 150 420 70 22 panel add btn2 btn2 addActionListener newActionListener publicvoidactionPerformed ActionEventarg0 JFileChooserchooser newJFileChooser FileNameExtensionFilterfilter newFileNameExtensionFilter JPG 2D图形的绘制 importjava awt BorderLayout importjava awt Color importjava awt Container importjava awt Graphics importjava awt Graphics2D importjava awt Shape importjava awt geom AffineTransform importjava awt geom Arc2D importjava awt geom CubicCurve2D importjava awt geom Ellipse2D importjava awt geom Line2D importjava awt geom QuadCurve2D importjava awt geom Rectangle2D importjava awt geom RoundRectangle2D importjavax swing JFrame importjavax swing JPanel publicclassGraphicsShape2DDemoextendsJFrame 主窗口类GraphicsShape2DDemo super 常用图形的绘制与填充 调用父类构造器设置窗口标题栏DrawPaneldrawPanel newDrawPanel 创建DrawPanel对象用于绘制图形Containercontent getContentPane 获得窗口的内容窗格content add drawPanel BorderLayout CENTER 把对象drawPanel加入内容窗格setSize 400 300 设置窗口大小setVisible true 设置窗口可视setDefaultCloseOperation JFrame EXIT ON CLOSE 关闭窗口时退出程序 publicstaticvoidmain String args newGraphicsShape2DDemo 创建GraphicsShapeDemo对象 显示图形的面板classDrawPanelextendsJPanel 重载paintComponent 方法publicvoidpaintComponent Graphicsg super paintComponent g 调用父类的绘制组件方法Graphics2Dg2D Graphics2D g setBackground Color white setForeground Color black intcharH 16 最大字符高度intgridW getWidth 5 intgridH getHeight 4 绘图网格高度intposX 2 各图形绘制位置的x坐标intposY 2 各图形位置的y坐标intstrY gridH 7 字符串绘制位置的y坐标intw gridW 2 posX 图形的宽度inth strY charH posY 图形的高度intcirlceD Math min w h 圆的直径Shape shape newShape 2 5 shape 0 0 newLine2D Float 0 0 w h 直线shape 0 1 newRectangle2D Double 0 0 w h 矩形shape 0 2 newRoundRectangle2D Float 0 0 w h 20 20 圆角矩形shape 0 3 newEllipse2D Float 0 0 cirlceD cirlceD 圆shape 0 4 newEllipse2D Float 0 0 w h 椭圆shape 1 0 newArc2D Float 0 0 w h 45 225 Arc2D OPEN 开弧shape 1 1 newArc2D Float 0 0 w h 45 225 Arc2D CHORD 弓形shape 1 2 newArc2D Float 0 0 w h 45 225 Arc2D PIE 饼形shape 1 3 newQuadCurve2D Double 0 0 w h 6 w h 二次曲线shape 1 4 newCubicCurve2D Double 0 0 w 2 h w h 2 w h 三次曲线 绘制几何图形的名称String shapeName 直线 矩形 圆角矩形 圆 椭圆 开弧 弓形 饼形 二次曲线 三次曲线 AffineTransformdefaultAT g2D getTransform for inti 0 i shapeName length i for intj 0 j shape i length j g2D setColor Color black g2D translate posX posY 坐标平移g2D draw shape i j g2D setColor Color blue g2D drawString shapeName i j 2 strY 绘制说明文字g2D setTransform defaultAT posY gridH g2D translate posX posY g2D setColor Color pink g2D fill shape i j g2D setColor Color blue g2D drawString 填充 shapeName i j 2 strY 文字g2D setTransform defaultAT posX gridW posY gridH posX 2 posY 2 gridH
展开阅读全文
相关资源
相关搜索

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


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

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


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