Java programming experiment one

"Java Programming" Experimental Report (1)

Experiment name: Familiar experiment location of Java development environment: 10-414

Tools and environment used:

Environment: Windows 8.1; jdk 1.8;

Tool: eclipse

Purpose:

Edit, compile, run, and debug simple Java programs under Eclipse.

Experiment content:

1. Familiar with Java Application programming;
example: Scanner class p125.
Standard dialog box
p155 2. Familiar with WindowBuilder UI programming, build a simple input and output box, as shown in the figure.
Insert picture description here

Procedure 1:

package com;

import java.util.Scanner;

public class Test01 {
    
    
    public static void main(String[] args) {
    
    
            for(int i=0;i<5;i++){
    
    

            Scanner input = new Scanner(System.in);
            System.out.println("请输入学生的分数:");
            double score = input.nextDouble();
            int level1,level2;
            if(score>=90.0 && score<=100.0){
    
     level1 = 1;}
            else if(score>=75.0){
    
     level1 = 2;}
            else if(score>=60.0){
    
     level1 = 3;}
            else if(score>=40.0){
    
     level1 = 4;}
            else{
    
     level1 = 5;}

            if(score>=85.0&&score<=100.0){
    
    level2 = 1;}
            else if(score>=70.0){
    
    level2 = 2;}
            else if(score>=60.0){
    
    level2 = 3;}
            else{
    
    level2 = 4;}
            switch(level1)
            {
    
    
                case 1:
                    System.out.print("该生的成绩绩点是:  5\n");
                    break;
                case 2:
                    System.out.print("该生的成绩绩点是:  4\n");
                    break;
                case 3:
                    System.out.print("该生的成绩绩点是:  3\n");
                    break;
                case 4:
                    System.out.print("该生的成绩绩点是: 2\n");
                    break;
                case 5:
                    System.out.print("该生的成绩绩点是: 1\n");
                    break;
            }

          }

    }
}


Insert picture description here

Procedure 2:

package com;

import javax.swing.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.HashMap;

public class Swing01 extends JFrame implements ActionListener {
    
    


//    public class OptionPaneDemo extends JFrame implements ActionListener {
    
    
        private JPanel p;
        private JButton btnInput, btnMsg, btnConfirm, btnOption;
        private JTextField txtResult;

