Java 鼠标中的Int值压在一个JPanel中,当Int值更改时不更新

Java 鼠标中的Int值压在一个JPanel中,当Int值更改时不更新,java,swing,menubar,actionevent,mousepress,Java,Swing,Menubar,Actionevent,Mousepress,我正在编写一个在网格中打印砖块的应用程序,我已经对它进行了设置,理想情况下,要更改颜色,菜单选择会更改一个int,该int设置砖块类中的颜色 我有两个面板,一个用于网格(画东西的地方),另一个用于菜单栏。如果我手动更改网格中的数字,它会工作,因此我认为菜单可能有问题,但我不确定。我想知道当int从菜单jpanel变为网格jpanel时,如何将它变为int 这是菜单代码: import java.awt.*; import java.awt.event.*; import javax.swing.

我正在编写一个在网格中打印砖块的应用程序,我已经对它进行了设置,理想情况下,要更改颜色,菜单选择会更改一个int,该int设置砖块类中的颜色

我有两个面板,一个用于网格(画东西的地方),另一个用于菜单栏。如果我手动更改网格中的数字,它会工作,因此我认为菜单可能有问题,但我不确定。我想知道当int从菜单jpanel变为网格jpanel时,如何将它变为int

这是菜单代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Selector extends JPanel implements Common, ActionListener{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Color colorValues[] = {Color.BLACK, Color.YELLOW, Color.RED, Color.ORANGE};
    public String colors[] = {"Black", "Yellow", "Red", "Orange"};
    public JMenuItem colorItems[];
    public int display;

    //constructor
    public Selector(){
        //set size and layout
        setPreferredSize(new Dimension(SELECTOR_WIDTH, SELECTOR_HEIGHT));
        setLayout(new BorderLayout());

        //menu bar
        JMenuBar bar = new JMenuBar();
        Font f = new Font("Helvetica", Font.BOLD, 15);

        //menus
        JMenu colorMenu = new JMenu("Colour");

        //create Color menu
        String colors[] = {"Black", "Yellow", "Red", "Orange"};
        colorItems = new JMenuItem[colors.length];

        for (int i = 0; i<colors.length; i++){
            colorItems [i] = new JMenuItem(colors[i]);
            colorMenu.add(colorItems[i]);
            colorItems[i].addActionListener(this);
        }// end of for loop


        //set all font the same
        UIManager.put("Menu.font", f);
        UIManager.put("MenuBar.font", f);
        UIManager.put("MenuItem.font", f);

        //add menus
        bar.add(colorMenu);

        //add menu bar
        add(bar, BorderLayout.PAGE_START);

    }//constructor end

    public void actionPerformed(ActionEvent e){

        if (e.getSource()==colorItems[0]){
            display=0;
        }
        else if (e.getSource()==colorItems[1]){
            display=1;
        }
        else if (e.getSource()==colorItems[2]){
            display=2;
        }
        else if (e.getSource()==colorItems[3]){
            display=3;
        }
    }
}//class end
你的问题是:

public class MapGrid extends JPanel implements Common, MouseListener{
    //...

    Selector s = new Selector();  // ******* HERE **********

    // ...

    public void mousePressed(MouseEvent evt) {
        // ....

            else{
                brick[index].setChoice(s.display);
                cell[index]=s.display;
            }
        // ....
    }

    //...
}
您正在上面创建一个新的选择器对象,但它可能与GUI中显示的选择器对象完全不同。因此,GUI持有和显示的选择器状态的更改不会反映在上面使用的选择器对象中

要解决此问题,请确保选择器变量引用GUI中显示的同一个选择器

e、 例如,将其更改为如下内容:

public class MapGrid extends JPanel implements Common, MouseListener{
    //...

    Selector s = null;

    public MapGrid(Selector s) {
      this.s = s;
    }

    // .... etc....

然后,当您创建MapGrid对象时,请确保传入对显示的true Selector实例的引用。

为了更快地获得更好的帮助,请发布一篇文章。@Andrew Thompson感谢您教人们如何正确提问,但在每个问题中看到相同的comente确实很奇怪:(@iShaalan“感谢您教人们如何正确提问”它不是“适当的”,而是“有助于获得答案”。使用“适当的”这个词特别聪明——请看原因。@iShaalan:你在这里逗留的时间越多,你对回答问题的贡献就越大,你就越能看到Andrew最初的评论有多大的帮助。如果原始海报上能贴出一个简短的compi我们可以自己编译和运行的标签程序,我们可以观察到他的问题,我们可以修改它来尝试解决他的问题,然后这通常会导致一个快速、完整和满足的解决方案。没有什么能真正打败这种结构,所以经常以一种非威胁性、非对抗性的方式建议它是非常有帮助的,穆不仅仅是你的评论。下次我会尽量让它更紧凑。但无论如何,谢谢你的帮助。我今天不上网。谢谢你的回答。这对我很有用。
public class MapGrid extends JPanel implements Common, MouseListener{
    //...

    Selector s = new Selector();  // ******* HERE **********

    // ...

    public void mousePressed(MouseEvent evt) {
        // ....

            else{
                brick[index].setChoice(s.display);
                cell[index]=s.display;
            }
        // ....
    }

    //...
}
public class MapGrid extends JPanel implements Common, MouseListener{
    //...

    Selector s = null;

    public MapGrid(Selector s) {
      this.s = s;
    }

    // .... etc....