简体   繁体   English

如何根据Swing中的条件调整面板?

[英]How to adjust panels depending on condition in Swing?

Here is my code to create new user. 这是我创建新用户的代码。 If I try to create new user without logging in it will show a message please log in first. 如果我尝试创建新用户而不登录,它将显示一条消息,请先登录。 Then I will log in and attempt to create new user. 然后,我将登录并尝试创建新用户。 My problem here is the previous text area displayed is still enabled. 我的问题是,先前显示的文本区域仍处于启用状态。 I'm not able to enter data into my text boxes. 我无法在文本框中输入数据。 I tried using remove() function but no use. 我尝试使用remove()函数,但没有用。

if(e.getSource()==createButton){
    //adminCard.remove(previlege);
    if(UID==0||users.size()==0){
        JPanel newUserCard = new JPanel(new FlowLayout());

        name = new JPanel( new GridLayout(1, 4));
        name.add(new JLabel("              "));
        userName=new JTextField(20);
        name.add(userName);
        name.add(new JLabel(" Name"));

        pword = new JPanel( new GridLayout(1, 4));
        pword.add(new JLabel("               "));
        pwd=new JTextField(20);
        pword.add(pwd);
        pword.add(new JLabel(" Password"));

        loginNamePanel = new JPanel( new GridLayout(1, 4));
        loginNamePanel.add(new JLabel("               "));
        loginName=new JTextField(20);
        loginNamePanel.add(loginName);
        loginNamePanel.add(new JLabel("UserName"));

        JPanel confirmpwd = new JPanel( new GridLayout(1, 3));
        confirmpwd.add(new JLabel("              "));
        confirmPassword=new JTextField(20);
        confirmpwd.add(confirmPassword);
        confirmpwd.add(new JLabel(" Confirm Password"));

        newUserCard.add(name);
        newUserCard.add( loginNamePanel);
        newUserCard.add(pword);
        newUserCard.add(confirmpwd);
        newUserCard.add(submit);
        displayUsers(users);
      // JSplitPane sw=new JSplitPane
        JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,newUserCard,userPane);
        //splitPane.setDividerLocation(0.8);
        splitPane.setOneTouchExpandable(true);
        //splitPane.repaint();
        userPane.setMaximumSize(new Dimension(10,20));
        //adminCard.re

        adminCard.add(splitPane);
        adminCard.validate();
        submit.addActionListener(this);
    } 

if(loginState==false && users.size()!=0){
    JTextArea previlege=new JTextArea("Please login in order to create new users");
                adminCard.add(previlege);
                adminCard.validate();

            }

Try calling repaint(); 尝试调用repaint(); as well, when you call validate(); 同样,当您调用validate(); Any time you do a remove(); 每当您执行remove(); or a removeAll(); removeAll(); , you should call both. ,您应该同时调用两者。

adminCard.validate();
adminCard.repaint();

A link to an earlier post on the issue 有关该问题的较早帖子的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM