我创建了一个小程序名称 ParentApplet.java,其任务是创建子框架
子框架编码在 ChildFrame.java 中定义
ParentApplet.java
public class ParentApplet extends Applet {
ChildFrame frame;
private static int time = 0;
@Override
public void start() {
frame.setVisible(true);
}
@Override
public void stop() {
frame.setVisible(false);
}
@Override
public void init() {
frame = new ChildFrame("Child");
this.setSize(400, 400);
}
@Override
public void paint(Graphics g) {
g.drawString("Child's Info : " + (++time), 50, 100);
g.drawString(frame.getMessage(), 400, 100);
System.out.println(frame.getMessage().isEmpty() ? "Empty" : frame.getMessage());
}
}
ChildFrame.java
公共类 ChildFrame 扩展框架 {
private String mess = "";
public ChildFrame(String title) {
super(title);
addMouseListener(new MyMouseAdapter(this));
addWindowListener(new MyWindowAdapter(this));
setSize(300, 500);
}
public String getMessage() {
return mess;
}
public void setMessage(String mess) {
this.mess = mess;
(new ParentApplet()).repaint();
System.out.println("Click");
}
}
MyMouseAdapter.java
公共类 MyMouseAdapter 扩展 MouseAdapter {
ChildFrame frame;
public MyMouseAdapter(ChildFrame frame) {
this.frame = frame;
}
@Override
public void mouseClicked(MouseEvent e) {
frame.setMessage("Mouse Cliked in Child");
}
}
MyWindowAdapter.java
公共类 MyWindowAdapter 扩展 WindowAdapter {
ChildFrame frame;
public MyWindowAdapter(ChildFrame frame) {
this.frame = frame;
}
@Override
public void windowClosing(WindowEvent we) {
frame.setVisible(false);
}
}
现在,即使在从 ChildFrame 类调用 repaint 方法后,我也无法再次访问 paint 方法。请建议我是否做错了什么或我需要理解的事情。
在此先感谢 Gagandeep Singh