JMenuItem上的JAVA:Swing-actionPerformed()会激发,但更新的值不会持久

JMenuItem上的JAVA:Swing-actionPerformed()会激发,但更新的值不会持久,java,swing,event-handling,actionlistener,jmenubar,Java,Swing,Event Handling,Actionlistener,Jmenubar,好的,我一直在为我一直在开发的opengl图形引擎构建一个简单的基于Java Swing的GUI 我遇到的问题是菜单栏没有按我所希望的那样工作。具体来说,我有一个MenuBarBuilder类,用于构建各种菜单、子菜单和菜单项。添加菜单项时,将为其分配一个新的actionEventListener,并为其分配一个简单的任务,即将布尔值设置为true;此布尔值存储在与该菜单关联的状态列表中。将菜单项添加到菜单时,将创建每个菜单项状态。状态作为“State”类型的对象存储在“StatesList”类

好的,我一直在为我一直在开发的opengl图形引擎构建一个简单的基于Java Swing的GUI

我遇到的问题是菜单栏没有按我所希望的那样工作。具体来说,我有一个MenuBarBuilder类,用于构建各种菜单、子菜单和菜单项。添加菜单项时,将为其分配一个新的actionEventListener,并为其分配一个简单的任务,即将布尔值设置为true;此布尔值存储在与该菜单关联的状态列表中。将菜单项添加到菜单时,将创建每个菜单项状态。状态作为“State”类型的对象存储在“StatesList”类中

在MenuBarBuilder类中,需要作为一个单元运行的每个菜单项都作为“组”添加。完成此操作后,将向每个菜单项添加第二个操作事件侦听器,该侦听器将菜单组的所有关联值设置为false。最终,这将确保组中只有一个菜单项的状态列表设置为true

单击菜单项时,将触发操作侦听器事件。激发时,他们会打印一个列表,以确定激发顺序,并确保状态值设置为预期值,请参见以下内容:

setfalse
list
MenuItem1Name: false
MenuItem2Name: false

MenuItem1Name
setTrue
list
MenuItem1Name: true
MenuItem2Name: false

setfalse
list
MenuItem1Name: false
MenuItem2Name: false

MenuItem2Name
setTrue
list
MenuItem1Name: false
MenuItem2Name: true
这可确保事件按正确顺序触发。我们可以看到,当按下MenuItem1Name按钮时,值被正确设置为false,相关字段随后更新为true。MenuItem2Name也是如此。现在我们来谈谈实际问题

当我获取状态列表并通过GUI类检查循环中的值时,我只从状态列表中获取“false”值。我使用Swing工具栏以类似的方式实现了这个模式,它工作得很好,但我无法理解为什么我无法检索动作侦听器事件正确设置的值。我看不到任何地方的值被覆盖,或者存在奇怪的对象实例化顺序/混合

因此,问题是: 为什么我在测试状态列表时只得到false

更新:SSCCE的尝试


下面是相关类的实现

State.java

public class State {

    private String name;
    private Object value;

    State(String name, Object value){
        this.name=name;
        this.value=value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String toString(){   
        return getName() + ": " + getValue();   
    }

}
import java.util.ArrayList;

public class StatesList {

    private ArrayList<State> states;

    StatesList(){
        states= new ArrayList<State>();
    }

    void addState(String name, Object value){
        states.add(new State(name, value));
    }

    State getState(String name){
        for(State s : states){
            if(s.getName().equals(name)){
                return s;
            }
        }
        return null;
    }

    ArrayList<State> getStates() {
        return states;
    }

