java实验报告实验六Java图形用户界面(共29页).doc
《java实验报告实验六Java图形用户界面(共29页).doc》由会员分享,可在线阅读,更多相关《java实验报告实验六Java图形用户界面(共29页).doc(29页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上信 息 工 程 学 院实验报告的内容与格式按任课教师的要求书写。 Java程序设计 实习报告JAVA图形用户界面实验六Java图形用户界面1实验目的(1)掌握图形用户界面基本组件。(2)了解如何使用布局管理器对组件进行管理。(3)掌握Java事件处理机制。2实验内容实验题1 编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。运行结果:加法:主要代码:private void initComponents() jButton1 = new javax.
2、swing.JButton();jButton2 = new javax.swing.JButton();jButton3 = new javax.swing.JButton();jButton4 = new javax.swing.JButton();jButton5 = new javax.swing.JButton();jButton6 = new javax.swing.JButton();jButton7 = new javax.swing.JButton();jButton8 = new javax.swing.JButton();jButton9 = new javax.swin
3、g.JButton();jButton10 = new javax.swing.JButton();jButton11 = new javax.swing.JButton();jButton12 = new javax.swing.JButton();jButton13 = new javax.swing.JButton();jButton14 = new javax.swing.JButton();jButton15 = new javax.swing.JButton();jTextField1 = new javax.swing.JTextField();setStub(null);jBu
4、tton1.setText(3);jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton1ActionPerformed(evt););jButton2.setText(1);jButton2.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.Act
5、ionEvent evt) jButton2ActionPerformed(evt););jButton3.setText(5);jButton3.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton3ActionPerformed(evt););jButton4.setText(2);jButton4.addActionListener(new java.awt.event.ActionListener(
6、) public void actionPerformed(java.awt.event.ActionEvent evt) jButton4ActionPerformed(evt););jButton5.setText(6);jButton5.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton5ActionPerformed(evt););jButton6.setText(8);jButton6.addA
7、ctionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton6ActionPerformed(evt););jButton7.setText(4);jButton7.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton7ActionPe
8、rformed(evt););jButton8.setText(7);jButton8.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton8ActionPerformed(evt););jButton9.setText(0);jButton9.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(
9、java.awt.event.ActionEvent evt) jButton9ActionPerformed(evt););jButton10.setText(9);jButton10.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton10ActionPerformed(evt););jButton11.setText(u00f7);jButton11.addActionListener(new jav
10、a.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton11ActionPerformed(evt););jButton12.setText(u00d7);jButton12.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton12ActionPerformed(evt);
11、);jButton13.setText(-);jButton13.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton13ActionPerformed(evt););jButton14.setText(+);jButton14.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt
12、.event.ActionEvent evt) jButton14ActionPerformed(evt););jButton15.setText(=);jButton15.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton15ActionPerformed(evt););实验题2 编写一个程序,有一个窗口,该窗口为BorderLayout布局。窗口的中心添加一个Panel容器:pCenter,pCent
13、er的布局是7行7列的GridLayout布局,pCenter的中放置49个标签,用来显示日历。窗口北面添加一个Panel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth,可以显示当前月的下一个月的日历;单击previousMonth按钮,可以显示当前月的上一个月的日历。窗口的南面添加一个Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一个标签用来显示一些信息。运行结果如图所示。图3.8 运行结果图基本要求 编写完整程序。运行结果:主要代码:private J
14、Label buttonDay = new JLabel42;private JButton buttonWeek = new JButton7;private JLabel labelMonth = new JLabel();private JButton buttonLastMonth = new JButton();private JButton buttonNextMonth = new JButton();private JPanel pCenter=new JPanel();private JPanel pNorth=new JPanel();private JPanel pSou
15、th=new JPanel();private JLabel time=new JLabel();public Calender() super(Calender); setBounds(250, 200, 600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonLastMonth.setText(上月); buttonLastMonth.addActionListener(this); pNorth.add(buttonLastMonth); buttonNextMonth.setText(下月); buttonNex
16、tMonth.addActionListener(this); pNorth.add(buttonNextMonth); getContentPane().add(pNorth,BorderLayout.NORTH); getContentPane().add(pCenter,BorderLayout.CENTER); pCenter.setLayout(new GridLayout(7,7); for (int i = 0; i 7; i+) buttonWeeki = new JButton(); buttonWeeki.setText(stringWeekCni); pCenter.ad
17、d(buttonWeeki); for (int i = 0; i 42; i+) buttonDayi = new JLabel(); buttonDayi.setText( ); pCenter.add(buttonDayi); 实验题3 实现如图3.9所示的布局方式功能:前两个文本框输入整型数据。第三个文本框存放前两个文本框数据之和。要求如下:第一个文本框的数据是100,200,如果超出该范围弹出对话框提示用户。弹出提示对话框的时刻是光标离开第一个文本框时。图3.9 求和运行结果:检验输入数据范围:主要代码:class MouseHander extends MouseAdapter p
18、ublic MouseHander(JTextField c) current=c; public void mousePressed(MouseEvent event) if(current=result) double firstNumber=Double.parseDouble(first.getText(); double secondNumber=Double.parseDouble(second.getText(); double Result=firstNumber+secondNumber; result.setText(+Result); else current.setTe
19、xt(); private JTextField current; class MouseMotionHander extends MouseMotionAdapter public void mouseMoved(MouseEvent event) double number=Double.parseDouble(first.getText(); if(number200) int type=JOptionPane.ERROR_MESSAGE; JOptionPane.showMessageDialog(null,new String(输入数字必须在100200之间),提示, 2); 实验题
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验 报告 图形 用户界面 29
限制150内