📄 ui.java
字号:
/*
**********************************************************************************************************************
Wal-Mart Checkout Simulation
By: Dustin Grimmeissen and Richard Anderson
-> UI Class
This class generates the graphical user interface for the Wal-Mart simulation program.
**********************************************************************************************************************
*/
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class UI implements ActionListener
{
Simulation TheApp;
boolean SimulationStarted = false;
JFrame frame = new JFrame("Wal-Mart Checkout Simulation");
JButton StartButton = new JButton(new ImageIcon("images/StartButton.gif"));
JButton ExitButton = new JButton(new ImageIcon("images/ExitButton.gif"));
ImageIcon BackgroundImage = new ImageIcon("images/Background.gif");
JLabel Background = new JLabel(BackgroundImage);
JPanel MainPanel = new JPanel();
JTextArea ProcessText = new JTextArea();
JTextArea FinishedText = new JTextArea();
JScrollPane ScrollBox = new JScrollPane(FinishedText);
//------------------------------------------------------------------------
// Constructor - The constructor keeps a pointer back to the simulation
// application and initializes the program's buttons.
//------------------------------------------------------------------------
public UI( Simulation App )
{
TheApp = App;
StartButton.addActionListener(this);
ExitButton.addActionListener(this);
}
//------------------------------------------------------------------------
// Initialize - Places all labels, buttons, and text fields into the graphical
// user interface and prepares them to display information from
// the simulation program.
//------------------------------------------------------------------------
public void Initialize()
{
// place all labels, buttons, and text fields into the JFrame
MainPanel.setOpaque(false);
frame.setSize(400, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Background.setBounds(0, 0, 400, 700);
frame.getLayeredPane().add(Background, new Integer(Integer.MIN_VALUE));
frame.setContentPane( MainPanel );
MainPanel.setLayout(new BoxLayout(MainPanel, BoxLayout.Y_AXIS));
MainPanel.setOpaque(false);
StartButton.setAlignmentX(Component.CENTER_ALIGNMENT);
ExitButton.setAlignmentX(Component.CENTER_ALIGNMENT);
MainPanel.add(Box.createRigidArea(new Dimension(0,105)));
MainPanel.add(StartButton);
MainPanel.add(Box.createRigidArea(new Dimension(0,5)));
MainPanel.add(ExitButton);
MainPanel.add(Box.createRigidArea(new Dimension(0,15)));
ProcessText.setMaximumSize(new Dimension(375,80));
MainPanel.add(ProcessText);
MainPanel.add(Box.createRigidArea(new Dimension(0,15)));
FinishedText.setFont(new Font("Courier New", Font.PLAIN, 11));
ScrollBox.setMaximumSize(new Dimension(375,340));
MainPanel.add(ScrollBox);
frame.setVisible(true);
}
//------------------------------------------------------------------------
// FinishSimulation - Prints the final results of the simulation in the
// results text box.
//------------------------------------------------------------------------
public void FinishSimulation(double NumCustomers[], double TotalWait[], double TotalResponse[], double ArrivalRate, Queue CheckoutLane[], int NumberLanes)
{
StartButton.setIcon(new ImageIcon("images/StartButton.gif"));
SimulationStarted = false;
String FinishText = new String("");
for (int i = 0; i < (NumberLanes + 1); i++)
{
if (i != NumberLanes)
FinishText += (" Queue " + (i+1) + " --- Arrival: " + (ArrivalRate*(1.0/(double)(NumberLanes))) + "\n Departure: " + CheckoutLane[i].getRate() + "\n Customers: " + NumCustomers[i] + "\n Avg. Wait: " + (TotalWait[i])/(NumCustomers[i]) + "\n Avg. Response: " + (TotalResponse[i])/(NumCustomers[i]) + "\n\n");
else
FinishText += (" Total --- Customers: " + NumCustomers[i] + "\n Avg. Wait: " + (TotalWait[i])/(NumCustomers[i]) + "\n Avg. Response: " + (TotalResponse[i])/(NumCustomers[i]));
}
FinishedText.setText(FinishText);
}
//------------------------------------------------------------------------
// setProcessText - Updates the text that displays each queue length.
//------------------------------------------------------------------------
public void setProcessText(String text)
{
ProcessText.setText(text);
}
//------------------------------------------------------------------------
// actionPerformed - This function handles all button clicks throughout the
// program. If the start button is clicked a series of
// pop-up menus appear asking the user to enter the
// number of queues, the arrival rate, and the service
// rates.
//------------------------------------------------------------------------
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == this.ExitButton)
{
System.exit(0);
}
else if (e.getSource() == this.StartButton)
{
if (!SimulationStarted)
{
boolean cancel = false;
int NumQueues = 1;
int NumExpress = 0;
double ArrivalRate = 0.5;
double RegularLane = 0.1667;
double ExpressLane = 0.3333;
String NumberQueues = JOptionPane.showInputDialog("How Many Queues Would You Like To Simulate? (1-5): ");
if (NumberQueues == null)
{
cancel = true;
}
else
{
try
{
NumQueues = Integer.parseInt(NumberQueues);
if ((NumQueues < 1) || (NumQueues > 5))
{
JOptionPane.showMessageDialog(null, "Incorrect Input", "Error", JOptionPane.ERROR_MESSAGE);
cancel = true;
}
else
{
int exit = 0;
while (exit == 0)
{
exit = 1;
String Arrival = JOptionPane.showInputDialog("Enter The Arrival Rate: ");
try
{
ArrivalRate = Double.parseDouble(Arrival);
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(null, "Incorrect Input, Try Again", "Error", JOptionPane.ERROR_MESSAGE);
exit = 0;
}
}
exit = 0;
while (exit == 0)
{
exit = 1;
String Regular = JOptionPane.showInputDialog("Enter The Service Rate: ");
try
{
RegularLane = Double.parseDouble(Regular);
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(null, "Incorrect Input, Try Again", "Error", JOptionPane.ERROR_MESSAGE);
exit = 0;
}
}
int ExpressYesNo = JOptionPane.showConfirmDialog(null, "Would You Like To Have Express Lanes?", "Express Lanes", JOptionPane.YES_NO_OPTION);
if (ExpressYesNo == JOptionPane.YES_OPTION)
{
exit = 0;
while (exit == 0)
{
exit = 1;
String Express = JOptionPane.showInputDialog("Enter The Service Rate For Express Lanes: ");
try
{
ExpressLane = Double.parseDouble(Express);
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(null, "Incorrect Input, Try Again", "Error", JOptionPane.ERROR_MESSAGE);
exit = 0;
}
}
exit = 0;
while (exit == 0)
{
exit = 1;
String ExpressNumber = JOptionPane.showInputDialog("How Many Express Lanes Do You Want? (1-" + NumQueues + "): ");
try
{
NumExpress = Integer.parseInt(ExpressNumber);
if ((NumExpress < 1) || (NumExpress > NumQueues))
{
JOptionPane.showMessageDialog(null, "Incorrect Input, Try Again", "Error", JOptionPane.ERROR_MESSAGE);
exit = 0;
}
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(null, "Incorrect Input, Try Again", "Error", JOptionPane.ERROR_MESSAGE);
exit = 0;
}
}
}
}
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(null, "Incorrect Input", "Error", JOptionPane.ERROR_MESSAGE);
cancel = true;
}
}
if (cancel == false)
{
StartButton.setIcon(new ImageIcon("images/RunningButton.gif"));
SimulationStarted = true;
TheApp.setValues(NumQueues, NumExpress, ArrivalRate, RegularLane, ExpressLane);
Thread newThread = new Thread(TheApp, "Simulation");
newThread.start();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -