0

我希望我的程序将执行以下操作:

对于添加 jpopupmenue 的每个按钮,当单击其中一个按钮以显示其 jpopupmenue 时以及何时:

  • 单击第一个JMenuItem - 将触发 jpopupmenue 的按钮的颜色更改为红色
  • 单击第二JMenuItem - 将触发 jpopupmenue 的按钮的颜色更改为蓝色
  • 单击第三JMenuItem - 将触发 jpopupmenue 的按钮的颜色更改为黄色,仅此而已。

问题是,当我单击其中一个按钮并通过 jpopupmenue 更改其颜色时,单击的第二个按钮会自动更改为相同的颜色....

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 
 * @author shani moyal
 */
public class systemgraph extends JPanel implements ActionListener {

    // data//

    private int countplugs;
    JPopupMenu popup;
    private JButton btn;
    static JFrame frame;
    private JButton[] buttons;
    private BufferedImage bi;
    JMenuItem m2Frame1;
    JMenuItem m2Frame3;
    JMenuItem m2Frame2;

    public systemgraph(int size) {
        popup = new JPopupMenu();
        m2Frame1 = new JMenuItem("plug");
        m2Frame2 = new JMenuItem("second");
        m2Frame3 = new JMenuItem("third");
        popup.add(m2Frame1);
        popup.add(m2Frame2);
        popup.add(m2Frame3);
        popup.addMouseListener(null);

        buttons = new JButton[size];

        for (int i = 0; i < size; i++) {
            ImageIcon circle = new ImageIcon(
                    "C:\\Users\\shani moyal\\Desktop\\תמונות לגרפיקה         לפרויקט   \\imagesCA4ZS816.jpg");
            buttons[i] = new JButton((String.valueOf(i)));
            buttons[i].setName(String.valueOf(i));
            buttons[i].setBackground(Color.GREEN);
            buttons[i].setPreferredSize(new Dimension(80, 80));
            buttons[i].setEnabled(true);
            buttons[i].addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    // JButton button = (JButton)e.getSource();

                    final Component cmp = (Component) e.getSource();

                    popup.show(e.getComponent(), e.getX(), e.getY());
                    // popup.addMouseListener(this);

                    m2Frame1.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e) {
                            cmp.setBackground(Color.red);

                        }
                    });

                    m2Frame2.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e) {
                            cmp.setBackground(Color.BLUE);

                        }
                    });
                    m2Frame3.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e) {
                            cmp.setBackground(Color.YELLOW);

                        }
                    });

                    /*
                     * m2Frame1.addMouseListener(new MouseAdapter() {
                     * 
                     * @Override public void mousePressed(MouseEvent e) {
                     * cmp.setBackground(Color.MAGENTA);
                     * 
                     * } });
                     */

                }
            });

            this.add(buttons[i], BorderLayout.NORTH);

        }

    }

    private static void createAndShowGUI() {

        frame = new JFrame("ButtonDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Simple Fiber GUI system (K&CG lab. Ariel U.");
        systemgraph newContentPane = new systemgraph(6);
        newContentPane.setOpaque(true);

        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
4

2 回答 2

0

也许创建您自己的类,该类将扩展JPopupMenu并包含用于确定需要更改哪个按钮的字段。这个类看起来像

class MyPopupMenu extends JPopupMenu{

    private static final long serialVersionUID = 1L;

    private JButton button;

    public JButton getButton() {
        return button;
    }

    public void setButton(JButton button) {
        this.button = button;
    }
}

您将需要更改JPopupMenu popupMyPopupMenu popup,并且类中的构造函数内部的代码SystemGraph看起来像

public SystemGraph(int size) {
    popup = new MyPopupMenu();
    m2Frame1 = new JMenuItem("red");
    m2Frame2 = new JMenuItem("blue");
    m2Frame3 = new JMenuItem("yellow");
    popup.add(m2Frame1);
    popup.add(m2Frame2);
    popup.add(m2Frame3);
    popup.addMouseListener(null);

    buttons = new JButton[size];

    for (int i = 0; i < size; i++) {
        ImageIcon circle = new ImageIcon(
                "C:\\Users\\shani moyal\\Desktop\\תמונות לגרפיקה         לפרויקט   \\imagesCA4ZS816.jpg");
        buttons[i] = new JButton((String.valueOf(i)));
        buttons[i].setName(String.valueOf(i));
        buttons[i].setBackground(Color.GREEN);
        buttons[i].setPreferredSize(new Dimension(80, 80));
        buttons[i].setEnabled(true);
        final JButton tmp = buttons[i];
        buttons[i].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                popup.setButton(tmp);
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });

        this.add(buttons[i], BorderLayout.NORTH);

    }

    m2Frame1.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            MyPopupMenu p = (MyPopupMenu)e.getComponent().getParent();
            p.getButton().setBackground(Color.RED);
        }
    });

    m2Frame2.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            MyPopupMenu p = (MyPopupMenu)e.getComponent().getParent();
            p.getButton().setBackground(Color.BLUE);
        }
    });
    m2Frame3.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            MyPopupMenu p = (MyPopupMenu)e.getComponent().getParent();
            p.getButton().setBackground(Color.YELLOW);
        }
    });

}
于 2013-09-11T20:23:25.887 回答
0

当您第一次按下按钮时,您正在将 MouseListener 添加到 m2Frame1。当您按下另一个按钮时,您正在添加另一个MouseListener,但请注意您并没有删除第一个。因此,当您单击 JMenuItem m2Frame1 时,您将调用两个 MouseListener。

这里发生的情况是,当调用 2 个 Mouselisteners 时,您将更改 2 个按钮的颜色。

您应该尝试在代码开头只向 m2Frame1 添加一个 MouseListener。当然,您需要弄清楚如何让 JMenuItem 选择它必须修改的按钮。

于 2013-09-11T18:30:42.703 回答