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

📄 camochoicedialog.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        panel.add(new JLabel(Messages.getString("CamoChoiceDialog.keep_old_camo")), layout); //$NON-NLS-1$        layout.gridy++;        // Create the "keep old camo" button.        keep = new JButton();        keep.setText(Messages.getString("CamoChoiceDialog.no_camo")); //$NON-NLS-1$        keep.setPreferredSize(new Dimension(84, 72));        keep.addActionListener(new ActionListener() {            // Pressing this button closes without saving.            public void actionPerformed(ActionEvent event) {                close();            }        });        keep.addKeyListener(new KeyAdapter() {            public void keyPressed(KeyEvent event) {                // Pressing enter on this button closes without saving.                if (event.getKeyCode() == KeyEvent.VK_ENTER) {                    close();                }            }        });        panel.add(keep, layout);        layout.gridy++;        // Add a label for the "select new camo" button.        panel.add(new JLabel(Messages.getString("CamoChoiceDialog.select_new_camo")), layout); //$NON-NLS-1$        layout.gridy++;        // Create the "select new camo" button.        select = new JButton();        select.setText(Messages.getString("CamoChoiceDialog.no_camo")); //$NON-NLS-1$        select.setPreferredSize(new Dimension(84, 72));        select.addActionListener(this);        panel.add(select, layout);        // Fire the "select new camo" action when the enter key is pressed        // on either the list or the "select new camo" button.        KeyAdapter enterAdapter = new KeyAdapter() {            public void keyPressed(KeyEvent event) {                if (event.getKeyCode() == KeyEvent.VK_ENTER) {                    actionPerformed                            (new ActionEvent(select, event.getID(),                                    select.getActionCommand()));                }            }        };        items.addKeyListener(enterAdapter);        select.addKeyListener(enterAdapter);        // Set the "previously selected" values to the defaults.        setPrevSelection(Player.NO_CAMO, Player.colorNames[0]);        // Fill the item list with the colors.        fillList(Player.NO_CAMO);        // Perform the initial layout.        pack();    }    /**     * Handle the "select new camo" button's action.     * <p/>     * Implements <code>ActionListener</code>.     *     * @param event - the <code>ActionEvent</code> that was invoked.     */    public void actionPerformed(ActionEvent event) {        // Did the worker change their selection?        String curCat = (String) categories.getSelectedItem();        String curItem = (String) items.getSelectedValue();        if (!curCat.equals(prevCat) || !curItem.equals(prevItem)) {            // Save the new values.            setPrevSelection(curCat, curItem);            // Are there any listeners?            if (!listeners.isEmpty()) {                // Get the Image.                Image image = null;                Image[] array = (Image[]) getSelectedObjects();                if (array != null) image = array[0];                // Create an ItemEvent for the change.                ItemEvent alert = new ItemEvent                        (this, event.getID(), image, ItemEvent.ITEM_STATE_CHANGED);                // Alert the listeners.                Enumeration alerts = listeners.elements();                while (alerts.hasMoreElements()) {                    ((ItemListener) alerts.nextElement())                            .itemStateChanged(alert);                }            } // End have-listeners        } // End selection-changed        // Now exit.        close();    }    public void itemStateChanged(ItemEvent event) {        updateButton();    }    /**     * Update the "select new camo" button whenever a list item is selected.     * <p/>     */    private void updateButton() {        // Get the category and the index of the selected item.        String curCat = (String) categories.getSelectedItem();        // If a "no camo" item is selected, clear the image.        if (Player.NO_CAMO.equals(curCat)) {            int colorInd = items.getSelectedIndex();            if (colorInd == -1) {                for (int color = 0; color < Player.colorNames.length; color++) {                    if (Player.colorNames[color].equals(prevItem)) {                        colorInd = color;                        break;                    }                }            }            if (colorInd == -1) return;            select.setIcon(null);            select.setBackground(PlayerColors.getColor(colorInd));            return;        }        // Replace the ROOT_CAMO string with "".        if (Player.ROOT_CAMO.equals(curCat)) {            curCat = ""; //$NON-NLS-1$        }        // Clear the background and try to set the camo image.        try {            select.setBackground(categories.getBackground());            String back = (String) items.getSelectedValue();            if (back != null) {                Image img = (Image) camos.getItem(curCat, back);                if (img != null) {                    select.setIcon(new ImageIcon(img));                } else {                    select.setIcon(null);                }            } else {                select.setIcon(null);            }        } catch (Exception err) {            // Print the stack trace and display the message.            err.printStackTrace();            JOptionPane.showMessageDialog(frame, err.getMessage(), Messages.getString("CamoChoiceDialog.error_getting_camo"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$        }    }    /**     * Get the most recently selected (and confirmed) image.  The player     * must click the "select new camo" button to change this value.     * <p/>     * Implements <code>ItemSelectable</code>.     *     * @return If the player has selected from the "no camo" category,     *         or if an error occurs in getting the selected image,     *         a <code>null</code> is returned.  Otherwise, the array     *         will contain a single <code>Image</code>.     */    public Object[] getSelectedObjects() {        // Update the prev selection.        setPrevSelection((String) categories.getSelectedItem(), (String) items.getSelectedValue());        // Return a null if the "no camo" category is selected.        if (Player.NO_CAMO.equals(prevCat)) return null;        // Try to get the selected camo's Image.        // Don't forget to translate the ROOT_CAMO.        Image image = null;        try {            String curCat = prevCat;            if (Player.ROOT_CAMO.equals(curCat)) {                curCat = ""; //$NON-NLS-1$            }            image = (Image) camos.getItem(curCat, prevItem);        } catch (Exception err) {            // Print the stack trace and display the message.            err.printStackTrace();            JOptionPane.showMessageDialog(frame, err.getMessage(), Messages.getString("CamoChoiceDialog.error_getting_camo"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$        }        if (image == null) return null;        // Return an array containing the image.        Image[] ret = new Image[1];        ret[0] = image;        return ret;    }    /**     * Add an <code>ItemListener</code> that wants to be alerted when a     * new camo is selected.     * <p/>     * Implements <code>ItemSelectable</code>.     *     * @param listener - the <code>ItemListener</code> to be alerted.     */    public void addItemListener(ItemListener listener) {        // Don't add a listener multiple times.        if (!listeners.contains(listener)) {            listeners.addElement(listener);        }    }    /**     * Remove an <code>ItemListener</code> that wants to stop be alerted     * when a new camo is selected.     * <p/>     * Implements <code>ItemSelectable</code>.     *     * @param listener - the <code>ItemListener</code> to be alerted.     */    public void removeItemListener(ItemListener listener) {        listeners.removeElement(listener);    }    /**     * Get the selected category.     *     * @return the <code>String</code> name of the most recently selected     *         category.  This value will not be <code>null</code>.     */    public String getCategory() {        return prevCat;    }    /**     * Get the selected item's name.  If the most recently selected category     * is <code>Player.NO_CAMO</code>, then the item named is a color from     * <code>Player.colorNames</code>.     *     * @return the <code>String</code> name of the most recently selected     *         item.  This value will not be <code>null</code>.     */    public String getItemName() {        return prevItem;    }    /**     * Set the selected category.     *     * @param category - the <code>String</code> name of the desired     *                 category.  This value may be <code>null</code>.  If no     *                 match is found, the category will not change.     */    public void setCategory(String category) {        // Get the current selection.        String cur = (String) categories.getSelectedItem();        // Do nothing, if the request is for the selected item.        if (!cur.equals(category)) {            // Try to find the requested item.            for (int loop = 0; loop < categories.getItemCount(); loop++) {                // Did we find it?                if (categories.getItemAt(loop).equals(category)) {                    // Select this position.                    categories.setSelectedIndex(loop);                    // Fill the list.                    fillList(category);                    // Stop looking for the category.                    break;                } // End found-requested-category            } // Check the next category        } // End new-selection    }    /**     * Set the selected item in the currently-selected category.     *     * @param item - the <code>String</code> name of the desired item.     *             This value may be <code>null</code>.  If no match is     *             found, the item selection will not change.     */    public void setItemName(String item) {        // Do nothing is we're passed a null.        if (item != null) {            // Get the current selection.            String cur = (String) items.getSelectedValue();            // Do nothing, if the request is for the selected item.            if (!item.equals(cur)) {                items.setSelectedValue(item, true);            } // End new-selection        } // End not-passed-null    }    /**     * Show the dialog.  Make sure that all selections have been applied.     * <p/>     * Overrides <code>Dialog#setVisible(boolean)</code>.     */    public void setVisible(boolean visible) {        // Make sure the "keep" button is set correctly.        setPrevSelection((String) categories.getSelectedItem(),                (String) items.getSelectedValue());        // Make sure the "select" button is set correctly.        updateButton();        // Now show the dialog.        super.setVisible(visible);    }    public void valueChanged(ListSelectionEvent event) {        updateButton();    }}

⌨️ 快捷键说明

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