    public String toString(){
        String rString="";
        rString+="list\n";
        for (State s : states) {
            rString+=s.toString()+"\n";
        }
        return rString;
    }

}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBar {

    private JMenuBar menuBar;
    private StatesList menustates;

    public MenuBar() {
        menuBar = new JMenuBar();
        menustates = new StatesList();
    }

    void createMenu(String title) {
        JMenu menu = new JMenu(title);
        menuBar.add(menu);
    }

    void createMenuItem(JMenu menu, String itemTitle) {

        JMenuItem menuItem = new JMenuItem(itemTitle);

        menustates.addState(itemTitle, false);

        menuItem.addActionListener(new ActionListener() {
            int menuItemindex = menustates.getStates().size()-1 ;

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(actionEvent.getActionCommand());
                menustates.getStates().get(menuItemindex).setValue(true);
                //print list of current states
                System.out.println("setTrue");
                System.out.println(menustates.toString());
            }
        });
        menu.add(menuItem);
    }

    //not a real group, just ensures MenuItem behaves the same as other menu items
    void addMenuGroup(JMenuItem[] jMenuItems, int startComponentIndex) {    

        for (int i = 0; i < jMenuItems.length; i++) {
            jMenuItems[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (int i2 = 0; i2 < jMenuItems.length; i2++) {
                        //make sure each state related to the menuItemGroup is to false
                        menustates.getStates().get(i2 + startComponentIndex).setValue(false);
                    }
                    //print list of current states
                    System.out.println("setfalse");
                    System.out.println(menustates.toString());
                }
            });
        }
    }

    JMenuBar getMenuBar() {
        return menuBar;
    }

    public StatesList getStates() {
        return menustates;
    }


}
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBarBuilder {

    private MenuBar DataViewMenuBar;

    MenuBarBuilder() {
        DataViewMenuBar = buildDataViewMenu();
    }

    MenuBar buildDataViewMenu() {
        MenuBar tb = new MenuBar();

        //create menu
        tb.createMenu("TestScenes");

        //add two menuItems
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem1Name");
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem2Name");

        //add buttons two a 'group' sort of
        tb.addMenuGroup(new JMenuItem[] { 
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(0),
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(1), },
                tb.getStates().getStates().size() - 2);

        return tb;
    }

    StatesList getDataViewMenuBarStates() {
        return DataViewMenuBar.getStates();
    }

    JMenuBar getDataViewMenuBarMenubar() {
        return DataViewMenuBar.getMenuBar();
    }

}
import javax.swing.JFrame;

public class GUI {

    MenuBarBuilder mbb;
    boolean Selected = false;

    GUI() {
        mbb = new MenuBarBuilder();
    }

    void run() {
        final JFrame frame = new JFrame("Main");
        frame.setJMenuBar(mbb.buildDataViewMenu().getMenuBar());

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (!Selected) {
            selectLoadScene();
        }
        System.out.print("bap- selection must have occured");
    }

    void selectLoadScene() {
        String selectedTest = "";


    // The Test Occurs here
    // loop through states list
        System.out.println(mbb.getDataViewMenuBarStates().getStates().toString());
        for (State s : mbb.getDataViewMenuBarStates().getStates()) {
            // check the value for each state -this value is always false,..
            // thats the problem
            if ((boolean) s.getValue() == true) {
                // if true save value
                selectedTest = s.getName();
                break;
            }
        }

        // test selected value and see if it matches the following cases
        switch (selectedTest) {
        case "MenuItem1Name":
            // dosomething
            Selected = true; // breaks while loop
            break;
        case "MenuItem2Name":
            // dosomething
            Selected = true;// breaks while loop
            break;
        default:
            // do nothing
        }
    }
}
public class Start {

    GUI gui;

    public static void main(String[] args) {
        GUI gui= new GUI(); 
        gui.run();
    }

}
StatesList.java

public class State {

    private String name;
    private Object value;

    State(String name, Object value){
        this.name=name;
        this.value=value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String toString(){   
        return getName() + ": " + getValue();   
    }

}
import java.util.ArrayList;

public class StatesList {

    private ArrayList<State> states;

    StatesList(){
        states= new ArrayList<State>();
    }

    void addState(String name, Object value){
        states.add(new State(name, value));
    }

