0

我创建了一个包含多个 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";
    }
  }
}
4

1 回答 1

3

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

我没有看到任何button.addActionListener(...)声明。

阅读 Swing 教程中有关如何编写动作侦听器的部分以获取更多信息。

编辑:

您尝试显示的卡片似乎不存在。卡名有误。这就是为什么很多人使用静态最终常量,这样你就不会出现打字错误:

cardGUI.add(issueList(), "issueList");
...
cards.show(cardGUI, "IssueList");
于 2013-04-07T18:17:40.687 回答