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

📄 messagegui.java.svn-base

📁 這是一個JAVA語言寫的多代理人程式用來模擬飛機起飛或是降落的程式
💻 SVN-BASE
字号:
package gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.util.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class MessageGUI extends JPanel{
	
	// Instantiate the constant variables
	private final int MAX_HEIGHT = 255;        // Max height of the base JPanel
	private final int MAX_OUTPUT_WIDTH = 500;  // Max width of the left hand column
	private final int MAX_SELECT_WIDTH = 150;  // Max width of the center hand column
	private final int MAX_SELECT2_WIDTH = 150; // Max width of the right hand column
	private final int BOTTOM_PADDING = 3;      // Padding between the two rows
	private final int RIGHT_PADDING = 10;      // Padding between the two columns
	
	// Instance Variables
	private int rows,cols;
	private GridBagConstraints c;
	
	// Instance variables - Top Panel[Main Message Panel]
	private JPanel mainMessage;
	private JTextArea output;                                       // The area in which the messages are rendered
	private JList select, select2;                                  // The list that manages the names of the agents (from and to respectively)
	private DefaultListModel listModel, listModel2;                 // The actual list of the agents that gets passed to the JList (from and to respectively)
	private JLabel labelOutput, labelSelect, labelSelect2;          // Labels for the two columns the message window and the agents' names, respectively 
	private JScrollPane scrollOutput, scrollSelect, scrollSelect2;  // The JScrollPane for the JTextarea and the JLists respectively
	private ArrayList <Message> messages;                           // The ArrayList holding all the Message objects
	
	public MessageGUI()
	{
		rows = 0;
		cols = 0;
		
		// Base Panel Layout Options  [DO NOT EDIT]
		c = new GridBagConstraints();
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.CENTER;
		setLayout(new GridBagLayout());
		
		// Set-up Top Panel[Main Message Panel]
		initializeMain();
		buildMainMessage();
		
		// Add Preliminary Panels
		c.gridx = rows;
		c.gridy = cols;
		cols++;
		add(mainMessage,c);		
	}
	
	private void initializeMain()
	{
		// Initialization Operations
		mainMessage = new JPanel();
		mainMessage.setLayout(new GridBagLayout());
		output = new JTextArea();
		listModel = new DefaultListModel();
		listModel2 = new DefaultListModel();
		select = new JList(listModel);
		select2 = new JList(listModel2);
		labelOutput = new JLabel("Messages");
		labelSelect = new JLabel("Agents-Sender");
		labelSelect2 = new JLabel("Agents-Receiver");
		messages = new ArrayList<Message>();
		
		listModel.addElement("All");
		listModel2.addElement("All");
		select.setSelectedIndex(0);
		select.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		select2.setSelectedIndex(0);
		select2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		output.setLineWrap(false);
		output.setEditable(false);
		scrollOutput = new JScrollPane(output); 
		scrollSelect = new JScrollPane(select); 
		scrollSelect2 = new JScrollPane(select2); 
		scrollOutput.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollSelect.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollSelect2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollOutput.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollSelect.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollSelect2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollOutput.setPreferredSize(new Dimension(MAX_OUTPUT_WIDTH, MAX_HEIGHT));
		scrollSelect.setPreferredSize(new Dimension(MAX_SELECT_WIDTH, MAX_HEIGHT));
		scrollSelect2.setPreferredSize(new Dimension(MAX_SELECT2_WIDTH, MAX_HEIGHT));
		
		
		// Add ListSelectionLisenter
		select.addListSelectionListener(new LS());
		select2.addListSelectionListener(new LS());
		
		// Original Start Of Displaying Messages
		String allMessages = "";
		for (int i=0; i < messages.size();i++)
		{
			allMessages += (messages.get(i)).getMessage() + "\n";
		}
		output.setText(allMessages);

	}
	private void buildMainMessage()
	{
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(0,0,BOTTOM_PADDING,RIGHT_PADDING);  //Changes bottom padding & right padding
		mainMessage.add(labelOutput,c);		

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		//c.insets = new Insets(0,0,BOTTOM_PADDING,0);  //Changes bottom padding & right padding
		mainMessage.add(labelSelect,c);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 2;
		c.gridy = 0;
		c.insets = new Insets(0,0,BOTTOM_PADDING,0);  //Changes bottom padding & right padding
		mainMessage.add(labelSelect2,c);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		c.insets = new Insets(0,0,0,RIGHT_PADDING);  //Changes bottom padding & right padding
		mainMessage.add(scrollOutput,c);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		//c.insets = new Insets(0,0,0,0);  //Changes bottom padding & right padding
		mainMessage.add(scrollSelect,c);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 2;
		c.gridy = 1;
		c.insets = new Insets(0,0,0,0);  //Changes bottom padding & right padding
		mainMessage.add(scrollSelect2,c);
		
		mainMessage.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
	}
	
	
	/**
	 * Adds a string object to listModel and updates the customer names located in the right hand column 
	 *
	 * @param   name  the name of the agent being added to the system.
	 * @return  void
	 */
	public void addAgent(String name)
	{	
		try
		{
			// main message panel
			listModel.addElement(name);
			listModel2.addElement(name);			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());			
		}
	}
	
	
	/**
	 * Adds a string object to messages.
	 * Updates the output(the messages being displayed) if the agent who is sending the messages if selected in
	 * select (the JList) or if the option "All" is selected. Same thing applies to the receiver's column
	 *
	 * @param   to  the name of the agent receiving the message
	 * @param   from  the name of the agent sending the message
	 * @param   message  the message being sent to & from agents
	 * @return  void
	 */
	public void logMessage(String from, String to, String message)
	{	
		try
		{
			messages.add(new Message(to, from, message));
			int [] index1, index2;
			index1 =  select.getSelectedIndices();
			index2 =  select2.getSelectedIndices();
			
			for (int i = 0; i<index1.length ;i++)
			{
				if ((index1[i] == 0) || (index1[i] > 0 && listModel.get(index1[i]).equals(from)))
				{		
					for (int n = 0; n<index2.length ;n++)
					{
						if ((index2[n] == 0) || (index2[n] > 0 && listModel2.get(index2[n]).equals(to)))
						{
							output.append( message +"\n");
						}
					}
				}
			}
			repaint();
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		
	}
	
	class LS implements ListSelectionListener
	{
		public void valueChanged(ListSelectionEvent e) 
		{	
			if (e.getValueIsAdjusting() == true)
			{
				String allMessages = "",
				from = "",		// variable for sender
				to= "",			// variable for receiver
				message= "";	// variable for message
				int [] index1, index2;
				boolean isDisplay;
				index1 =  select.getSelectedIndices();
				index2 =  select2.getSelectedIndices();
				
				for (int i=0; i < messages.size();i++)
				{
					from = messages.get(i).getFrom();		// re-evaluate variable for sender
					to = messages.get(i).getTo();			// re-evaluate variable for receiver
					message = messages.get(i).getMessage();	// re-evaluate variable for message
					isDisplay = false;
					for (int m = 0; !isDisplay && m<index1.length ;m++)
					{
						if ((index1[m] == 0) || (index1[m] > 0 && listModel.get(index1[m]).equals(from)))
						for (int n = 0; !isDisplay && n<index2.length ;n++)
						{
							if ((index2[n] == 0) || (index2[n] > 0 && listModel.get(index2[n]).equals(to)))
							{
								isDisplay = true;
							}
						}
					}
					if (isDisplay)
					{
						allMessages += message + "\n";
					}
				}
				output.setText(allMessages);
			}	
		}
	}
}

⌨️ 快捷键说明

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