JPopupMenue和按钮

时间:2013-09-11 17:41:13

标签: java swing jbutton jpopupmenu

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

为每个按钮添加jpopupmenue - 单击其中一个按钮以显示其jpopupmenue时的工作以及何时:

  • 单击第一个 JMenueItem - 将触发jpopupmenue的按钮的颜色更改为红色;
  • 单击第二个JMenueItem - 将触发jpopupmenue的按钮颜色更改为蓝色;
  • 单击第三个JMenueItem - 将触发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) {

    }
}

2 个答案:

答案 0 :(得分:0)

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

这里发生的事情是,当正在调用2个Mouselistener时,您将获得2个按钮的颜色更改。

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

答案 1 :(得分: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 popup更改为MyPopupMenu 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);
        }
    });

}