重绘主框架

问题描述

| 想象一下这种情况。我有一个主JFrame窗口,另一个是通过单击按钮从主窗口弹出的窗口。如果我想通过单击弹出窗口上的按钮在主窗口中重绘图形,该怎么办?     

解决方法

弹出的窗口需要参考主窗口。因此,在弹出窗口的构造函数中,您将接受对主窗口的引用,并保留它直到需要为止。 在主窗口中,您可以编写一个方法update(),该方法可以从弹出窗口中调用,并使主窗口相应地重新绘制自身。例如:
PoppedUpWindow win = new PoppedUpWindow(this);//this is the main window
并在弹出的窗口中:
MainWindow mainWin;
JButton btn;

public PoppedUpWindow(MainWindow mwin){
    mainWin = mwin;
    btn = new JButton(\"Click to Update\");
    btn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
              mainWin.update();
        }
    });
}