




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
广州大学学生实验报告开课学院及实验室:计算机科学与工程实验室2014年11月14日学院计算机科学与教育软件学院年级/专业/班姓名学号实验课程名称Java语言成绩实验项目名称图形界面设计指导老师一、实验目的实验十 图形用户界面(1)1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。实验十一 图形用户界面(2)1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。二、实验器材MacBook Pro一台操作系统:OS X Yosemite编程软件:eclipse3、 实验要求实验十 图形用户界面(1)1. 理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。2. 掌握编写独立运行的窗口界面的方法。3. 了解Java Swing 组件的使用方法。4. 了解对话框的使用方法。实验十一 图形用户界面(2)1. 理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。2. 掌握编写独立运行的窗口界面的方法。3. 了解Java Swing 组件的使用方法。4. 了解对话框的使用方法。四、实验过程原始数据记录实验十 图形用户界面(1)1. 如下图所示,用了三个文本框,第一个文本框给用户输入商品单价,第二个则是给用户输入商品数量,第三个用于显示总金额。代码:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.text.BadLocationException;import javax.swing.text.Document;public class test_2_1_1 extends JFrame implements ActionListenerpublic static void main(String args) / TODO Auto-generated method stubtest_2_1_1 good = new test_2_1_1();JTextField goodPriceTF;JTextField goodNumTF;JTextField sumPriceTF;JLabel goodPriceTipsLabel;JLabel goodNumTipsLabel;Boolean canCal = false;public test_2_1_1()super(商品价格计算器);setSize(435, 135);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/ 设置JFrame是否可以改变大小。 setResizable(false); / JFrame打开后居中。 setLocationRelativeTo(getOwner(); FlowLayout flo = new FlowLayout();setLayout(flo);JLabel goodPriceLabel = new JLabel(商品价格,JLabel.RIGHT);JLabel goodNumLabel = new JLabel(商品数量,JLabel.RIGHT);JLabel sumPriceLabel = new JLabel(商品总额,JLabel.RIGHT);goodPriceTipsLabel = new JLabel(请输入价格,JLabel.RIGHT);goodNumTipsLabel = new JLabel(请输入数量,JLabel.RIGHT);goodPriceTF = new JTextField(20);goodPriceTF.getDocument().addDocumentListener(new DocumentListener() Overridepublic void removeUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText();Overridepublic void insertUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText();Overridepublic void changedUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText(););goodNumTF = new JTextField(20);goodNumTF.getDocument().addDocumentListener(new DocumentListener() Overridepublic void removeUpdate(DocumentEvent e)/ TODO Auto-generated method stubchangeTFTipsLabel(goodNumTipsLabel, goodNumTF.getText();Overridepublic void insertUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodNumTipsLabel, goodNumTF.getText();Overridepublic void changedUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodNumTipsLabel, goodNumTF.getText(););sumPriceTF = new JTextField(20);sumPriceTF.setEditable(false);JButton btn = new JButton(计算);btn.addActionListener(this);add(goodPriceLabel);add(goodPriceTF);add(goodPriceTipsLabel);add(goodNumLabel);add(goodNumTF);add(goodNumTipsLabel);add(sumPriceLabel);add(sumPriceTF);add(btn);setVisible(true);public void actionPerformed(ActionEvent event)if (event.getActionCommand().equals(计算)if (!canCal)showMessage(输入的数据不合法);return;double sum = Double.parseDouble(goodPriceTF.getText() * Double.parseDouble(goodNumTF.getText();sumPriceTF.setText(Double.toString(sum);/检测文本是否纯数字public Boolean isNumber(String s)if (s.length() = 0)return false;Boolean isNumber = true;char c = s.toCharArray();for (int i = 0;i s.length();i+)if (Character.isDigit(ci) = false)isNumber = false;break;return isNumber;/弹出警告public void showMessage(String s)JOptionPane.showMessageDialog(null, s, 警告, JOptionPane.PLAIN_MESSAGE);/修改文本提示labelpublic void changeTFTipsLabel(JLabel aLabel,String str)/if (isNumber(str) = false)canCal = false;aLabel.setText(X );else if (isNumber(goodPriceTF.getText() | isNumber(goodNumTF.getText()if (isNumber(goodPriceTF.getText() & isNumber(goodNumTF.getText()canCal = true;aLabel.setText( );else if (str.length() = 0)canCal = false;if (aLabel = goodPriceTipsLabel)aLabel.setText(请输入价格);elseaLabel.setText(请输入数量);运行结果:启动画面当输入的字符是数字,对应右边的框旁边会提示 ,如果输入非法字符,则会出现 X,此时点击计算,则会弹出错误警告框。输入正确数字后,点计算按钮,得到正确结果。2. 制作如下图所示的界面,当用户点击单选按钮时,会在一个标签上显示出当前所选定的数据库服务器类型。代码:import java.awt.EventQueue;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ButtonGroup;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JRadioButton;public class test_2_1_2 implements ActionListenerprivate JFrame frmAsdfasdf;JLabel label;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try test_2_1_2 window = new test_2_1_2();window.frmAsdfasdf.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the application. */public test_2_1_2() initialize();/* * Initialize the contents of the frame. */private void initialize() frmAsdfasdf = new JFrame();frmAsdfasdf.setTitle(数据库);frmAsdfasdf.setBounds(100, 100, 251, 301);frmAsdfasdf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frmAsdfasdf.getContentPane().setLayout(new GridLayout(0, 1, 0, 0);/创建Radio按钮JRadioButton rdbtnNewRadioButton_1 = new JRadioButton(SQL);frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton_1);rdbtnNewRadioButton_1.addActionListener(this);JRadioButton rdbtnNewRadioButton = new JRadioButton(Oracle);frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton);rdbtnNewRadioButton.addActionListener(this);JRadioButton rdbtnNewRadioButton_2 = new JRadioButton(SQLite);frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton_2);rdbtnNewRadioButton_2.addActionListener(this);/添加按钮ButtonGroup bg=new ButtonGroup();bg.add(rdbtnNewRadioButton);bg.add(rdbtnNewRadioButton_1);bg.add(rdbtnNewRadioButton_2);label = new JLabel(学习使用JRadioButton控件);frmAsdfasdf.getContentPane().add(label);public void actionPerformed(ActionEvent event)if (event.getSource() instanceof JRadioButton)label.setText(event.getActionCommand();运行结果:启动截图点击Oracle选项实验十一 图形用户界面(2)1. 创建如图所示的菜单代码:import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JMenu;import java.awt.Component;import javax.swing.Box;import javax.swing.JCheckBoxMenuItem;import javax.swing.JCheckBox;import javax.swing.JRadioButton;import javax.swing.JSeparator;public class test_2_2 private JFrame frmMenu;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try test_2_2 window = new test_2_2();window.frmMenu.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the application. */public test_2_2() initialize();/* * Initialize the contents of the frame. */private void initialize() frmMenu = new JFrame();frmMenu.setTitle(Menu菜单使用);frmMenu.setBounds(100, 100, 349, 219);frmMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JMenuBar menuBar = new JMenuBar();frmMenu.setJMenuBar(menuBar);JMenu mnNewMenu = new JMenu(File);menuBar.add(mnNewMenu);JMenuItem mntmNewMenuItem = new JMenuItem(New);mnNewMenu.add(mntmNewMenuItem);JMenuItem mntmNewMenuItem_1 = new JMenuItem(Open);mnNewMenu.add(mntmNewMenuItem_1);JMenuItem mntmNewMenuItem_2 = new JMenuItem(Close);mnNewMenu.add(mntmNewMenuItem_2);JMenu mnNewMenu_1 = new JMenu(Option);menuBar.add(mnNewMenu_1);JMenuItem mntmNewMenuItem_4 = new JMenuItem(Font.);mnNewMenu_1.add(mntmNewMenuItem_4);JMenu mnNewMenu_2 = new JMenu(Color.);mnNewM
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高速防盗应急预案
- 2025年洛阳保安考试题目及答案
- 桑植中考语文试卷及答案
- 学堂在线 大学生职业指导-就业指导 章节测试答案
- 极限挑战面试试题及答案
- 琴行培训合同(标准版)
- 人事保管合同(标准版)
- 定做橱柜合同(标准版)
- 内部劳务派遣合同(标准版)
- 福建省厦门英才学校2025-2026学年高一上学期第一次月考化学试卷(含解析)
- 2025年甘肃省天水市供热有限公司招聘12人考试历年参考题附答案详解
- 2025新疆医科大学第一附属医院招聘事业单位编制外工作人员(119人)考试参考题库及答案解析
- 四年级数学上册第1单元《 大数的认识 》作业设计
- (2025年)贵州省遵义市【辅警协警】笔试预测试题含答案
- 2025年建筑施工企业薪酬管理规定
- 妇科肿瘤影像学课件
- 客户开发情况汇报
- (高清版)DBJ∕T 13-318-2025 《建筑施工盘扣式钢管脚手架安全技术标准》
- 全国一等奖统编版语文三年级上册《小狗学叫》公开课课件
- 中国能源数据报告2025
- 育龄妇女生殖健康知识
评论
0/150
提交评论