1. 程式人生 > >南京郵電大學java第四次實驗報告

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

hid AR 控制結構 swing textarea 報告 lis tr1 listener

實 驗 報 告

( 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個文本框。單擊相應的按鈕,將兩個文本框的數字做運算,在第三個文本框中顯示結果。

技術分享圖片
/*
 * 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
}
View Code

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:學習了用線程來寫控制程序。

五、指導教師評語

成 績

批閱人

日 期

                            

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