我希望我的程序将执行以下操作:
对于添加 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) {
}
}