为什么没有清除jframe?

时间:2016-12-21 15:59:52

标签: java swing layout-manager

这是一个奇怪的情况。我删除了一个框架的内容并添加了新的内容 但内容正在被添加两次。为什么会发生这种情况? 这可能看起来像很长的代码,但大部分都是为帧生成内容 正如我从 addUserFrame()的 ActionListener 中调用 manager() 函数时所看到的那个问题开始/ em> 功能。

public class adminManager {
    private static Connection con;
    private static Statement stmt;
    private static JFrame f;
    private static JPanel p = new JPanel(); 
    private static JButton addUser,exitTest,addTest,editTest;   

    public static void manager(JFrame frame)
   {
    f=frame;f.setSize(400,800);
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    Dimension d = new Dimension(100,30);
    addUser = new JButton("ADD USER");
    exitTest = new JButton("EXIT");
    addTest =  new JButton("ADD TEST");
    editTest = new JButton("EDIT TEST");
    p.add(addUser);p.add(addTest);
    p.add(editTest);p.add(exitTest);
    f.setTitle("Select Option");
    f.setLayout(new FlowLayout());
    f.add(p);   
    f.setVisible(true);     
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    bindEvents();
  }

    public static void addUserFrame(JFrame f)
   {        
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();J
    Panel parentPanel = new JPanel();
    JLabel l1 = new JLabel("Enter User-Name");
    JLabel l2 = new JLabel("Enter Password");
    JTextField t1 = new JTextField(10);
    JTextField t2 = new JTextField(10);
    JButton btn = new JButton("Submit");
    JButton back = new JButton("Go Back");
    p1.setLayout(new FlowLayout(FlowLayout.CENTER));
    p2.setLayout(new FlowLayout(FlowLayout.CENTER));
    p3.setLayout(new FlowLayout(FlowLayout.CENTER));
    parentPanel.setLayout(new BoxLayout(parentPanel,BoxLayout.Y_AXIS));
    p1.setSize(200,100);
    p2.setSize(200,100);
    p3.setSize(200,100);
    p1.add(l1);p1.add(t1);
    p2.add(l2);p2.add(t2);
    p3.add(btn);p3.add(back);
    parentPanel.add(p1);parentPanel.add(p2);parentPanel.add(p3);
    f.add(parentPanel);
    back.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){             
            f.getContentPane().removeAll();f.repaint();
            manager(f);             
        }
    });

}

public static void bindEvents(){
    addUser.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            f.getContentPane().removeAll();
            f.repaint();    
            addUserFrame(f);
            f.setSize(400, 150);
            f.setTitle("Add User");
            f.setVisible(true);             
        }
    });
    }

public static void main(String []args){
    manager(new JFrame());
    }
}

enter image description here

enter image description here

enter image description here

更新

通过将p=new JPanel()移至manager()方法

来解决问题

2 个答案:

答案 0 :(得分:4)

<%@ page language="java" session="true" contentType="text/html;charset=UTF-8" %>

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-bean" prefix="logic" %>
<%@ taglib uri="/tags/jstl-c" prefix="c" %>
<%@ taglib uri="/tags/widgets" prefix="widget" %>
<%@ taglib uri="/tags/jstl-fmt" prefix="fmt" %>

<fmt:setBundle basename="resources/application" var="kanisa" />

<html>
<head>
    <title><bean:message key='label.common.title'/></title>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE10">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-store">
    <META HTTP-EQUIV="Expires" CONTENT="Now">
    <META HTTP-EQUIV="Expires" CONTENT="-1">
    <link href="../css/knova.css" type="text/css" rel="stylesheet">
</head>
<body style="width: 380px; margin: 0px;">   
    <widget:search action="microsite.do" template="GS.vm"
                   showBrowseArea="none" showSearchWithin="false" showNavigationArea="false" showSynopsis="Title" hideDocType="true" 
                   widgetName="dd_home" widgetTitle="searchwidget.hotTopics"
                   UMFilterUMType="DT_DocType"  showUMFilter="true"  searchDocuments="KSM,Authored,Threaded"
                   sortDDL="newest,popular" sortCriteria="newest" numChar="100"
                   numDocsPerPage="5" useMSPreferences="true" />
</body>
</html>

您从框架中删除了面板f.getContentPane().removeAll(); ,但在方法p中添加了添加新元素的manager()。您必须在p上致电clear()或创建新的小组p

答案 1 :(得分:0)

问题是正在使用相同的JPanel 通过将p=new JPanel()移动到manager()方法

来解决此问题