标签:nbu nta 教材 解决 lua 计算 exce getc import
实 验 报 告
( 2017 / 2018学年 第2学期)
课程名称 |
JAVA语言程序设计 |
|||||
实验名称 |
Java集成开发环境的安装与使用、 Java变量、表达式与控制结构 |
|||||
实验时间 |
2018 |
年 |
6 |
月 |
7 |
日 |
指导单位 |
计算机学院软件教学中心 |
|||||
指导教师 |
许棣华 |
学生姓名 |
王利国 |
班级学号 |
B160209 |
学院(系) |
电子与光学工程学院,微电子学院 |
专 业 |
微电子科学与工程 |
实验名称 |
方法、数组和类 |
指导教师 |
许棣华 |
|
|||||||
实验类型 |
上机 |
实验学时 |
2 |
实验时间 |
2017.6.7
|
一、 实验目的 1. 了解和掌握Java中GUI组件和界面化设计 2. 掌握Java中创建线程对象的方法 3. 熟悉控制线程状态的过程 |
二、实验环境(实验设备) 1. 每位学生配备计算机一台 2. 计算机需安装好JDK和Jcreator |
三、实验内容
1. 编写一个Applet,利用两个文本框对象input1和input2,接收用户从键盘输入的两个整型数。当用户单击“计算”按钮时,可进行算术计算,并输出运算结果;运算结果放在多行文本域JTextArea组件中。GUI界面参考教材198页9.6题。
package swing; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * @Author liguo * @Description * @Data 2018-06-04 20:06 */ public class Test { public static void main(String[] args) { // 创建 JFrame 实例 JFrame frame = new JFrame( "四则运算" ); frame.setSize( 350, 300 ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); /* 创建面板,这个类似于 HTML 的 div 标签 * 我们可以创建多个面板并在 JFrame 中指定位置 * 面板中我们可以添加文本字段,按钮及其他组件。 */ JPanel panel = new JPanel(); //添加面板 frame.add( panel ); /* * 调用用户定义的方法并添加组件到面板 */ placeComponents( panel ); // 设置界面可见 frame.setVisible( true ); } private static void placeComponents(JPanel panel) { panel.setLayout( null ); // 创建 JLabel JLabel firstLabel = new JLabel( "第一个数为" ); /* 这个方法定义了组件的位置。 * setBounds(x, y, width, height) * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。 */ firstLabel.setBounds( 10, 20, 80, 25 ); panel.add( firstLabel ); //field JTextField firstNumber = new JTextField( 20 ); firstNumber.setBounds( 100, 20, 165, 25 ); panel.add( firstNumber ); // 输入的文本域 JLabel secondlabe2 = new JLabel( "第二个数字" ); secondlabe2.setBounds( 10, 50, 80, 25 ); panel.add( secondlabe2 ); /** * 第二个数字的输入 */ JTextField secondNumber = new JTextField( 20 ); secondNumber.setBounds( 100, 50, 165, 25 ); panel.add( secondNumber ); // 创建计算按钮 JButton loginButton = new JButton( "计算" ); loginButton.setBounds( 10, 80, 80, 25 ); panel.add( loginButton ); //添加页面输出 JTextArea area = new JTextArea(); area.setBounds( 100, 110, 165, 100 ); panel.add( area ); loginButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int a = Integer.parseInt( firstNumber.getText() ); int b = Integer.parseInt( secondNumber.getText() ); String str = "和为" + (a + b) + "\n差为" + (a - b) + "\n积为" + (a * b) + "\n商为" + (a / b); area.append( str ); } } ); } }
2. 编写一个应用程序,设计4个按钮,分别命名为“加”、“减”、“乘”、“除”,有3个文本框。单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。
/* * Created by JFormDesigner on Thu Jun 07 19:46:32 CST 2018 */ package swing; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * @author ç??å?©å?½ */ public class Test6 extends JFrame { public static void main(String[] args) { Test6 one = new Test6(); one.setVisible( true ); } public Test6() { initComponents(); } private void button1ActionPerformed(ActionEvent e) { // TODO add your code here int a = Integer.parseInt( textField1.getText() ); int b = Integer.parseInt( textField2.getText() ); String str1 = ""+(a+b); textArea1.append( str1 ); } private void button2ActionPerformed(ActionEvent e) { // TODO add your code here int a = Integer.parseInt( textField1.getText() ); int b = Integer.parseInt( textField2.getText() ); String str1 = ""+(a-b); textArea1.append( str1 ); } private void button3ActionPerformed(ActionEvent e) { // TODO add your code here int a = Integer.parseInt( textField1.getText() ); int b = Integer.parseInt( textField2.getText() ); String str1 = ""+(a*b); textArea1.append( str1 ); } private void button4ActionPerformed(ActionEvent e) { // TODO add your code here int a = Integer.parseInt( textField1.getText() ); int b = Integer.parseInt( textField2.getText() ); String str1 = ""+(a/b); textArea1.append( str1 ); } private void initComponents() { // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents // Generated using JFormDesigner Evaluation license - ç??å?©å?½ dialogPane = new JPanel(); contentPanel = new JPanel(); label1 = new JLabel(); textField1 = new JTextField(); label2 = new JLabel(); textField2 = new JTextField(); button1 = new JButton(); scrollPane1 = new JScrollPane(); textArea1 = new JTextArea(); button2 = new JButton(); button3 = new JButton(); button4 = new JButton(); buttonBar = new JPanel(); //======== this ======== Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); //======== dialogPane ======== { dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12)); // JFormDesigner evaluation mark dialogPane.setBorder(new javax.swing.border.CompoundBorder( new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0), "JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12), java.awt.Color.red), dialogPane.getBorder())); dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}}); dialogPane.setLayout(new BorderLayout()); //======== contentPanel ======== { contentPanel.setLayout(new GridBagLayout()); ((GridBagLayout)contentPanel.getLayout()).columnWidths = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ((GridBagLayout)contentPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0}; ((GridBagLayout)contentPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4}; ((GridBagLayout)contentPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4}; //---- label1 ---- label1.setText("\u7b2c\u4e00\u4e2a\u6570"); contentPanel.add(label1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0)); //---- textField1 ---- textField1.setText(" "); contentPanel.add(textField1, new GridBagConstraints(3, 1, 6, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0)); //---- label2 ---- label2.setText("\u7b2c\u4e8c\u4e2a\u6570"); contentPanel.add(label2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0)); //---- textField2 ---- textField2.setText(" "); contentPanel.add(textField2, new GridBagConstraints(3, 2, 6, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0)); //---- button1 ---- button1.setText("\u52a0"); button1.addActionListener(e -> button1ActionPerformed(e)); contentPanel.add(button1, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0)); //======== scrollPane1 ======== { //---- textArea1 ---- textArea1.setText(" "); scrollPane1.setViewportView(textArea1); } contentPanel.add(scrollPane1, new GridBagConstraints(3, 3, 11, 4, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); //---- button2 ---- button2.setText("\u51cf"); button2.addActionListener(e -> button2ActionPerformed(e)); contentPanel.add(button2, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0)); //---- button3 ---- button3.setText("\u4e58"); button3.addActionListener(e -> button3ActionPerformed(e)); contentPanel.add(button3, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0)); //---- button4 ---- button4.setText("\u9664"); button4.addActionListener(e -> button4ActionPerformed(e)); contentPanel.add(button4, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0)); } dialogPane.add(contentPanel, BorderLayout.CENTER); //======== buttonBar ======== { buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0)); buttonBar.setLayout(new GridBagLayout()); ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80}; ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0}; } dialogPane.add(buttonBar, BorderLayout.SOUTH); } contentPane.add(dialogPane, BorderLayout.CENTER); pack(); setLocationRelativeTo(getOwner()); // JFormDesigner - End of component initialization //GEN-END:initComponents } // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables // Generated using JFormDesigner Evaluation license - ç??å?©å?½ private JPanel dialogPane; private JPanel contentPanel; private JLabel label1; private JTextField textField1; private JLabel label2; private JTextField textField2; private JButton button1; private JScrollPane scrollPane1; private JTextArea textArea1; private JButton button2; private JButton button3; private JButton button4; private JPanel buttonBar; // JFormDesigner - End of variables declaration //GEN-END:variables }
3. 编写一个有两个线程的程序,第一个线程用来计算2~100000之间的质数及个数,第二个线程用来计算100000~200000之间的质数及个数。
package swing; /** * @Author liguo * @Description * @Data 2018-06-07 9:26 */ public class Thread extends java.lang.Thread { public static void run(int min, int max) { int i, j; for (i = min; i <= max; i++) { for (j = 2; j < (int) Math.sqrt( i ); j++) { if (i % j == 0) break; else System.out.print( i ); count++; } System.out.println( min + " " + max + ":" + count ); } } private int min, max; private static int count = 0; public static void main(String[] args) { Thread t1 = new Thread(); Thread t2 = new Thread(); t1.run( 2, 100000 ); t2.run( 100000, 20000 ); } }
四、实验小结(包括问题和解决方法、心得体会等)
1:此次实验加深了自己对于swing的使用,尤其是几种布局的格式,以及控件按钮方法的书写。学会了通过插件JFormDesigner来写Swing界面做类似计算器的东西。 2:学习了用线程来写控制程序。 |
|||||
五、指导教师评语
|
|||||
成 绩 |
|
批阅人 |
|
日 期 |
|
标签:nbu nta 教材 解决 lua 计算 exce getc import
原文地址:https://www.cnblogs.com/liguo-wang/p/9152591.html