编程语言
首页 > 编程语言> > Java语言 实验报告(二)

Java语言 实验报告(二)

作者:互联网

实验报告(二)

实验目的

实验任务 1

编写 Java 应用程序,实现以下登陆界面(需注意密码框输入的内容不显示明文)

在这里插入图片描述

实验源代码

package com.science.sc2.No1;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Main {
    public static void main(String[] args) {
        Frame frame = new Frame("登陆");
        frame.setSize(400,300);
        frame.setVisible(true);
        //添加监听器,点击右上角的X后关闭按钮
        frame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {
                frame.dispose();
            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });
        frame.setLayout(null);
        Label l1 = new Label("用户名:");
        Label l2 = new Label("密 码:");
        TextField username = new TextField(30);
        TextField password = new TextField(30);
        Button btn = new Button("确定");
        frame.add(l1);
        frame.add(l2);
        frame.add(username);
        frame.add(password);
        frame.add(btn);
        l1.setBounds(50,80,60,20);
        username.setBounds(110,80,200,20);
        l2.setBounds(50,120,60,20);
        password.setBounds(110,120,200,20);
        btn.setBounds(160,160,80,40);
        password.setEchoChar('*');  //设置回显字符
        frame.setLocationRelativeTo(null);   //默认显示在正中间
    }
}

实验运行截图

在这里插入图片描述

实验任务2

编写 Java 应用程序,实现以下界面:

在这里插入图片描述

实验源代码

package com.science.sc2.No2;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Main {
    public static void main(String[] args) {
        Frame frame = new Frame("Menu");
        frame.setSize(400,300);
        frame.setVisible(true);
        //添加监听器,点击右上角的X后关闭按钮
        frame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {
                frame.dispose();
            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);   //默认显示在正中间
        MenuBar menuBar = new MenuBar();
        frame.setMenuBar(menuBar);
        Menu menu1 = new Menu("File");
        Menu menu2 = new Menu("Format");
        Menu menu3 = new Menu("Help");
        menuBar.add(menu1);
        menuBar.add(menu2);
        menuBar.add(menu3);
        Menu menu22 = new Menu("进制");
        menu2.add(new MenuItem("中文"));
        menu2.add(menu22);
        menu22.add(new CheckboxMenuItem("二进制"));
        menu22.add(new CheckboxMenuItem("八进制"));
        menu22.add(new CheckboxMenuItem("十进制"));
    }
}

实验运行截图

在这里插入图片描述

实验任务 3

编写一个 Java 多线程应用程序,完成三个售票窗口同时出售 20 张票。具体要求如下:

运行效果参考下图:

在这里插入图片描述

实验源代码

package com.science.sc2.No3;

public class Main {
    public static final int DEFAULT_TICKET = 20;
    public static final int DEFAULT_STATION = 3;
    public static int ticket = 1;
    public static int station = DEFAULT_STATION;



    public static class Station extends Thread{
        @Override
        public void run() {
            while(true){
                synchronized (Main.class){
                    if(ticket <= DEFAULT_TICKET){
                        System.out.println(this.getName() + "卖出了第" + (ticket++) + "张票");
                        if(ticket > DEFAULT_TICKET){
                            System.out.println("票卖完了!");
                        }
                    }else{
                        break;
                    }

                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }

        public Station(String name) {
            super(name);
        }
    }

    public static void main(String[] args) {
        for (int i = 1; i <= station; i++) {
            new Station("窗口" + i).start();
        }

    }
}

实验运行截图

在这里插入图片描述

标签:Java,语言,frame,void,WindowEvent,Override,new,实验报告,public
来源: https://blog.csdn.net/qq_45740349/article/details/113783660