⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 busroutedialog.java

📁 A part public bus simulation system, mainly about map design, java file, groupwork, helpful to the b
💻 JAVA
字号:
//Import Java Libraries required
package GUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import tools.BusRoute;
import tools.MapData;
import tools.Tools.*;

//Import the Tools Package Classes

//##################### BUS ROUTE DIALOG #####################

//Bus Route Dialog:	Consists of the Bus Route List and a Delete Bus Route button 
//					Dialog appears only when the Bus Route Object is selected
//Bus Route List:	Displays all Bus Routes stored at the moment the dialog is 
//					accessed. When a Bus Route is selected the Map Canvas will
//					indicate the Bus Route accordingly
//Delete Bus Route:	Deletes current Bus Route selected.

//BusRouteDialog Class
public class BusRouteDialog extends JDialog {
	
	private CmeGUI frame;
	private MapData mapData;
	
	private String[] routes;			//String array containing Bus Route
	//Numbers
	
	//Components of Bus Route Dialog
	private JComboBox busRouteList;		//Bus Route List
	private JButton deleteBusRouteB;	//Delete Bus Route
	private JLabel busRouteLabel;		//Label of Bus Route Dialog
	
	//##################### CONSTRUCTOR #####################
	public BusRouteDialog(Frame parent, CmeGUI frame, MapData mapData, 
			boolean modal) {
		
		super(parent, modal);
		this.frame = frame;
		this.mapData = mapData;
		
		//Bus Route Dialog Layout Specifications
		setTitle("Bus Route List");
		setSize(175, 100);
		getContentPane().setLayout(new BorderLayout());
		
		//Bus Route Label
		busRouteLabel = new JLabel("Select Bus Route: ");
		getContentPane().add(busRouteLabel, BorderLayout.NORTH);
		
		//Bus Route List
		busRouteList = new JComboBox();
		
		//To obtain route numbers from bus route linked list of MapData class
		//attach each number to the String "Bus Route #" and add to a String 
		//array to be displayed in the ComboBox
		createBusRouteList();
		
		busRouteList.setSelectedIndex(0);
		busRouteList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				busRouteChoiceActionPerformed(evt);
			}
		});
		getContentPane().add(busRouteList, BorderLayout.CENTER);
		
		//Delete Bus Route
		deleteBusRouteB = new JButton();
		deleteBusRouteB.setText("Delete Bus Route");
		deleteBusRouteB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				deleteBusRouteActionPerformed(evt);
			}
		});
		getContentPane().add(deleteBusRouteB, BorderLayout.SOUTH);
		
		//##################### WINDOW CLOSING SETTINGS #####################
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){}});
		
	}//End of Constructor
	
	//##################### METHOD DEFINITIONS #####################
	
	//##################### CREATE BUS ROUTE LIST #####################	
	//To obtain route numbers from bus route linked list of MapData class
	//attach each number to the String "Bus Route #" and add to a String 
	//array to be displayed in the ComboBox and to update the Bus Route List 
	//after a Route Number has been changed
	//CreateBusRouteList()
	private void createBusRouteList() {
		
		int length = mapData.busRouteSize();
		String routeListItem;
		BusRoute busRoute;
		
		//No Bus Routes created yet
		if(length==0){
			routes = new String[1];
			routes[0] = new String("No Bus Routes available");
		}
		
		else{
			//Obtain list of Bus Routes to be displayed
			routes = new String[length];
			
			for(int i=0;i<length;i++)
			{
				busRoute = mapData.getOneBusRoute(i);
				routeListItem = new String("Bus Route #" +busRoute.getBusNum());
				routes[i] = routeListItem;
			}
		}
		//edited by SYS
		//refresh busRouteList,required as routes now point to new route
		if(length==0){
			busRouteList.removeAllItems();
			busRouteList.addItem(routes[0]);
			busRouteList.setSelectedIndex(0);
		}
		else{
			busRouteList.removeAllItems();
			for(int i=0;i<length;i++)
				busRouteList.addItem(routes[i]);
			busRouteList.setSelectedIndex(length-1);
			frame.setStatus(routes[length-1] + " Selected\n");
		}	
		
		//end edited by SYS
	}//End of createBusRouteList()
	
	//##################### UPDATE BUS ROUTE LIST #####################	
	//To update the Bus Route List after a Route Number has been changed
	//busRouteListUpdate()
	public void busRouteListUpdate() {
		//TODO MapCanvas to draw Bus Route at Index 0 in the list
		createBusRouteList();
		
	}//End of busRouteListUpdate()
	
        public void setMapData(MapData mapdata) { this.mapData = mapdata; }
        
	//##################### CHANGE SELECTED INDEX #####################	
	//To change the selected index for the Bus Route List when user selects Bus
	//Route from Map directly
	public void setSelected(int index) { busRouteList.setSelectedIndex(index); }
	
	//##################### BUS ROUTE SELECTED METHOD #####################	
	
	//busRouteChoiceActionPerformed()
	private void busRouteChoiceActionPerformed(ActionEvent evt) {
		
		int length = mapData.busRouteSize();
		
		//No Bus Routes created yet
		if(length==0){
			frame.setStatus("No Bus Routes available\n");
		}
		
		//Select chosen Bus Route	
		else{
			int choice = busRouteList.getSelectedIndex();
			//added by SYS
			if (choice<mapData.busRouteSize() &&
					choice>=0) {
				BusRoute busRouteSelected = mapData.getOneBusRoute(choice);
//				frame.setDisplayedBusRoute(busRouteSelected);
				
				//TODO Map Canvas to implement display of selected bus route
				//and display the bus route number in propertyPane accordingly
				frame.setStatus(routes[choice] + " Selected\n");
			}
			
			//end added by SYS
		}
	}//End of busRouteChoiceActionPerformed()
	
	//##################### DELETE BUS ROUTE METHOD #####################
	
	//deleteBusRouteActionPerformed()
	private void deleteBusRouteActionPerformed(ActionEvent evt) {
		//TODO Implement deleteBusRouteActionPerformed()
		int length = mapData.busRouteSize();
		String routeListItem;
		BusRoute busRoute;
		
		//No Bus Routes created yet
		if(length==0){
			frame.setStatus("No Bus Routes available\n");
			return;
		}
		
		else{
			//Delete Selected Bus Route and Update list of Bus Routes Displayed
			//routes = new String[length];
			int choice = busRouteList.getSelectedIndex();
			BusRoute busRouteSelected = mapData.removeBusRoute(choice);
			frame.setStatus(routes[choice] + " Deleted\n");
			
			busRouteListUpdate();
			
		}
		
	}//End of deleteBusRouteActionPerformed()
	
}//End of BusRouteDialog Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -