如何使用 CardLayout,将 Menu 作为动作侦听器? [英] How to use CardLayout, with Menu as action listener?

查看:36
本文介绍了如何使用 CardLayout,将 Menu 作为动作侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 CardLayout 类更改 JFrame 的 JPanel.我已经运行了这个示例并且它有效.

现在我想使用 JMenuItem 作为动作侦听器;所以如果我按下那个 JMenuItem,我想用一个特定的面板来改变它.所以这是 JFrame:

public class FantaFrame extends JFrame 实现 Observer {private static final long serialVersionUID = 1L;私人 JPanel cardPanel = new JPanel();私有 CardLayout cardLayout = new CardLayout();公共 FantaFrame(HashMap fantaPanels) {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("FantaCalcio 应用程序");setSize(500, 500);cardPanel.setLayout(cardLayout);setPanels(fantaPanels);}公共无效更新(Observable o,对象arg){cardLayout.show(cardPanel, arg.toString());}private void setPanels(HashMap fantaPanels) {for (字符串名称:fantaPanels.keySet()) {cardPanel.add(fantaPanels.get(name), name);}}}

那些是菜单、控制器和主:

 private void press(){home.addActionListener(new ActionListener() {@覆盖public void actionPerformed(ActionEvent e) {controller.changePanel(home.getText());}});}公共类控制器扩展 Observable {公共无效更改面板(字符串面板){设置更改();通知观察者(面板);}}公共静态无效主(字符串 [] args){fantaPanels.put("登录", new LoginPanel());控制器控制器 = 新控制器();MenuBarApp menuApp = new MenuBarApp(controller);FantaFrame frame = new FantaFrame(fantaPanels);frame.setJMenuBar(menuApp);controller.addObserver(frame);frame.setVisible(true);}

问题是 JPanel 没有改变.你认为有什么问题?我已经对其进行了调试,并且在 update() 方法中,正确的 String 值到达了.

解决方案

您永远不会添加 cardPanel JPanel,即使用 CardLayout 并将卡片"显示到任何内容的那个.您需要将它添加到 JFrame 的 contentPane 中才能显示任何内容.即,

public FantaFrame(HashMap fantaPanels) {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("FantaCalcio 应用程序");setSize(500, 500);cardPanel.setLayout(cardLayout);添加(cardPanel,BorderLayout.CENTER);//****** 添加这一行 ******setPanels(fantaPanels);}

I want to change the JPanel of a JFrame, using the CardLayout class. I have already run this example and it works.

Now I want to use as action listener, the JMenuItem; so If I press that JMenuItem, I want to change it, with a specific panel. So this is the JFrame:

public class FantaFrame extends JFrame implements Observer {

    private static final long serialVersionUID = 1L;
    private JPanel cardPanel = new JPanel();
    private CardLayout cardLayout = new CardLayout();

    public FantaFrame(HashMap<String, JPanel> fantaPanels) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("FantaCalcio App");
        setSize(500, 500);
        cardPanel.setLayout(cardLayout);
        setPanels(fantaPanels);

    }

    public void update(Observable o, Object arg) {
        cardLayout.show(cardPanel, arg.toString());
    }

    private void setPanels(HashMap<String, JPanel> fantaPanels) {
        for (String name : fantaPanels.keySet()) {
            cardPanel.add(fantaPanels.get(name), name);
        }
    }
}

Those are the Menu, the Controller and the Main:

    private void pressed(){
        home.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controller.changePanel(home.getText());
            }
        });
    }
   public class Controller extends Observable {


    public void changePanel(String panel){
        setChanged();
        notifyObservers(panel);
    }
}
    public static void main(String[] args) {
        fantaPanels.put("Login", new LoginPanel());
        Controller controller = new Controller();
        MenuBarApp menuApp = new MenuBarApp(controller);
        FantaFrame frame = new FantaFrame(fantaPanels);
        frame.setJMenuBar(menuApp);
        controller.addObserver(frame);
        frame.setVisible(true);
    }

The problem is that the JPanel doesn't change. What do you think is the problem ? I've already debugged it, and in the update() method, the correct String value arrives.

解决方案

You never add the cardPanel JPanel, the one using the CardLayout and displaying the "cards" to anything. You need to add it to your JFrame's contentPane for it to display anything. i.e.,

public FantaFrame(HashMap<String, JPanel> fantaPanels) {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("FantaCalcio App");
    setSize(500, 500);
    cardPanel.setLayout(cardLayout);
    add(cardPanel, BorderLayout.CENTER); // ****** add this line ******
    setPanels(fantaPanels);
}

这篇关于如何使用 CardLayout,将 Menu 作为动作侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