        public Swing01() {
    
    
            super("标准对话框");
            p = new JPanel();
            btnInput = new JButton("输入");
            btnMsg = new JButton("消息");
            btnConfirm = new JButton("确认");
            btnOption = new JButton("选项");
            txtResult = new JTextField(20);
            // 注册监听
            btnInput.addActionListener(this);
            btnMsg.addActionListener(this);
            btnConfirm.addActionListener(this);
            btnOption.addActionListener(this);

            p.add(btnInput);
            p.add(btnMsg);
            p.add(btnConfirm);
            p.add(btnOption);
            p.add(txtResult);

            this.add(p);
            this.setSize(300, 150);
            this.setLocation(100, 100);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public void actionPerformed(ActionEvent e) {
    
    
            // 获取事件源
            Object source = e.getSource();
            if (source == btnInput) {
    
    
                String strIn = JOptionPane.showInputDialog(btnInput, "请输入姓名:");
                txtResult.setText(strIn);
            }
            if (source == btnMsg) {
    
    
                JOptionPane.showMessageDialog(btnMsg, "这是一个消息对话框", "提示",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            if (source == btnConfirm) {
    
    
                int r = JOptionPane.showConfirmDialog(btnConfirm, "您确定要删除吗?", "删除",
                        JOptionPane.YES_NO_OPTION);
                if (r == JOptionPane.YES_OPTION) {
    
    
                    txtResult.setText("删除!");
                }

            }
            if (source == btnOption) {
    
    
                Object[] options = {
    
     "Red", "Green", "Blue" };
                int sel = JOptionPane.showOptionDialog(btnOption, "选择颜色:", "选择",
                        JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                        null, options, options[0]);
                if (sel != JOptionPane.CLOSED_OPTION) {
    
    
                    txtResult.setText("颜色: " + options[sel]);
                }
            }
        }

        public static void main(String[] args) {
    
    
            Swing01 f = new Swing01();
            f.setVisible(true);
        }
    }




Insert picture description here

Procedure 3:

package com;


import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.Font;

public class index extends JFrame {
    
    
    private JTextField textField;
    public index() {
    
    
        getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setFont(new Font("宋体", Font.PLAIN, 15));
        textField.setText("登录成功");
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        textField.setBounds(123, 62, 172, 70);
        getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel = new JLabel("恭喜您");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        lblNewLabel.setBounds(181, 161, 114, 27);
        getContentPane().add(lblNewLabel);
    }
}
package com;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import javax.swing.JProgressBar;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
public class mainWindow {
    
    
    private JFrame frame,f2;
    private JTextField acc;
    private JTextField psw;
    public static void main(String[] args) {
    
    
        EventQueue.invokeLater(new Runnable() {
    
    
            public void run() {
    
    
                try {
    
    
                    mainWindow window = new mainWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }
        });
    }
    public mainWindow() {
    
    
        initialize();
    }
    private void initialize() {
    
    
        frame = new JFrame();
        f2= new index();
        f2.setVisible(false);
        f2.setSize(500,300);
        frame.setTitle("请验证用户名和密码");
        frame.getContentPane().setFont(new Font("宋体", Font.PLAIN, 15));
        frame.getContentPane().setLayout(null);
        frame.setSize(500,300);

        acc = new JTextField();
        acc.setBackground(Color.WHITE);
        acc.setBounds(135, 37, 222, 32);
        frame.getContentPane().add(acc);
        acc.setColumns(10);

        JLabel lblNewLabel = new JLabel("用户名");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel.setBounds(67, 41, 58, 23);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("密码");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_1.setBounds(67, 107, 58, 23);
        frame.getContentPane().add(lblNewLabel_1);

        JButton btnNewButton = new JButton("确认");
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));
        btnNewButton.setBounds(79, 168, 97, 38);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                // TODO Auto-generated method stub
                check();
            }
        });
        JButton btnNewButton_1 = new JButton("取消");
        btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 16));
        btnNewButton_1.setBounds(274, 168, 97, 38);
        frame.getContentPane().add(btnNewButton_1);

        psw = new JTextField();
        psw.setBounds(135, 103, 222, 32);
        frame.getContentPane().add(psw);
        psw.setColumns(10);
    }
    public void check() {
    
    
        if(psw.getText().equals("123456")&&acc.getText().equals("root")) {
    
    
            //JOptionPane.showMessageDialog(null, "登录成功!");
            f2.setVisible(true);
            frame.setVisible(false);

        }
        else if (psw.getText().trim().length() == 0 || new String(acc.getText()).trim().length() == 0) {
    
    
            JOptionPane.showMessageDialog(null, "用户名或密码不能为空!");
        }
        else {
    
    
            JOptionPane.showMessageDialog(null, "登录失败(用户名或密码错误)!");
        }
    }
}

Insert picture description here
Insert picture description here

Experimental experience:

1. You must have good indentation and comment habits for program writing, otherwise it will not be convenient for others to read your own code, and it is also not convenient for you to review and consolidate your own code in the future.
2. Be careful about the introduction and management of packages. In many cases, errors are reported because the corresponding classes and packages are not introduced.
3. For inheritance and object-oriented programming methods, you still have to practice hard to become more familiar with this programming method.
4. Think about the layout format of the graphical interface from the beginning, and don't change it later, it will be more difficult to adjust and cause unnecessary trouble.

to sum up:

I learned the basic skills of java for writing graphical interfaces and some basic knowledge about layout, and I have a more in-depth understanding of the use of swing and AWT. At the same time, it also deepened my familiarity and understanding of java object-oriented programming. In addition, I also felt the vastness of the entire Java ecosystem and the convenience of programming. I have more knowledge about my own learning, and I still have to explore more unknown technical fields that I don’t know myself.

Guess you like

Origin blog.csdn.net/david2000999/article/details/115129552