    State getState(String name){
        for(State s : states){
            if(s.getName().equals(name)){
                return s;
            }
        }
        return null;
    }

    ArrayList<State> getStates() {
        return states;
    }

    public String toString(){
        String rString="";
        rString+="list\n";
        for (State s : states) {
            rString+=s.toString()+"\n";
        }
        return rString;
    }

}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBar {

    private JMenuBar menuBar;
    private StatesList menustates;

    public MenuBar() {
        menuBar = new JMenuBar();
        menustates = new StatesList();
    }

    void createMenu(String title) {
        JMenu menu = new JMenu(title);
        menuBar.add(menu);
    }

    void createMenuItem(JMenu menu, String itemTitle) {

        JMenuItem menuItem = new JMenuItem(itemTitle);

        menustates.addState(itemTitle, false);

        menuItem.addActionListener(new ActionListener() {
            int menuItemindex = menustates.getStates().size()-1 ;

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(actionEvent.getActionCommand());
                menustates.getStates().get(menuItemindex).setValue(true);
                //print list of current states
                System.out.println("setTrue");
                System.out.println(menustates.toString());
            }
        });
        menu.add(menuItem);
    }

    //not a real group, just ensures MenuItem behaves the same as other menu items
    void addMenuGroup(JMenuItem[] jMenuItems, int startComponentIndex) {    

        for (int i = 0; i < jMenuItems.length; i++) {
            jMenuItems[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (int i2 = 0; i2 < jMenuItems.length; i2++) {
                        //make sure each state related to the menuItemGroup is to false
                        menustates.getStates().get(i2 + startComponentIndex).setValue(false);
                    }
                    //print list of current states
                    System.out.println("setfalse");
                    System.out.println(menustates.toString());
                }
            });
        }
    }

    JMenuBar getMenuBar() {
        return menuBar;
    }

    public StatesList getStates() {
        return menustates;
    }


}
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBarBuilder {

    private MenuBar DataViewMenuBar;

    MenuBarBuilder() {
        DataViewMenuBar = buildDataViewMenu();
    }

    MenuBar buildDataViewMenu() {
        MenuBar tb = new MenuBar();

        //create menu
        tb.createMenu("TestScenes");

        //add two menuItems
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem1Name");
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem2Name");

        //add buttons two a 'group' sort of
        tb.addMenuGroup(new JMenuItem[] { 
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(0),
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(1), },
                tb.getStates().getStates().size() - 2);

        return tb;
    }

    StatesList getDataViewMenuBarStates() {
        return DataViewMenuBar.getStates();
    }

    JMenuBar getDataViewMenuBarMenubar() {
        return DataViewMenuBar.getMenuBar();
    }

}
import javax.swing.JFrame;

public class GUI {

    MenuBarBuilder mbb;
    boolean Selected = false;

    GUI() {
        mbb = new MenuBarBuilder();
    }

    void run() {
        final JFrame frame = new JFrame("Main");
        frame.setJMenuBar(mbb.buildDataViewMenu().getMenuBar());

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (!Selected) {
            selectLoadScene();
        }
        System.out.print("bap- selection must have occured");
    }

    void selectLoadScene() {
        String selectedTest = "";


    // The Test Occurs here
    // loop through states list
        System.out.println(mbb.getDataViewMenuBarStates().getStates().toString());
        for (State s : mbb.getDataViewMenuBarStates().getStates()) {
            // check the value for each state -this value is always false,..
            // thats the problem
            if ((boolean) s.getValue() == true) {
                // if true save value
                selectedTest = s.getName();
                break;
            }
        }

        // test selected value and see if it matches the following cases
        switch (selectedTest) {
        case "MenuItem1Name":
            // dosomething
            Selected = true; // breaks while loop
            break;
        case "MenuItem2Name":
            // dosomething
            Selected = true;// breaks while loop
            break;
        default:
            // do nothing
        }
    }
}
public class Start {

    GUI gui;

