版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、广州大学学生实验报告开课学院及实验室:计算机科学与工程实验室2014年11月14日学院计算机科学与教育软件学院年级/专业/班姓名学号实验课程名称Java语言成绩实验项目名称图形界面设计指导老师一、实验目的实验十 图形用户界面(1)1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。实验十一 图形用户界面(2)1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。二、实验器材MacBook P
2、ro一台操作系统:OS X Yosemite编程软件:eclipse3、 实验要求实验十 图形用户界面(1)1. 理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。2. 掌握编写独立运行的窗口界面的方法。3. 了解Java Swing 组件的使用方法。4. 了解对话框的使用方法。实验十一 图形用户界面(2)1. 理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。2. 掌握编写独立运行的窗口界面的方法。3. 了解Java Swing 组件的使用方法。4. 了解对话框的使用方法。四、实验过程原始数据记录实验十 图形用户界面(1)1. 如下图所示,用了三个文本框,
3、第一个文本框给用户输入商品单价,第二个则是给用户输入商品数量,第三个用于显示总金额。代码: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.swin
4、g.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 m
5、ethod 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);/ 设置J
6、Frame是否可以改变大小。 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(&q
7、uot;商品总额",JLabel.RIGHT);goodPriceTipsLabel = new JLabel("请输入价格",JLabel.RIGHT);goodNumTipsLabel = new JLabel("请输入数量",JLabel.RIGHT);goodPriceTF = new JTextField(20);goodPriceTF.getDocument().addDocumentListener(new DocumentListener() Overridepublic void removeUpdate(DocumentEv
8、ent 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
9、 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(goodNumTipsLa
10、bel, 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.getTex
11、t(););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);se
12、tVisible(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);
13、/检测文本是否纯数字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
14、(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() &a
15、mp;& isNumber(goodNumTF.getText()canCal = true;aLabel.setText(" ");else if (str.length() = 0)canCal = false;if (aLabel = goodPriceTipsLabel)aLabel.setText("请输入价格");elseaLabel.setText("请输入数量");运行结果:启动画面当输入的字符是数字,对应右边的框旁边会提示 ,如果输入非法字符,则会出现 X,此时点击计算,则会弹出错误警告框。输入正确数字后,点
16、计算按钮,得到正确结果。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.JRadioBu
17、tton;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); catc
18、h (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.setDefau
19、ltCloseOperation(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
20、 rdbtnNewRadioButton = new JRadioButton("Oracle");frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton);rdbtnNewRadioButton.addActionListener(this);JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("SQLite");frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton_2);rdbtnNewRadi
21、oButton_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
22、(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 ja
23、vax.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
24、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(&quo
25、t;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"
26、);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.");mnNewMenu_1.add(mnNewMenu_2);JMenuItem mntmBlack = new JMenuItem("Black");mnNewMenu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026广东华南师范大学招聘幼儿教师1人备考题库含答案详解(预热题)
- 2026新疆疆粮恒丰粮油食品有限公司招聘20人备考题库附答案详解(基础题)
- 2026上海戏曲艺术中心所属上海长江剧场(上海市宛平艺苑)副总经理招聘1人备考题库附参考答案详解(b卷)
- 2026中煤环保公司徐州分公司社会招聘工作人员59人备考题库及答案详解(网校专用)
- 2026四川乐山市犍为县第一批就业见习岗位及招募见习人员58人备考题库及参考答案详解(新)
- 安全生产责任制考核管理制度
- 建筑安全月活动方案-建筑工程项目安全策划方案
- 2026上海浦银理财有限责任公司招聘备考题库附参考答案详解ab卷
- 2026四川成都市简阳市射洪坝沱江幼儿园公益性岗位招聘1人备考题库附答案详解(预热题)
- 2026年安徽省合肥市外企德科安徽派驻蜀山区公立幼儿园多名工勤岗位招聘备考题库含答案详解(新)
- 厨房设备维保方案
- 2025年浙江省杭州市事业单位招聘考试教师招聘高中物理学科专业知识试卷
- 用材料抵工程款的协议书
- T/CNFAGS 3-2021三聚氰胺单位产品消耗限额
- T/CHTS 10149-2024公路缆索承重桥梁健康监测阈值技术指南
- 2025跨境电商购销合同范本(中英文对照)
- 2025年人教版九年级物理知识点全面梳理与总结
- DB33T 2256-2020 大棚草莓生产技术规程
- 《建设工程造价咨询服务工时标准(房屋建筑工程)》
- 工程(项目)投资合作协议书样本
- 半导体技术合作开发合同样式
评论
0/150
提交评论