【问题标题】:Trying to change title of JFrame from a Thread?试图从线程更改 JFrame 的标题?
【发布时间】:2014-02-17 21:59:09
【问题描述】:

当我尝试使用我在与 JFrame 相同的类中创建的函数从线程中设置 JFrame 的标题时:

public  void setTheTitle(String newTitle)
{
    this.setTitle(newTitle);
}

在我的线程中,我尝试通过以下方式使用该功能:

Main.setTitle(schar1 + schar2 + "-Resistance-" + schar3 + schar4);

(Main是我的类的名字,schar1、shcar2、schar3、schar4是前面Thread中定义的变量)

当我这样做时,它表示我无法从 Frame 类型中对非静态方法 setTitle(String) 进行静态引用。所以我把函数改成这样:

public static void setTheTitle(String newTitle)
{
    this.setTitle(newTitle);
}

现在这给了我一个错误,我不能在静态上下文中使用它。谁能告诉我如何解决这个问题?

【问题讨论】:

  • 格式化您的问题,否则没人会阅读它...
  • 文本块和未格式化的代码 - 这个问题看起来不太好。更重要的是永远不要在 EDT 之外更改或以其他方式与 Swing 组件交互。也许从Oracle tutorial on the topic开始。
  • 你不能在static函数中使用this...基本101编程知识!

标签: java multithreading static jframe


【解决方案1】:

这是我最好的选择 - 考虑到给出的代码量 - 尝试设置 JFrame 而不是扩展它

private JFrame jf = new JFrame("First Title");

并通过该方法访问标题

jf.setTitle(newTitle);

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2020-11-16
    • 2022-10-03
    • 2013-06-02
    • 1970-01-01
    相关资源
    最近更新 更多