《最新java实验报告.doc》由会员分享,可在线阅读,更多相关《最新java实验报告.doc(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精品资料java实验报告.信息工程学院Java语言课内实习报告 (20132014学年第 二学期) 实习题目:Swing 姓 名: 学 号: 专 业: 年级班级:一、实习目的 使用Java做图形界面啦,了解图形用户界面基本组件的使用,布局管理器如何对组件进行管理以及事件处理机制的应用。二、实习设计过程任务一:小小界面设计请设计一个界面,包括:菜单,文本框,按钮,标签,文本区,选择框,单选按钮和下拉列表。 任务二: 计算器(1) 编写一个计算器的程序,主界面由多个按钮组建、文本框组建构成。运行结果如图所示: 任务三:算数测试编写一个算术测试小软件,用来训练小学生的算术能力。程序由3个类组成,其中
2、Teacher类对象负责给出算术题目,并判断回答者的答案是否正确:ComputerFrame类对象提供的GUI界面看到题目并通过该GUI界面给出题目的答案;MailClass是主类。 任务四: 日历该窗口为BorderLayout布局。窗口的中心添加一个Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter的中放置49个标签,用来显示日历。窗口北面添加一个Panel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth,可以显示当前月的下一个月的日
3、历;单击previousMonth按钮,可以显示当前月的上一个月的日历。窗口的南面添加一个Panel容器pSouth,其布局是FlowLayout布局,pSouth中请完成界面设计和相关功能。 三、调试过程中存在问题分析 布局过程中运行出来的图片效果不是自己设计的那样,有的文字被隐藏,重新调整文本框大小就弄好了。四、心得、体会与建议 这次实习过程中,感觉最有难度的是算数测试题目的设计,要判定结果是否正确。总体做起来还是很简单的。附录:题目1代码:import javax.swing.*;import java.awt.*;public class MyTu private JFrame fra
4、me;private JTextField textField;private JTextField textField_1;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try MyTu window = new MyTu();window.frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create
5、 the application. */public MyTu() initialize(null);/* * Initialize the contents of the frame. * param menuBar */SuppressWarnings( unchecked, rawtypes )private void initialize(JMenuBar menuBar) frame = new JFrame();frame.setTitle(u6211u81EAu5DF1u7684u5C0Fu5C0Fu754Cu9762);frame.setBounds(100, 100, 450
6、, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);menuBar = new JMenuBar();frame.setJMenuBar(menuBar);JMenu mnNewMenu = new JMenu(u6587u4EF6);mnNewMenu.setFont(new Font(宋体, Font.PLAIN, 14);mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);menuBar.add(mnNewMenu);JMenuItem mntmNewMenuI
7、tem = new JMenuItem(u65B0u5EFA);mntmNewMenuItem.setHorizontalAlignment(SwingConstants.LEFT);mnNewMenu.add(mntmNewMenuItem);JMenuItem menuItem = new JMenuItem(u6253u5F00);menuItem.setHorizontalAlignment(SwingConstants.LEFT);mnNewMenu.add(menuItem);JMenuItem menuItem_1 = new JMenuItem(u5173u95ED);menu
8、Item_1.setHorizontalAlignment(SwingConstants.LEFT);mnNewMenu.add(menuItem_1);JMenu mnNewMenu_1 = new JMenu(u7F16u8F91);mnNewMenu_1.setFont(new Font(宋体, Font.PLAIN, 14);mnNewMenu_1.setHorizontalAlignment(SwingConstants.CENTER);menuBar.add(mnNewMenu_1);JMenuItem menuItem_2 = new JMenuItem(u67E5u627E);
9、menuItem_2.setHorizontalAlignment(SwingConstants.LEFT);mnNewMenu_1.add(menuItem_2);JMenuItem menuItem_3 = new JMenuItem(u66FFu6362);menuItem_3.setHorizontalAlignment(SwingConstants.LEFT);mnNewMenu_1.add(menuItem_3);JMenuItem menuItem_4 = new JMenuItem(u5B9Au4F4D);menuItem_4.setHorizontalAlignment(Sw
10、ingConstants.LEFT);mnNewMenu_1.add(menuItem_4);JMenu menu = new JMenu(u683Cu5F0F);menu.setHorizontalAlignment(SwingConstants.CENTER);menu.setFont(new Font(宋体, Font.PLAIN, 14);menuBar.add(menu);JMenuItem menuItem_5 = new JMenuItem(u6BB5u843D);menu.add(menuItem_5);JMenuItem menuItem_6 = new JMenuItem(
11、u5B57u4F53);menu.add(menuItem_6);frame.getContentPane().setLayout(null);textField = new JTextField();textField.setBounds(91, 38, 122, 21);frame.getContentPane().add(textField);textField.setColumns(10);JLabel label = new JLabel(u7528u6237u540D);label.setBounds(23, 38, 58, 21);frame.getContentPane().a
12、dd(label);JLabel label_1 = new JLabel(u5BC6u7801);label_1.setBounds(23, 72, 41, 15);frame.getContentPane().add(label_1);textField_1 = new JTextField();textField_1.setBounds(91, 69, 122, 21);frame.getContentPane().add(textField_1);textField_1.setColumns(10);JLabel label_2 = new JLabel(u6027u522B);lab
13、el_2.setBounds(23, 107, 54, 15);frame.getContentPane().add(label_2);JRadioButton RadioButton2 = new JRadioButton(u7537);RadioButton2.setBounds(84, 103, 54, 23);frame.getContentPane().add(RadioButton2);JRadioButton RadioButton1 = new JRadioButton(u5973);RadioButton1.setBounds(157, 103, 54, 23);frame.
14、getContentPane().add(RadioButton1);ButtonGroup select=new ButtonGroup();select.add(RadioButton1);select.add(RadioButton2);JLabel label_3 = new JLabel(u6BD4u8D5Bu7C7Bu578B);label_3.setBounds(23, 147, 75, 21);frame.getContentPane().add(label_3);JCheckBox checkBox = new JCheckBox(u4E13u4E1Au7EC4);check
15、Box.setBounds(84, 146, 66, 23);frame.getContentPane().add(checkBox);JCheckBox checkBox_1 = new JCheckBox(u4E1Au4F59u7EC4);checkBox_1.setBounds(157, 146, 103, 23);frame.getContentPane().add(checkBox_1);JButton button = new JButton(u767Bu9646);button.setBounds(38, 210, 93, 23);frame.getContentPane().a
16、dd(button);JButton button_1 = new JButton(u53D6u6D88);button_1.setBounds(182, 210, 93, 23);frame.getContentPane().add(button_1);JTextArea textArea = new JTextArea();textArea.setText(u62A5u540Du7684u76EEu7684u662FuFF1A);textArea.setBounds(266, 58, 160, 144);frame.getContentPane().add(textArea);JCombo
17、Box comboBox = new JComboBox();comboBox.setBounds(265, 27, 103, 21);comboBox.addItem(比赛);comboBox.addItem(报名);frame.getContentPane().add(comboBox);JLabel label_4 = new JLabel(u62A5u540Du76EEu7684);label_4.setBounds(266, 10, 54, 15);frame.getContentPane().add(label_4);题目2代码:import java.awt.EventQueue
18、;public class MyTuTwo private JFrame frame;private JTextField textField;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try MyTuTwo window = new MyTuTwo();window.frame.setVisible(true); catch (Exception e) e.printStackTrace(
19、););/* * Create the application. */public MyTuTwo() initialize();/* * Initialize the contents of the frame. */private void initialize() frame = new JFrame();frame.setTitle(u7B80u6613u8BA1u7B97u5668);frame.getContentPane().setFont(new Font(Dialog, Font.PLAIN, 15);frame.setBounds(100, 100, 450, 300);f
20、rame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(null);JPanel panel_1 = new JPanel();panel_1.setBounds(20, 66, 404, 37);frame.getContentPane().add(panel_1);panel_1.setLayout(null);JButton btnNewButton_23 = new JButton(Back);btnNewButton_23.setFont(new Font(Dialog,
21、 Font.PLAIN, 15);btnNewButton_23.setBounds(0, 0, 96, 33);panel_1.add(btnNewButton_23);JButton btnNewButton_22 = new JButton(CE);btnNewButton_22.setFont(new Font(Dialog, Font.PLAIN, 15);btnNewButton_22.setBounds(100, 0, 96, 33);panel_1.add(btnNewButton_22);JButton btnNewButton_21 = new JButton(C);btn
22、NewButton_21.setFont(new Font(Dialog, Font.PLAIN, 15);btnNewButton_21.setBounds(225, 0, 86, 33);panel_1.add(btnNewButton_21);JButton btnNewButton_20 = new JButton(About);btnNewButton_20.setFont(new Font(Dialog, Font.PLAIN, 15);btnNewButton_20.setBounds(316, 0, 88, 33);panel_1.add(btnNewButton_20);JP
23、anel panel_2 = new JPanel();panel_2.setBounds(20, 113, 195, 139);frame.getContentPane().add(panel_2);panel_2.setLayout(new GridLayout(4, 3, 4, 4);JButton btnNewButton_1 = new JButton(9);btnNewButton_1.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_1);JButton btnNewButton_3 = new J
24、Button(8);btnNewButton_3.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_3);JButton btnNewButton_6 = new JButton(7);btnNewButton_6.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_6);JButton btnNewButton_4 = new JButton(6);btnNewButton_4.setFont(new Font(Dialog, Fo
25、nt.PLAIN, 15);panel_2.add(btnNewButton_4);JButton btnNewButton_5 = new JButton(5);btnNewButton_5.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_5);JButton btnNewButton_2 = new JButton(4);btnNewButton_2.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_2);JButton bt
26、nNewButton = new JButton(3);btnNewButton.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton);JButton btnNewButton_9 = new JButton(2);btnNewButton_9.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_9);JButton btnNewButton_7 = new JButton(1);btnNewButton_7.setFont(new F
27、ont(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_7);JButton btnNewButton_8 = new JButton(0);btnNewButton_8.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_8);JButton btnNewButton_10 = new JButton(+/-);btnNewButton_10.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewBu
28、tton_10);JButton btnNewButton_11 = new JButton(.);btnNewButton_11.setFont(new Font(Dialog, Font.PLAIN, 15);panel_2.add(btnNewButton_11);JPanel panel_3 = new JPanel();panel_3.setBounds(245, 115, 179, 137);frame.getContentPane().add(panel_3);panel_3.setLayout(new GridLayout(4, 2, 4, 4);JButton btnNewB
29、utton_13 = new JButton(/);btnNewButton_13.setFont(new Font(Dialog, Font.PLAIN, 15);panel_3.add(btnNewButton_13);JButton btnNewButton_14 = new JButton(sqrt);btnNewButton_14.setFont(new Font(Dialog, Font.PLAIN, 15);panel_3.add(btnNewButton_14);JButton btnNewButton_16 = new JButton(*);btnNewButton_16.s
30、etFont(new Font(Dialog, Font.PLAIN, 15);panel_3.add(btnNewButton_16);JButton btnNewButton_15 = new JButton(%);btnNewButton_15.setFont(new Font(Dialog, Font.PLAIN, 15);panel_3.add(btnNewButton_15);JButton btnNewButton_18 = new JButton(-);btnNewButton_18.setFont(new Font(Dialog, Font.PLAIN, 15);panel_
31、3.add(btnNewButton_18);JButton btnNewButton_12 = new JButton(1/x);btnNewButton_12.setFont(new Font(Dialog, Font.PLAIN, 15);panel_3.add(btnNewButton_12);JButton btnNewButton_17 = new JButton(+);btnNewButton_17.setFont(new Font(Dialog, Font.PLAIN, 15);panel_3.add(btnNewButton_17);JButton btnNewButton_
32、19 = new JButton(=);btnNewButton_19.setFont(new Font(Dialog, Font.PLAIN, 15);panel_3.add(btnNewButton_19);JPanel panel = new JPanel();panel.setBounds(20, 10, 404, 30);frame.getContentPane().add(panel);panel.setLayout(null);textField = new JTextField();textField.setBounds(0, 0, 404, 30);panel.add(tex
33、tField);textField.setHorizontalAlignment(SwingConstants.RIGHT);textField.setText(0);textField.setEditable(false);textField.setColumns(10);题目3代码:import java.awt.*;public class ComputerJFrame extends JFrame implements ActionListenerJTextField textOne,textTwo,textResult;JButton getProblem,giveAnswer;JL
34、abel operatorLabel,message;Teacher teacher;ComputerJFrame(String s)super(s);teacher = new Teacher();textOne=new JTextField(10);textOne.setBounds(111, 6, 66, 21);textTwo=new JTextField(10);textTwo.setBounds(215, 6, 66, 21);textResult=new JTextField(10);textResult.setBounds(307, 6, 66, 21);operatorLab
35、el =new JLabel(+);operatorLabel.setBounds(187, 9, 18, 15);message = new JLabel(你还没有回答呢);message.setBounds(220, 37, 108, 15);getProblem = new JButton(获取题目);getProblem.setBounds(10, 5, 91, 23);giveAnswer = new JButton(确认答案);giveAnswer.setBounds(103, 33, 101, 23);getContentPane().setLayout(null);getCon
36、tentPane().setLayout(null);getContentPane().setLayout(null);getContentPane().setLayout(null);getContentPane().add(getProblem);getContentPane().add(textOne);getContentPane().add(operatorLabel);getContentPane().add(textTwo);Label label = new Label(=);label.setBounds(290, 5, 21, 23);getContentPane().ad
37、d(label);getContentPane().add(textResult); getContentPane().add(giveAnswer);getContentPane().add(message);/添加若干组件到窗口textResult.requestFocus();/textResult 获得焦点textOne.setEditable(false);textTwo.setEditable(false);/textOne设置textOne和textTwo不可编辑getProblem.addActionListener(this);/将当前窗口注册为getProblem的Acti
38、onEvent事件监视器 giveAnswer.addActionListener(this);/将当前窗口注册为giveAnwser的ActionEvent事件监视器textResult.addActionListener(this);/将当前窗口注册为textResult的ActionEvent事件监视器setBounds(100, 100, 432, 100);setLocationRelativeTo(null);/设置窗体居中显示setVisible(true);/窗口的可见性设置validate();addWindowFocusListener(new WindowAdapter(
39、)public void windowClosing(WindowEvent e)System.exit(0););public void actionPerformed(ActionEvent e)if(e.getSource()=getProblem)/如果点击了“获取题目 ”int number1 = teacher.giveNumberOne(100);/获取第一个操作数的值int number2 = teacher.giveNumberTwo(100);/获取第二个操作数的值String operator = teacher.giveOperator();/获取运算符textOne.
40、setText(+number1);/设置文本框参数字符串指定文本textTwo.setText(+number2);operatorLabel.setText(operator);message.setText(请回答);textResult.setText(null);/设置图形界面上文本框以及标签的文本,包括:textOne,textTwo,textResult,operatorLabel,messageif(e.getSource()=giveAnswer)/如果点击了“确认答案 ”String answer = textResult.getText();/获取文本框中输入的答案try
41、int result = Integer.parseInt(answer); if(teacher.getRight(result)=true) message.setText(回答正确); else message.setText(回答错误); /如果答案正确,message显示“你回答正确”,如果不正确,显示“你回答错误”catch(NumberFormatException ex)message.setText(请输入数字字符);/如果输入的不是数字,显示“请输入数字字符” textResult.requestFocus();/textResult 获得焦点validate();题目4代
42、码:import java.awt.EventQueue;public class Calendar private JFrame frame;private JLabel lblNewLabel_1;private JLabel labelDay= new JLabel42;private JButton button_1;private JButton button_2;private JButton button_3;private JButton button_4;private JButton button_5;private JButton button_6;private JBut