    public static void main(String[] args) {
        GUI gui= new GUI(); 
        gui.run();
    }

}
GUI.java

public class State {

    private String name;
    private Object value;

    State(String name, Object value){
        this.name=name;
        this.value=value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String toString(){   
        return getName() + ": " + getValue();   
    }

}
import java.util.ArrayList;

public class StatesList {

    private ArrayList<State> states;

    StatesList(){
        states= new ArrayList<State>();
    }

    void addState(String name, Object value){
        states.add(new State(name, value));
    }

    State getState(String name){
        for(State s : states){
            if(s.getName().equals(name)){
                return s;
            }
        }
        return null;
    }

    ArrayList<State> getStates() {
        return states;
    }

    public String toString(){
        String rString="";
        rString+="list\n";
        for (State s : states) {
            rString+=s.toString()+"\n";
        }
        return rString;
    }

}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBar {

    private JMenuBar menuBar;
    private StatesList menustates;

    public MenuBar() {
        menuBar = new JMenuBar();
        menustates = new StatesList();
    }

    void createMenu(String title) {
        JMenu menu = new JMenu(title);
        menuBar.add(menu);
    }

    void createMenuItem(JMenu menu, String itemTitle) {

        JMenuItem menuItem = new JMenuItem(itemTitle);

        menustates.addState(itemTitle, false);

        menuItem.addActionListener(new ActionListener() {
            int menuItemindex = menustates.getStates().size()-1 ;

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(actionEvent.getActionCommand());
                menustates.getStates().get(menuItemindex).setValue(true);
                //print list of current states
                System.out.println("setTrue");
                System.out.println(menustates.toString());
            }
        });
        menu.add(menuItem);
    }

    //not a real group, just ensures MenuItem behaves the same as other menu items
    void addMenuGroup(JMenuItem[] jMenuItems, int startComponentIndex) {    

        for (int i = 0; i < jMenuItems.length; i++) {
            jMenuItems[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (int i2 = 0; i2 < jMenuItems.length; i2++) {
                        //make sure each state related to the menuItemGroup is to false
                        menustates.getStates().get(i2 + startComponentIndex).setValue(false);
                    }
                    //print list of current states
                    System.out.println("setfalse");
                    System.out.println(menustates.toString());
                }
            });
        }
    }

    JMenuBar getMenuBar() {
        return menuBar;
    }

    public StatesList getStates() {
        return menustates;
    }


}
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBarBuilder {

    private MenuBar DataViewMenuBar;

    MenuBarBuilder() {
        DataViewMenuBar = buildDataViewMenu();
    }

    MenuBar buildDataViewMenu() {
        MenuBar tb = new MenuBar();

        //create menu
        tb.createMenu("TestScenes");

        //add two menuItems
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem1Name");
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem2Name");

        //add buttons two a 'group' sort of
        tb.addMenuGroup(new JMenuItem[] { 
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(0),
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(1), },
                tb.getStates().getStates().size() - 2);

        return tb;
    }

    StatesList getDataViewMenuBarStates() {
        return DataViewMenuBar.getStates();
    }

    JMenuBar getDataViewMenuBarMenubar() {
        return DataViewMenuBar.getMenuBar();
    }

}
import javax.swing.JFrame;

public class GUI {

    MenuBarBuilder mbb;
    boolean Selected = false;

    GUI() {
        mbb = new MenuBarBuilder();
    }

    void run() {
        final JFrame frame = new JFrame("Main");
        frame.setJMenuBar(mbb.buildDataViewMenu().getMenuBar());

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (!Selected) {
            selectLoadScene();
        }
        System.out.print("bap- selection must have occured");
    }

    void selectLoadScene() {
        String selectedTest = "";


    // The Test Occurs here
    // loop through states list
        System.out.println(mbb.getDataViewMenuBarStates().getStates().toString());
        for (State s : mbb.getDataViewMenuBarStates().getStates()) {
            // check the value for each state -this value is always false,..
            // thats the problem
            if ((boolean) s.getValue() == true) {
                // if true save value
                selectedTest = s.getName();
                break;
            }
        }

        // test selected value and see if it matches the following cases
        switch (selectedTest) {
        case "MenuItem1Name":
            // dosomething
            Selected = true; // breaks while loop
            break;
        case "MenuItem2Name":
            // dosomething
            Selected = true;// breaks while loop
            break;
        default:
            // do nothing
        }
    }
}
public class Start {

    GUI gui;

    public static void main(String[] args) {
        GUI gui= new GUI(); 
        gui.run();
    }

}
Start.java

public class State {

    private String name;
    private Object value;

    State(String name, Object value){
        this.name=name;
        this.value=value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String toString(){   
        return getName() + ": " + getValue();   
    }

}
import java.util.ArrayList;

public class StatesList {

    private ArrayList<State> states;

    StatesList(){
        states= new ArrayList<State>();
    }

    void addState(String name, Object value){
        states.add(new State(name, value));
    }

    State getState(String name){
        for(State s : states){
            if(s.getName().equals(name)){
                return s;
            }
        }
        return null;
    }

    ArrayList<State> getStates() {
        return states;
    }

    public String toString(){
        String rString="";
        rString+="list\n";
        for (State s : states) {
            rString+=s.toString()+"\n";
        }
        return rString;
    }

}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBar {

    private JMenuBar menuBar;
    private StatesList menustates;

    public MenuBar() {
        menuBar = new JMenuBar();
        menustates = new StatesList();
    }

    void createMenu(String title) {
        JMenu menu = new JMenu(title);
        menuBar.add(menu);
    }

    void createMenuItem(JMenu menu, String itemTitle) {

        JMenuItem menuItem = new JMenuItem(itemTitle);

        menustates.addState(itemTitle, false);

        menuItem.addActionListener(new ActionListener() {
            int menuItemindex = menustates.getStates().size()-1 ;

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(actionEvent.getActionCommand());
                menustates.getStates().get(menuItemindex).setValue(true);
                //print list of current states
                System.out.println("setTrue");
                System.out.println(menustates.toString());
            }
        });
        menu.add(menuItem);
    }

    //not a real group, just ensures MenuItem behaves the same as other menu items
    void addMenuGroup(JMenuItem[] jMenuItems, int startComponentIndex) {    

        for (int i = 0; i < jMenuItems.length; i++) {
            jMenuItems[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (int i2 = 0; i2 < jMenuItems.length; i2++) {
                        //make sure each state related to the menuItemGroup is to false
                        menustates.getStates().get(i2 + startComponentIndex).setValue(false);
                    }
                    //print list of current states
                    System.out.println("setfalse");
                    System.out.println(menustates.toString());
                }
            });
        }
    }

    JMenuBar getMenuBar() {
        return menuBar;
    }

    public StatesList getStates() {
        return menustates;
    }


}
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuBarBuilder {

    private MenuBar DataViewMenuBar;

    MenuBarBuilder() {
        DataViewMenuBar = buildDataViewMenu();
    }

    MenuBar buildDataViewMenu() {
        MenuBar tb = new MenuBar();

        //create menu
        tb.createMenu("TestScenes");

        //add two menuItems
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem1Name");
        tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem2Name");

        //add buttons two a 'group' sort of
        tb.addMenuGroup(new JMenuItem[] { 
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(0),
                (JMenuItem) tb.getMenuBar().getMenu(0).getItem(1), },
                tb.getStates().getStates().size() - 2);

        return tb;
    }

    StatesList getDataViewMenuBarStates() {
        return DataViewMenuBar.getStates();
    }

    JMenuBar getDataViewMenuBarMenubar() {
        return DataViewMenuBar.getMenuBar();
    }

}
import javax.swing.JFrame;

public class GUI {

    MenuBarBuilder mbb;
    boolean Selected = false;

