如何删除现有的JPanel并持续实现动作监听器?

时间:2019-01-17 20:01:22

标签: java jbutton actionlistener

我正在尝试为我正在处理的个人项目创建JFrame / JPanel GUI。我想通过两个分别由beta和启动按钮切换的菜单来区分beta模式和启动模式。自实现第二个JPanel以来,我一直无法使动作监听器继续响应按钮(在呈现新菜单后),也无法使旧面板消失,我相当确定只是在写它。我在操作侦听器时遇到了麻烦,因为在我创建按钮时添加了动作侦听器,所以应该没有任何问题,并且在弄清楚处理重复JPanel的最佳方法时遇到了麻烦

下面是我的代码片段

我尝试使用remove(panel),添加新面板,新框架等。

当我将add调用移至BorderLayout.SOUTH时,我可以看到新面板具有我的原始模式菜单和2个JComboBoxes按需要呈现,但是我原始模式菜单仍然存在,并且当我单击时不再起作用。底部的新菜单使我单击一次并在停止响应之前打印我的对帐单TWICE。 如果我不单击测试版模式按钮,而只是单击启动按钮,则它将在底部每次打印两次,在顶部它一次打印一次,并让我尽可能地完成。

    protected JButton betaModeButton, launchModeButton;
JPanel modePanel;
public View() {
    setLayout(new BorderLayout());
    setup();
}
public void setup() {
    add(modeButtons(), BorderLayout.NORTH);
    System.out.println("setup");
}


/**
 * choose your mode for testing or implementation
 * @return
 */
protected JPanel modeButtons() {
    modePanel =  new JPanel(new GridLayout(1,2));

    betaModeButton = new JButton("BETA");
    betaModeButton.setOpaque(true);
    betaModeButton.addActionListener(this);

    launchModeButton = new JButton("LAUNCH");
    launchModeButton.setOpaque(true);
    launchModeButton.addActionListener(this);

    modePanel.add(betaModeButton);
    modePanel.add(launchModeButton);

    return modePanel;
}

private JPanel betaMenu() {

    System.out.println("Beta Menu should be added");
    JPanel comboMenu=new JPanel (new GridLayout(2,1));
    JPanel betaMenu=new JPanel(new GridLayout(1,2));

    //readd combo buttons
    comboMenu.add(modeButtons(), BorderLayout.NORTH);

    legShape= new JComboBox();
    legShape.addItem("Circle");
    legShape.addItem("Square");
    legShape.addItem("triangle");
    betaMenu.add(legShape);

    center= new JComboBox();
                 "code repeats structure

    betaModeButton.addActionListener(this);
    launchModeButton.addActionListener(this);

    comboMenu.add(betaMenu);
    return comboMenu;
}

我执行动作的电话是             add(betaMenu(),BorderLayout.SOUTH);             validate();

每次单击时,按钮应打印出来,并且顶部菜单应该消失

0 个答案:

没有答案