【问题标题】:Forcing a JPanel to repaint after calling the show method of CardLayout调用 CardLayout 的 show 方法后强制 JPanel 重新绘制
【发布时间】:2013-03-18 12:56:58
【问题描述】:

我正在为一门课程编写一个假期推荐系统。其中的 GUI 使用 CardLayout。在主类中创建一个用户对象,它的构造函数中定义了默认名称和访问级别。此对象从 main 传递到 UserCard 面板,后者将其传递给 Login 并登录。 如果用户成功登录,则卡片面板从登录转换为登录,并且应该通过调用 user.getUsername() 来显示登录用户的用户名;方法。

我的问题是这样的。由于卡片布局的工作方式,已经在 UserCards 的构造函数中创建了带有用户名 display 的面板,并使用默认值从那时起首先创建了用户对象。在卡片布局对象上调用 show 方法后,我需要找到一种方法来强制此面板重新绘制。以下是有问题的 3 个类的代码。 (我已将代码粘贴限制为相关方法)。

//the usercards panel

        public UserCards(User u)
    {
        CardLayout cl = new CardLayout();
        this.setLayout(cl);

        UserOptionsPanel options_card = new UserOptionsPanel(cl, this);
        RegisterPanel register_card = new RegisterPanel(cl, this);
        LoggedInPanel loggedin_card = new LoggedInPanel(cl, this, u);
        LoginPanel login_card = new LoginPanel(cl, this, u, loggedin_card);

        this.add(options_card, options);
        this.add(login_card, login);
        this.add(register_card, register);
        this.add(loggedin_card, loggedin);
    }

//the Loggin action listener user is passed in as a reference to the user object created in //main. the createUser(); method is a badly named method that simply calls setter methods on //the user object's fields

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        String[] vals = packData();
        try
        {
            DBConnection d = new DBConnection();
            Connection conn = d.getConnection();
            Validation v = new Validation(vals, user);
            v.getActual(conn);
            if(v.validate())
            {
                user = v.createUser();
                System.out.println(user.getUserName());
                l.revalidate();
                cl.show(pane, "loggedin");
            }
            else
            {
                lbl_statusmsg.setText("Password Incorrect");
                lbl_statusmsg.repaint();
            }

        }
        catch(ClassNotFoundException | SQLException ex)
        {
            ex.printStackTrace();
        }
    }

//the loggedin constructor

public class LoggedInPanel extends JPanel
{
    private User user;
    private JLabel lbl_details;
    public LoggedInPanel(CardLayout cl, Container pane, User u)
    {
        super();
        user = u;
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        lbl_details = new JLabel();
        lbl_details.setText("Welcome "+user.getUserName());

        this.add(lbl_details);
    }    
}

抱歉,如果我没有说得太清楚,我不会寻求帮助:)

【问题讨论】:

  • 为了更好的帮助,请尽快发布SSCCE,简短,可运行,可编译,

标签: java swing repaint cardlayout


【解决方案1】:

你的意思是类似的吗?

CardLayout cl = new CardLayout() {
  @Override
  public void show(java.awt.Container parent, String name) {
    super.show(parent, name);
    // your code here

  }
};

【讨论】:

  • 登录需要数据库访问权限,所以恐怕如果不进行相当广泛的重写,这实际上是不可能的(主要是因为我的大学认为适合在我和他们的 MySQL 安装之间增加 4 层安全性)
  • @user2182380 是的,你可以尝试在那里添加重绘。
猜你喜欢
  • 2013-11-12
  • 2011-05-22
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2011-10-04
相关资源
最近更新 更多