    GUI() {
        mbb = new MenuBarBuilder();
    }

    void run() {
        final JFrame frame = new JFrame("Main");
        frame.setJMenuBar(mbb.buildDataViewMenu().getMenuBar());

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (!Selected) {
            selectLoadScene();
        }
        System.out.print("bap- selection must have occured");
    }

    void selectLoadScene() {
        String selectedTest = "";


    // The Test Occurs here
    // loop through states list
        System.out.println(mbb.getDataViewMenuBarStates().getStates().toString());
        for (State s : mbb.getDataViewMenuBarStates().getStates()) {
            // check the value for each state -this value is always false,..
            // thats the problem
            if ((boolean) s.getValue() == true) {
                // if true save value
                selectedTest = s.getName();
                break;
            }
        }

        // test selected value and see if it matches the following cases
        switch (selectedTest) {
        case "MenuItem1Name":
            // dosomething
            Selected = true; // breaks while loop
            break;
        case "MenuItem2Name":
            // dosomething
            Selected = true;// breaks while loop
            break;
        default:
            // do nothing
        }
    }
}
public class Start {

    GUI gui;

    public static void main(String[] args) {
        GUI gui= new GUI(); 
        gui.run();
    }

}

while循环中的菜单栏与执行的操作方法中的菜单栏不同。为什么? 如果您仔细查看类GUI中的以下两行代码,您就会知道

mbb = new MenuBarBuilder();
frame.setJMenuBar(mbb.buildDataViewMenu().getMenuBar());
这里创建了两个不同的菜单栏。由于MenuBarBuilder的构造函数在内部调用buildDataViewMenu(),因此有2次调用“buildDataViewMenu()”。 最终将创建2个菜单栏

为了避免这种情况

不要在中调用“buildDataViewMenu()”

由于您只需要菜单栏,因此应该以这样一种方式管理代码,即您将获得this.tb=new menubar()的句柄。您可以为同一个菜单栏添加getter和setter

当我为bellow添加getter和setter时,它开始工作了

public class MenuBarBuilder {

private MenuBar DataViewMenuBar;
MenuBar tb;

MenuBarBuilder() {
    DataViewMenuBar = buildDataViewMenu();
}

MenuBar buildDataViewMenu() {
   this.tb = new MenuBar();

    //create menu
    tb.createMenu("TestScenes");

    //add two menuItems
    tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem1Name");
    tb.createMenuItem(tb.getMenuBar().getMenu(0), "MenuItem2Name");

    //add buttons two a 'group' sort of
    tb.addMenuGroup(new JMenuItem[] { 
            (JMenuItem) tb.getMenuBar().getMenu(0).getItem(0),
            (JMenuItem) tb.getMenuBar().getMenu(0).getItem(1), },
            tb.getStates().getStates().size() - 2);

    return tb;
}

public MenuBar getMenuBar(){
    return this.tb;
}

StatesList getDataViewMenuBarStates() {
    return DataViewMenuBar.getStates();
}

JMenuBar getDataViewMenuBarMenubar() {
    return DataViewMenuBar.getMenuBar();
}

}
类内GUI调用应该是这样的

frame.setJMenuBar(mbb.getMenuBar().getMenuBar());

这将解决问题。如果您面临任何问题,请告诉我。

您是否正在寻找javax.swing.ButtonGroup?您是否通过调试器运行了此功能?为了更快地获得更好的帮助,请发布or。我尝试了button group,但它不能很好地处理JMenuItems,至少上次检查时没有。调试器,。。是的,我早该想到的,我现在就要试一试了。为这篇长文章道歉,。。这是我的第一次,所以我真的不知道如何处理它,也许你有它向后。当您更改模型的状态时,菜单项应该相应地更新它们的状态,因此菜单项应该观察模型的更改我已经将构建方法设置为私有,这应该可以防止我将来犯这样一个愚蠢的错误,。。非常感谢。