南京郵電大學java第四次實驗報告


 

 

實 驗 報 告

( 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 );
            }
        } );


    }
}
View Code

2. 編寫一個應用程序,設計4個按鈕,分別命名為“加”、“減”、“乘”、“除”,有3個文本框。單擊相應的按鈕,將兩個文本框的數字做運算,在第三個文本框中顯示結果。

package swing;

/**
 * @Author liguo
 * @Description  設計4個按鈕,分別命名為“加”、“減”、“乘”、“除”,另外,窗口中還有3個文本框。
 * 單擊相應的按鈕,將兩個文本框的數字做運算,在第三個文本框中顯示結果。要求處理NumberFormatException異常。
 * @Data 2018-06-29 20:34
 */
import java.awt.Color;
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.JTextField;

public class Caculate extends JFrame implements ActionListener {
    JButton jia;
    JButton jian;
    JButton cheng;
    JButton chu;
    JTextField one;
    JTextField two;
    JTextField three;
    JLabel label;

    public Caculate() {
        init();
        setVisible(true);
        setResizable(true);
        validate();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void init() {
        setLayout(new FlowLayout());
        setSize(300, 320);
        setTitle("計算");
        jia = new JButton("加");
        jian = new JButton("減");
        cheng = new JButton("乘");
        chu = new JButton("除");
        one = new JTextField(10);
        two = new JTextField(10);
        three = new JTextField(10);
        label = new JLabel(" ", JLabel.CENTER);
        label.setBackground(Color.green);
        add(one);
        add(label);
        add(two);
        add(three);
        add(jia);
        add(jian);
        add(cheng);
        add(chu);
        jia.addActionListener(this);
        jian.addActionListener(this);
        cheng.addActionListener(this);
        chu.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        double n;
        if (e.getSource() == jia) {
            double n1, n2;
            try {
                n1 = Double.parseDouble(one.getText());
                n2 = Double.parseDouble(two.getText());
                n = n1 + n2;
                three.setText(String.valueOf(n));
                label.setText("+");
            } catch (NumberFormatException ee) {
                three.setText("請輸入數字字符");
            }
        } else if (e.getSource() == jian) {
            double n1, n2;
            try {
                n1 = Double.parseDouble(one.getText());
                n2 = Double.parseDouble(two.getText());
                n = n1 - n2;
                three.setText(String.valueOf(n));
                label.setText("-");
            } catch (NumberFormatException ee) {
                three.setText("請輸入數字字符");
            }
        } else if (e.getSource() == cheng) {
            double n1, n2;
            try {
                n1 = Double.parseDouble(one.getText());
                n2 = Double.parseDouble(two.getText());
                n = n1 * n2;
                three.setText(String.valueOf(n));
                label.setText("*");
            } catch (NumberFormatException ee) {
                three.setText("請輸入數字字符");
            }
        } else if (e.getSource() == chu) {
            double n1, n2;
            try {
                n1 = Double.parseDouble(one.getText());
                n2 = Double.parseDouble(two.getText());
                n = n1 / n2;
                three.setText(String.valueOf(n));
                label.setText("/");
            } catch (NumberFormatException ee) {
                three.setText("請輸入數字字符");
            }
        }
        validate();
    }

    public static void main(String[] args) {
        Caculate test = new Caculate();
    }
}
View Code

//那個用JFormDesigner插件是好寫 ,就是生成的代碼量太多了,現在這個是重寫的;

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 );
    }
}
View Code

四、實驗小結(包括問題和解決方法、心得體會等)

 

1:此次實驗加深了自己對於swing的使用,尤其是幾種布局的格式,以及控件按鈕方法的書寫。學會了通過插件JFormDesigner來寫Swing界面做類似計算器的東西。

2:學習了用線程來寫控制程序。

五、指導教師評語

 

成  績

 

批閱人

 

日  期

 

                            

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM