ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

用图形界面实现过独木桥问题

2021-12-29 23:30:51  阅读:184  来源: 互联网

标签:bridge 独木桥 实现 图形界面 int add static oneWoodBridge new


要求采用GUI图形化界面模拟多个人通过独木桥的模拟。这个独木桥南北走向,只能容纳一个人过桥,现在桥的两侧分别有9人和8人,编写一个多线程程序,让这些人都能够安全地到达桥的对岸,要求每个人用一个线程表示,桥为共享资源,在过桥的过程中显示谁在过桥,且显示走向显示一下每次通过独木桥人的姓名。

  1. 用多线程技术实现多人过独木桥;
  2. 模拟不同速度过桥,速度可自行调整;
  3. 用面向对象方法设计程序。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Bridge {
    boolean Enable = false;

    synchronized void OpenBridge (String thisperson, String Direct, OneWoodBridge oneWoodBridge) throws InterruptedException {
        if (Enable) {
            Thread.sleep(100 * oneWoodBridge.time);
            notify();
            oneWoodBridge.directiontext.setText(Direct);
            oneWoodBridge.thispeopletext.setText(thisperson);
            System.out.println(thisperson + " " + Direct + " " + oneWoodBridge.time);
        }
    }

    synchronized void CloseBridge () throws InterruptedException {
        if (!Enable) {
            wait();
        }
    }
}

class Person implements Runnable {
    static int SouthernPerson = 9;
    static int NorthernPerson = 8;
    static String[] direct = {"South to North", "North to South"};
    static String[] name = {"xv", "lin", "zhang", "gu", "wu", "shui", "huang", "yuan", "liu", "lu", "zheng", "li", "luo", "fang", "hong", "he", "yang"};
    String direction;
    String thisperson;

    Bridge bridge;
    OneWoodBridge oneWoodBridge;

    static int GetPersonOfSouthern () {
        return SouthernPerson;
    }

    static int GetPersonOfNorthern () {
        return NorthernPerson;
    }

    static int SumOfPerson () {
        return SouthernPerson + NorthernPerson;
    }

    static String Direct (int flag) {
        if (flag == 0)
            return direct[0];
        else
            return direct[1];
    }

    static String Name (int counter) {
        return name[counter];
    }

    Person (int flag, int num, Bridge bridge, OneWoodBridge oneWoodBridge) {
        this.direction = Direct(flag);
        this.thisperson = Name(num);
        this.bridge = bridge;
        this.oneWoodBridge = oneWoodBridge;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (bridge.Enable) {
            try {
                bridge.OpenBridge(thisperson, direction, oneWoodBridge);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else {
            try {
                bridge.CloseBridge();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


public class OneWoodBridge extends JFrame implements ActionListener {
    static OneWoodBridge oneWoodBridge;
    int time = 10;

    JTextField directiontext = new JTextField(12);
    JTextField thispeopletext = new JTextField(12);

    public OneWoodBridge () {
        setTitle("OneWoodBridge");

        Container container = this.getContentPane();

        JPanel jPanel = new JPanel();
        JPanel panelsouth = new JPanel();

        JButton buttonaction = new JButton("Action");
        JButton buttonfast = new JButton("Fast");
        JButton button = new JButton("Normal");
        JButton buttonslow = new JButton("Slow");

        JLabel labeldirect = new JLabel("Direct");
        JLabel labelthisperson = new JLabel("This");
        JLabel jLabel = new JLabel("South <          > North");
        JLabel whitespace = new JLabel("                    SpeedChoice");

        jPanel.add(labeldirect);
        jPanel.add(directiontext);
        jPanel.add(labelthisperson);
        jPanel.add(thispeopletext);
        jPanel.add(jLabel);

        panelsouth.add(buttonaction);
        panelsouth.add(whitespace);
        panelsouth.add(buttonfast);
        panelsouth.add(button);
        panelsouth.add(buttonslow);

        container.add(jPanel, BorderLayout.NORTH);
        container.add(panelsouth,BorderLayout.SOUTH);

        buttonaction.addActionListener(this);
        buttonfast.addActionListener(new fastlistener());
        button.addActionListener(new Normallistener());
        buttonslow.addActionListener(new slowlistener());

        directiontext.setEditable(false);
        thispeopletext.setEditable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int Sum = Person.SumOfPerson();

        Bridge bridge = new Bridge();
        for (int i = 0; i < Person.GetPersonOfSouthern(); i++) {
            Thread thread = new Thread(new Person(0, i, bridge, oneWoodBridge));
            thread.start();
        }
        for (int i = Person.GetPersonOfNorthern(); i > 0 ; i--) {
            Thread thread = new Thread(new Person(1,Sum - i, bridge, oneWoodBridge));
            thread.start();
        }

        bridge.Enable = true;
    }

    private class fastlistener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            time = 5;
        }
    }

    private class Normallistener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            time = 10;
        }
    }

    private class slowlistener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            time = 30;
        }
    }

    public static void main(String[] args) {
        OneWoodBridge oneWoodBridge = new OneWoodBridge();
        OneWoodBridge.oneWoodBridge = oneWoodBridge;
        oneWoodBridge.setSize(600,300);
        oneWoodBridge.setVisible(true);
        oneWoodBridge.setResizable(false);
    }
}

标签:bridge,独木桥,实现,图形界面,int,add,static,oneWoodBridge,new
来源: https://blog.csdn.net/weixin_53360289/article/details/122226810

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有