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

📄 configframe.java

📁 java实现的遗传算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			// add the textfield for the config file name
			configItem = new JTextField(50);
			configPanel.add(configItem,
					new TableLayoutConstraints(0, 0, 0, 0,
					TableLayout.RIGHT, TableLayout.CENTER));
			configureButton = new JButton("Configure");
			configureButton.addActionListener(cbl);
			configPanel.add(configureButton,
					new TableLayoutConstraints(1, 0, 1, 0,
					TableLayout.LEFT, TableLayout.CENTER));
			
			if(isRoot) {
				fileName = new JTextField("jgap.con");
				configPanel.add(fileName,
						new TableLayoutConstraints(2, 0, 2, 0,
						TableLayout.RIGHT, TableLayout.CENTER));
				configButton = new JButton("Generate");
				configButton.addActionListener(cbl);
				configPanel.add(configButton,
						new TableLayoutConstraints(3, 0, 3, 0,
						TableLayout.LEFT, TableLayout.CENTER));
			}
			else {
				configButton = new JButton("Save Configuration");
				configButton.addActionListener(cbl);
				configPanel.add(configButton,
						new TableLayoutConstraints(3, 0, 3, 0,
						TableLayout.LEFT, TableLayout.CENTER));
			}

		}
		catch(Exception ex) {
			JOptionPane.showMessageDialog( null ,
					"Exception"+ex.toString(),
					"This is the title",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	/**
	 * This class groups the property data structure along with the JLists
	 * associated with it.
	 * @author Siddhartha Azad
	 * @since 2.3
	 * */
	public class ListGroup {
		/**
		 * Constructor responsible for creating all items that go on the list
		 * panel.
		 * @author Siddhartha Azad
		 * @since 2.3
		 */
		ListGroup(ConfigFrame _frame) {
			frame = _frame;
			// create the List of values
			listModel = new DefaultListModel();
			list = new JList(listModel);
			list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
			list.setVisibleRowCount(-1);
			listScroller = new JScrollPane(list);
			listScroller.setPreferredSize(new Dimension(250, 80));
			
			
			// create the output list
			outListModel = new DefaultListModel();
			outList= new JList(outListModel);
			outList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			outList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
			outList.setVisibleRowCount(-1);
			outListScroller = new JScrollPane(outList);
			outListListener = new ConfigListSelectionListener(frame, outList);
			outList.getSelectionModel().addListSelectionListener
				(outListListener);
			outListScroller.setPreferredSize(new Dimension(250, 80));

			// The buttons to move data to/from outList
			listBL = new ListButtonListener(this);
			lButton = new JButton("<-");
			lButton.addActionListener(listBL);
			rButton = new JButton("->");
			rButton.addActionListener(listBL);
		}

		/**
		 * Getter for the ConfigProperty object associated with this ListGroup.
		 * @return The ConfigProperty object associated with this ListGroup.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public ConfigProperty getProp() {
			return prop;
		}

		/**
		 * Setter for the ConfigProperty object associated with this ListGroup.
		 * This object is used to retrieve the values that the list is initialized
		 * with.
		 * @param _prop The ConfigProperty object associated with this ListGroup.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public void setProp(ConfigProperty _prop) {
			prop = _prop;
		}

		/**
		 * Getter for the list.
		 * @return The JList containing the items to select from.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public JList getList() {
			return list;
		}

		/**
		 * Getter for the list's associated model.
		 * @return DefaultListModel for the list.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public DefaultListModel getListModel() {
			return listModel;
		}

		/**
		 * Getter for the scroller for the list.
		 * @return scroller for the list.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public JScrollPane getListScroller() {
			return listScroller;
		}

		/**
		 * Getter for the output list
		 * @return Output JList.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public JList getOutList() {
			return outList;
		}

		/**
		 * Getter for the output list's associated model.
		 * @return DefaultListModel for the output list.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public DefaultListModel getOutListModel() {
			return outListModel;
		}

		/**
		 * Getter for the scroller for the output list.
		 * @return scroller for the output list.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public JScrollPane getOutListScroller() {
			return outListScroller;
		}

		/**
		 * Return left button for this ListGroup
		 * @return The button to move items from outlist to list.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public JButton getLButton() {
			return lButton;
		}

		/**
		 * Return right button for this ListGroup
		 * @return The button to move items from list to outlist.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public JButton getRButton() {
			return rButton;
		}

		/**
		 * Move selected items from the output list back to the list.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 * */
		public void leftButtonPressed() {
			int indices [] = outList.getSelectedIndices();
			for(int i = 0; i < indices.length; i++) {
				String removed = (String)outListModel.remove(indices[0]);
				listModel.addElement(removed);
			}
		}

		/**
		 * Move selected items from list to the output list.
		 * @author Siddhartha Azad.
		 * @since 2.3
		 */
		public void rightButtonPressed() {
			int indices [] = list.getSelectedIndices();
			for(int i = 0; i < indices.length; i++) {
				String removed = (String)listModel.remove(indices[0]);
				outListModel.addElement(removed);
			}
		}

		// list that will display the available items
		private JList list;
		// model for list
		private DefaultListModel listModel;
		private JScrollPane listScroller;
		// list that will display the selected items
		private JList outList;
		// model for outList
		private DefaultListModel outListModel;
		private JScrollPane outListScroller;
		private ConfigListSelectionListener outListListener;
		// buttons to move data to/from lists
		private JButton lButton;
		private JButton rButton;
		// property object associated with this ListGroup
		private ConfigProperty prop;
		private ListButtonListener listBL;
		private ConfigFrame frame;
	}

	/**
	 * This class groups the property data structure along with the JLists
	 * associated with it.
	 * @author Siddhartha Azad
	 * @since 2.3
	 * */
	class TextGroup {
		TextGroup() {
			textField = new JTextField(20);
			label = new JLabel();
		}

		public ConfigProperty getProp() {
			return prop;
		}

		public void setProp(ConfigProperty _prop) {
			prop = _prop;
		}

		public JTextField getTextField() {
			return textField;
		}

		public JLabel getLabel() {
			return label;
		}

		private JTextField textField;
		private JLabel label;
		private ConfigProperty prop;
	}

	/**
	 * Listener for the Configure button.
	 * @author Siddhartha Azad.
	 * @since 2.3
	 * */
	class ConfigButtonListener implements ActionListener {
		ConfigButtonListener(ConfigFrame _frame) {
			frame = _frame;
		}
		public void actionPerformed(ActionEvent e) {
			// configButton is pressed
			if(e.getActionCommand().equals("Configure")) {
				String conStr = configItem.getText();
				if(conStr.equals("")) {
					JOptionPane.showMessageDialog( null ,
							"Configurable name is empty, cannot configure.",
							"Configuration Error",
							JOptionPane.INFORMATION_MESSAGE);
				}
				else {
					try{
						Class conClass;
						conObj = null;
						try{
							conClass = Class.forName(conStr);
						}
						catch(ClassNotFoundException cnfEx) {
							JOptionPane.showMessageDialog( null ,
									cnfEx.getMessage(),
									"Configuration Error: Class not found",
									JOptionPane.INFORMATION_MESSAGE);
							return;
						}
						try{
							conObj = (Configurable)conClass.
								newInstance();
						}
						catch(Exception ex) {
							JOptionPane.showMessageDialog( null ,
									ex.getMessage(),
									"Configuration Error:Could not create object",
									JOptionPane.INFORMATION_MESSAGE);	
							return;
						}
						try {
							SwingUtilities.invokeLater(new Runnable() {
						        public void run() {
						        	try {
						        		GUIManager.instance().showFrame(frame, conObj);
						        	}
						        	catch(Exception ex) {
						        		JOptionPane.showMessageDialog( null ,
												ex.getMessage(),
												"Configuration Error:Could not create new Frame",
												JOptionPane.ERROR_MESSAGE);
						        	}
						        }
							});
						}
						catch(Exception ex) {
							JOptionPane.showMessageDialog( null ,
									ex.getMessage(),
									"Configuration Error:Could not create new frame",
									JOptionPane.ERROR_MESSAGE);
						}
					}
					catch(Exception ex) {
						JOptionPane.showMessageDialog( null ,
								ex.getMessage(),
								"Configuration Error",
								JOptionPane.INFORMATION_MESSAGE);
					}
				}
			}
			else {
				// generate the config file
				ConfigWriter.instance().write(frame);
			}
			
		}
		ConfigFrame frame;

	}
	/**
	 * Listener for list buttons to move items around.
	 * @author Siddhartha Azad.
	 * @since 2.3
	 */
	public class ListButtonListener implements ActionListener {
		ListButtonListener(ListGroup _lg) {
			lg = _lg;
		}
		public void actionPerformed(ActionEvent e) {
			// one of the list buttons is pressed
			if(e.getActionCommand().equals("<-")) {
				// from outList to list
				lg.leftButtonPressed();
			}
			else {
				// from list to outList
				lg.rightButtonPressed();
			}

		}
		private ListGroup lg;

	}

	/**
	 * Listener for changes in the list of items.
	 * @author Siddhartha Azad.
	 * @since 2.3
	 */
	public class ConfigListSelectionListener implements ListSelectionListener {
		public ConfigListSelectionListener(ConfigFrame _frame, JList _list) {
			list = _list;
			frame = _frame;
		}
		public void valueChanged(ListSelectionEvent e) {
			Object values[] = list.getSelectedValues();
			if(values.length > 0) {
				String value = (String)values[0];
				notifySelection(value);
			}
		}
		private JList list;
		private ConfigFrame frame;
	}
	
	/**
	 * Notify the frame that a value has been selected in the output list for
	 * further configuration.
	 * @author Siddhartha Azad.
	 * @since 2.3
	 */
	private void notifySelection(String value) {
		configItem.setText(value);
	}
	

}

⌨️ 快捷键说明

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