【问题标题】:I can't get my CardLayout to switch cards using a button我无法让 CardLayout 使用按钮切换卡片
【发布时间】:2014-11-05 16:12:24
【问题描述】:

我试图让 CardLayout 使用按钮更改面板的内容(用于不同类中的内容),但由于某种原因,按钮(添加新项目)在单击时没有任何作用。

“添加新项目”按钮的事件侦听器正在尝试访问不同的类(NewProject 类)

    ...
//CardLayout variables
private JPanel cardPanel;
private static final String CARD_PROJECTTAB = "Card Project Tab";
private static final String CARD_NEWPROJECT = "Card Add Project";
private NewProject newProject;
//private TabPanel tp = new TabPanel();

//

public ProTab(){

//Create cardPanel the panel that holds all the cards.
cardPanel = new JPanel();
cardPanel.setLayout(new CardLayout(0,3));       

//Creating the proPanel that holds all other panels (below cardPanel)
proPanel = new JPanel();
proPanel.setBackground(new Color(204, 255, 102));


/*
  ==========================
  CardLayout Class Instances
  ==========================
*/

cardPanel.add(proPanel, CARD_PROJECTTAB);
newProject = new NewProject();
cardPanel.add(newProject, CARD_NEWPROJECT);

//Creating the contentPane that holds all GUI components and
//uses vertical/horizontal sidebars as needed
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);

//Giving the contentPane the GridBagLayout
contentPane.setLayout(new GridBagLayout());
GridBagConstraints g = new GridBagConstraints();
g.insets = new Insets(20,0,0,0);

...

//Project bottom buttons START

 bButtonsPanel = new JPanel();
 bButtonsPanel.setBackground(Color.WHITE);
 bButtonsPanel.setBorder(BorderFactory.createMatteBorder(0,0,0,0, Color.gray));
 bButtonsPanel.setLayout(new FlowLayout());
    g.anchor = GridBagConstraints.PAGE_END;
    g.gridx = 0;
    g.gridy = 2;
    contentPane.add(bButtonsPanel, g);

viewProjectButton = new JButton("View Selected Project");
addProjectButton = new JButton ("Add a New Project");

bButtonsPanel.add(viewProjectButton);
bButtonsPanel.add(addProjectButton);

addProjectButton.addActionListener(new buttonListener());

//Project bottom buttons END

}

//Project bottom buttons ActionListeners
private class buttonListener implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        if (ae.getSource() == addProjectButton){
        CardLayout cl = (CardLayout) cardPanel.getLayout();
        cl.show(cardPanel, CARD_NEWPROJECT);
        }
    }
 }

【问题讨论】:

    标签: java user-interface cardlayout


    【解决方案1】:

    我想通了。我的错误不在于 CardLayout 的实现,而是我未能从另一个 c 正确调用包含 CardLayout 的 JPanel

    Before //getProTab() 是一个返回带有 Project Tab 元素的 JPanel 的方法。

        pt = new ProTab();
        tabPane.addTab("Projects", pt.getProTab());
        tabPane.setBackgroundAt(2, (new Color(204, 255, 102)));
    

    在 //getCardPanel() 之后是我创建的一个新方法,用于返回包含所有卡片的 JPanel(包括 // proPanel 面板)

        pt = new ProTab();
        tabPane.addTab("Projects", pt.getCardPanel());
        tabPane.setBackgroundAt(2, (new Color(204, 255, 102)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2014-09-18
      • 2016-04-21
      • 2016-09-03
      • 1970-01-01
      相关资源
      最近更新 更多