0

JPanelscardLayout. 当我切换到另一个JPanel时,它不会关闭前一个,并且在使用该程序后JPanels我会同时打开许多:JPanels

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{

  public  String name = "login";
    public boolean isDev = false;
    public boolean isMan = false;
  public  JButton loginSubmit, loginCancel,
        newIssue, newUser, list,
        detailsCancel, detailsSubmit, detailsAssign, detailsClose, detailsValidate, detailsFail,
        addUserCancel, addUserSubmit;
  public  JPanel cardGUI;
  public  JTextField loginTxt, pwTxt, userTxt, userPWTxt, issueDetailsTxt;
  public  JComboBox userCmb;
  public  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);
    loginTxt = new JTextField();
    inputPnl.add(loginTxt);
    JLabel pwLbl = new JLabel("Password");
    inputPnl.add(pwLbl);
    pwTxt = new JTextField();
    inputPnl.add(pwTxt);

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

    loginSubmit = new JButton("Submit");
      loginSubmit.addActionListener(this);
    buttonPnl.add(loginSubmit);
    loginCancel = new JButton("Cancel");
      loginCancel.addActionListener(this);
    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));

  newIssue = new JButton("New");
      newIssue.addActionListener(this);
  listFlow.add(newIssue);
  list = new JButton("Issues");
      list.addActionListener(this);
  listFlow.add(list);
  newUser = new JButton("Add User");
      newUser.setVisible(isMan);
      //newUser.setEnabled(false);
      newUser.addActionListener(this);
  listFlow.add(newUser);


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

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

  return listPnl;
 }

  public Component issueDetails(){
  JList list = new JList();
      issueDetailsTxt = new JTextField();
      issueDetailsTxt.addActionListener(this);

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

  detailsCancel = new JButton("Cancel");
      detailsCancel.addActionListener(this);
  detailsFlow.add(detailsCancel);

      detailsFlow.add(issueDetailsTxt);
  detailsSubmit = new JButton("Submit");
      detailsSubmit.addActionListener(this);
  detailsFlow.add(detailsSubmit);

  detailsAssign = new JButton("Assign");
      detailsAssign.setVisible(isMan);
      detailsAssign.addActionListener(this);
  detailsFlow.add(detailsAssign);
  detailsClose = new JButton("Close");
      detailsClose.setVisible(isMan);
      detailsClose.addActionListener(this);
  detailsFlow.add(detailsClose);
  detailsValidate = new JButton("Validate");
      detailsValidate.setVisible(isDev);
      detailsValidate.addActionListener(this);
  detailsFlow.add(detailsValidate);
  detailsFail = new JButton("Fail");
      detailsFail.addActionListener(this);
  detailsFlow.add(detailsFail);
      detailsFail.setVisible(isDev);

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

  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);
  userTxt = new JTextField();
  addUserGrid.add(userTxt);
  JLabel pwLbl = new JLabel("Password");
  addUserGrid.add(pwLbl);
  userPWTxt = new JTextField();
  addUserGrid.add(userPWTxt);

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

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

  addUserCancel = new JButton("Cancel");
      addUserCancel.addActionListener(this);
  addUserFlow.add(addUserCancel);
  addUserSubmit = new JButton("Submit");
      addUserSubmit.addActionListener(this);
  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){
        manager newUsr = new manager();

        if(newUsr.loginValidation(loginTxt.getText(), pwTxt.getText())){
            name = "issueList";
            isMan = newUsr.isManager();
            isDev = newUsr.isDeveloper();
            System.out.println("User Type: "+isMan);
            cardView();
        }else{
            loginTxt.setText("");
            pwTxt.setText("");
        }
    }else if(e.getSource() == loginCancel){
        loginTxt.setText(name);
        pwTxt.setText("");
        cardView();
        //cards.show(cardGUI, "login");
    }else if (e.getSource() == newIssue){//Goto New Issue Screen
        name = "issueDetails";
        cardView();
    }else if(e.getSource() == newUser){
        name = "addUser";
        cardView();//Goto Add User Screen
    }else if(e.getSource() == addUserCancel){//Go back to Issue List Screen
        name = "issueList";
        cardView();
    }else if(e.getSource() == addUserSubmit){//Add user
        manager newUsr = new manager();
        newUsr.createUser(userTxt.getText(), userPWTxt.getText(), userCmb.getSelectedItem());

        userTxt.setText("");
        userPWTxt.setText("");

    }else if(e.getSource() == list){//Goto Issue Details Screen
        name = "issueDetails";
        cardView();
    }else if(e.getSource() == detailsCancel){//Go Back to Issue List Screen
        name = "issueList";
        cardView();
    }else if(e.getSource() == detailsSubmit){

    }else if(e.getSource() == detailsAssign){

    }else if (e.getSource() == detailsClose){

    }else if (e.getSource() == detailsValidate){

    }else if (e.getSource() == detailsFail){

    }
  }
}

这是主文件:

  public class testSerializable{
    public static void main(String[] args){
       manager newUsr = new manager();
       newUsr.loadUserInfo();
       guiFrames gui = new guiFrames();
       gui.cardView();
     }  
  }
4

1 回答 1

1

由于您使用Container将 JPanel 添加到其中,因此您可以使用removeAll方法。

您正在将组件添加到您的containerContainer 中,因此我假设您需要从该 Container 中删除所有组件,然后添加新组件并刷新它。

伪代码:

container.removeAll();

container.add(NEW_COMPONENTS);

container.revalidate();
container.repaint();
于 2013-04-15T18:46:57.817 回答