我有两个类,一个 AnalogClock 类和一个 MainInterface 类。
我在 AnalogClock 类中创建了一个 timeChanged
方法,每当时间改变时都会调用它。我的 AnalogClock 基本上是一个带有绘图的 JPanel。在 MainInterface 中,我设置了一个 JFrame 并添加了我的 AnalogClock 对象。
是否可以在调用“timeChanged”时更改窗口的标题?我尝试使用 getParent()
或 getRootParent()
但它们无法识别 setTitle()
。
最佳答案
使用getWindowAncestor
来自 SwingUtilities
的方法。
//This gives you the first Window Object that contains the panel component
Window window = SwingUtilities.getWindowAncestor(panel);
//Cast it to JFrame
JFrame frame = (JFrame) window;
//Now, change the title
frame.setTitle("New Title");
关于java - 从其他类更改 JFrame 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957874/