JPanels不会切换到cardLayout

时间:2013-04-07 18:10:54

标签: java swing jpanel actionlistener cardlayout

我创建了一个包含多个JPanel的类,我想使用cardLayout在这些JPanel之间切换。每个Jpanel和cardLayout都是他们自己的方法。当我点击任何按钮时,它应显示下一个Jpanel但不显示。我不能在这里得到什么

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

public class guiFrames extends JFrame implements ActionListener{

    String name = "login";
    JButton loginSubmit, list;
    JPanel cardGUI;
    CardLayout cards;

  public guiFrames(){
  }

  public Component inputFrame(){

    JPanel inputPnl = new JPanel();
    inputPnl.setLayout(new GridLayout(3,2));

    JLabel loginLbl = new JLabel("Login");
    inputPnl.add(loginLbl);
    JTextField loginTxt = new JTextField();
    inputPnl.add(loginTxt);
    JLabel pwLbl = new JLabel("Password");
    inputPnl.add(pwLbl);
    JTextField pwTxt = new JTextField();
    inputPnl.add(pwTxt);

    JPanel buttonPnl = new JPanel();
    buttonPnl.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

    loginSubmit = new JButton("Submit");
    buttonPnl.add(loginSubmit);
    JButton loginCancel = new JButton("Cancel");
    buttonPnl.add(loginCancel);

    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());
    container.add(inputPnl, BorderLayout.CENTER);
      container.add(buttonPnl, BorderLayout.SOUTH);

      container.setVisible(true);

    return container;
  }

  public Component issueList(){

  JPanel listPnl = new JPanel();

  JPanel listFlow = new JPanel();
  listFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

  JButton newIssue = new JButton("New");
  listFlow.add(newIssue);
  list = new JButton("Issues");
  listFlow.add(list);
  JButton newUser = new JButton("Add User");
  listFlow.add(newUser);


  JPanel container = new JPanel();
  container.setLayout(new BorderLayout());
  container.add(listFlow, BorderLayout.SOUTH);

  listPnl.add(container);
//  listPnl.pack();
  listPnl.setVisible(true);
//  listPnl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  return listPnl;
 }

  public Component issueDetails(){
  JList list = new JList();

  JPanel detailsFlow = new JPanel();
  detailsFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

  JButton detailsCancel = new JButton("Cancel");
  detailsFlow.add(detailsCancel);
  JButton detailsSubmit = new JButton("Submit");
  detailsFlow.add(detailsSubmit);
  JButton detailsAssign = new JButton("Assign");
  detailsFlow.add(detailsAssign);
  JButton detailsClose = new JButton("Close");
  detailsFlow.add(detailsClose);
  JButton detailsValidate = new JButton("Validate");
  detailsFlow.add(detailsValidate);
  JButton detailsFail = new JButton("Fail");
  detailsFlow.add(detailsFail);
      detailsFail.setVisible(false);

  JPanel container = new JPanel();
  container.setLayout(new BorderLayout());
  container.add(detailsFlow, BorderLayout.SOUTH);
  container.add(list, BorderLayout.CENTER);

  container.setVisible(true);

  return container;

  } 

  public Component addUser(){

  JPanel addUserGrid = new JPanel();
  addUserGrid.setLayout(new GridLayout(3,2));


  JLabel loginLbl = new JLabel("Login");
  addUserGrid.add(loginLbl);
  JTextField loginTxt = new JTextField();
  addUserGrid.add(loginTxt);
  JLabel pwLbl = new JLabel("Password");
  addUserGrid.add(pwLbl);
  JTextField pwTxt = new JTextField();
  addUserGrid.add(pwTxt);

  JLabel userLbl = new JLabel("User Type");
  addUserGrid.add(userLbl);
  JComboBox userCmb = new JComboBox();
  userCmb.addItem("User");
  userCmb.addItem("Manager");
  userCmb.addItem("Developer");
  addUserGrid.add(userCmb);

  JPanel addUserFlow = new JPanel();
  addUserFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

  JButton addUserCancel = new JButton("Cancel");
  addUserFlow.add(addUserCancel);
  JButton addUserSubmit = new JButton("Submit");
  addUserFlow.add(addUserSubmit);

  JPanel container = new JPanel();
  container.setLayout(new BorderLayout());
  container.add(addUserFlow, BorderLayout.SOUTH);
  container.add(addUserGrid, BorderLayout.CENTER);

  container.setVisible(true);

  return container;
  }

  public void cardView(){
        JFrame cardFrame = new JFrame(name);
        cardGUI = new JPanel();
        cards = new CardLayout();
        cardGUI.setLayout(cards);
        cardGUI.add(inputFrame(), "login");
    cardGUI.add(issueList(), "issueList");
    cardGUI.add(issueDetails(), "issueDetails");
    cardGUI.add(addUser(), "addUser");

        cardFrame.add(cardGUI, BorderLayout.CENTER);
        cardFrame.pack();
        cardFrame.setVisible(true);
        cardFrame.setDefaultCloseOperation(cardFrame.EXIT_ON_CLOSE);

     cards.show(cardGUI, name);
  }

  public void actionPerformed(ActionEvent e){
    if(e.getSource() == loginSubmit){
      cards.show(cardGUI, "IssueList");
    }else if(e.getSource() == list){
      name = "issueDetails";
    }
  }
}

1 个答案:

答案 0 :(得分:3)

  

当我点击任何按钮时,它应显示下一个Jpanel但不显示。

我没有看到任何button.addActionListener(...)语句。

阅读How to Write an Action Listener上的Swing教程中的部分以获取更多信息。

编辑:

看起来你想要展示的卡片不存在。卡名称错误。这就是为什么许多人使用静态最终常量,因此您不会输入错误:

cardGUI.add(issueList(), "issueList");
...
cards.show(cardGUI, "IssueList");