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

📄 configframe.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      // add the textfield for the config file name
      m_configItem = new JTextField(50);
      m_configPanel.add(m_configItem,
                        new TableLayoutConstraints(0, 0, 0, 0,
          TableLayout.RIGHT,
          TableLayout.CENTER));
      m_configureButton = new JButton("Configure");
      m_configureButton.addActionListener(m_cbl);
      m_configPanel.add(m_configureButton,
                        new TableLayoutConstraints(1, 0, 1, 0,
          TableLayout.LEFT,
          TableLayout.CENTER));
      if (m_isRoot) {
        m_fileName = new JTextField("jgap.con");
        m_configPanel.add(m_fileName,
                          new TableLayoutConstraints(2, 0, 2, 0,
            TableLayout.RIGHT, TableLayout.CENTER));
        m_configButton = new JButton("Generate");
        m_configButton.addActionListener(m_cbl);
        m_configPanel.add(m_configButton,
                          new TableLayoutConstraints(3, 0, 3, 0,
            TableLayout.LEFT, TableLayout.CENTER));
      }
      else {
        m_configButton = new JButton("Save Configuration");
        m_configButton.addActionListener(m_cbl);
        m_configPanel.add(m_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 {
    // list that will display the available items
    private JList m_list;

    // model for list
    private DefaultListModel m_listModel;

    private JScrollPane m_listScroller;

    // list that will display the selected items
    private JList m_outList;

    // model for outList
    private DefaultListModel m_outListModel;

    private JScrollPane m_outListScroller;

    private ConfigListSelectionListener m_outListListener;

    // buttons to move data to/from lists
    private JButton m_lButton;

    private JButton m_rButton;

    // property object associated with this ListGroup
    private ConfigProperty m_prop;

    private ListButtonListener m_listBL;

    private ConfigFrame m_frame;

    /**
     * Constructor responsible for creating all items that go on the list
     * panel.
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    ListGroup(final ConfigFrame a_frame) {
      m_frame = a_frame;
      // create the List of values
      m_listModel = new DefaultListModel();
      m_list = new JList(m_listModel);
      m_list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
      m_list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
      m_list.setVisibleRowCount( -1);
      m_listScroller = new JScrollPane(m_list);
      m_listScroller.setPreferredSize(new Dimension(250, 80));
      // create the output list
      m_outListModel = new DefaultListModel();
      m_outList = new JList(m_outListModel);
      m_outList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      m_outList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
      m_outList.setVisibleRowCount( -1);
      m_outListScroller = new JScrollPane(m_outList);
      m_outListListener = new ConfigListSelectionListener(m_frame, m_outList);
      m_outList.getSelectionModel().addListSelectionListener(m_outListListener);
      m_outListScroller.setPreferredSize(new Dimension(250, 80));
      // The buttons to move data to/from outList
      m_listBL = new ListButtonListener(this);
      m_lButton = new JButton("<-");
      m_lButton.addActionListener(m_listBL);
      m_rButton = new JButton("->");
      m_rButton.addActionListener(m_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 m_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 a_prop the ConfigProperty object associated with this ListGroup
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    public void setProp(final ConfigProperty a_prop) {
      m_prop = a_prop;
    }

    /**
     * @return the JList containing the items to select from
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    public JList getList() {
      return m_list;
    }

    /**
     * @return DefaultListModel for the list
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    public DefaultListModel getListModel() {
      return m_listModel;
    }

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

    /**
     * @return Output JList
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    public JList getOutList() {
      return m_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 m_outListModel;
    }

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

    /**
     * @return the button to move items from outlist to list
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    public JButton getLButton() {
      return m_lButton;
    }

    /**
     * @return the button to move items from list to outlist
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    public JButton getRButton() {
      return m_rButton;
    }

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

    /**
     * Move selected items from list to the output list.
     *
     * @author Siddhartha Azad
     * @since 2.3
     */
    public void rightButtonPressed() {
      int[] indices = m_list.getSelectedIndices();
      for (int i = 0; i < indices.length; i++) {
        String removed = (String) m_listModel.remove(indices[0]);
        m_outListModel.addElement(removed);
      }
    }
  }
  /**
   * This class groups the property data structure along with the JLists
   * associated with it.
   *
   * @author Siddhartha Azad
   * @since 2.3
   */
  class TextGroup {
    private JTextField m_textField;

    private JLabel m_label;

    private ConfigProperty m_prop;

    TextGroup() {
      m_textField = new JTextField(20);
      m_label = new JLabel();
    }

    public ConfigProperty getProp() {
      return m_prop;
    }

    public void setProp(final ConfigProperty a_prop) {
      m_prop = a_prop;
    }

    public JTextField getTextField() {
      return m_textField;
    }

    public JLabel getLabel() {
      return m_label;
    }
  }
  /**
   * Listener for the Configure button.
   *
   * @author Siddhartha Azad
   * @since 2.3
   */
  class ConfigButtonListener
      implements ActionListener {
    private ConfigFrame m_frame;

    ConfigButtonListener(final ConfigFrame a_frame) {
      m_frame = a_frame;
    }

    public void actionPerformed(final ActionEvent a_e) {
      // configButton is pressed
      if (a_e.getActionCommand().equals("Configure")) {
        String conStr = m_configItem.getText();
        if (conStr.equals("")) {
          JOptionPane.showMessageDialog(null,
                                        "Configurable name is empty, cannot"
                                        + " configure.",
                                        "Configuration Error",
                                        JOptionPane.INFORMATION_MESSAGE);
        }
        else {
          try {
            Class conClass;
            m_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 {
              m_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.getInstance().showFrame(m_frame, m_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.getInstance().write(m_frame);
      }
    }
  }
  /**
   * Listener for list buttons to move items around.
   *
   * @author Siddhartha Azad
   * @since 2.3
   */
  public class ListButtonListener
      implements ActionListener {
    private ListGroup m_lg;

    ListButtonListener(final ListGroup a_lg) {
      m_lg = a_lg;
    }

    public void actionPerformed(final ActionEvent a_e) {
      // one of the list buttons is pressed
      if (a_e.getActionCommand().equals("<-")) {
        // from outList to list
        m_lg.leftButtonPressed();
      }
      else {
        // from list to outList
        m_lg.rightButtonPressed();
      }
    }
  }
  /**
   * Listener for changes in the list of items.
   *
   * @author Siddhartha Azad
   * @since 2.3
   */
  public class ConfigListSelectionListener
      implements ListSelectionListener {
    private JList m_list;

    private ConfigFrame m_frame;

    public ConfigListSelectionListener(final ConfigFrame a_frame,
                                       final JList a_list) {
      m_list = a_list;
      m_frame = a_frame;
    }

    public void valueChanged(final ListSelectionEvent a_e) {
      Object[] values = m_list.getSelectedValues();
      if (values.length > 0) {
        String value = (String) values[0];
        notifySelection(value);
      }
    }
  }
  /**
   * 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(final String a_value) {
    m_configItem.setText(a_value);
  }
}

⌨️ 快捷键说明

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