【问题标题】:How to return a JFrame inside a static method using a 'get' method如何使用“get”方法在静态方法中返回 JFrame
【发布时间】:2015-05-07 16:46:07
【问题描述】:

我正在尝试将JFrame 返回到另一种方法中,以便我可以从中添加/删除内容,但是我不想每次我想重新绘制时都创建一个新的JFrame。我知道我需要在另一个类中重新绘制它,但是我不明白将它“获取”到类中,代码:

public class TileFrame{
    public TileFrame {
    //all the usual jframe creation stuff here (not the issue)
    JButton[][] tiles = new JButton[4][4];
        for (int i=0; i<4; i++) {
          for (int j=0; j<4; j++){
            tiles[i][j] = new JButton();
          }   
        }
    }
}
public class doSomethingWithTheTileFrame{ 
   public doSomethingWithTheTileFrame{
        retrievedTileFrame = getTileFrame();
        retrievedtileframe .....do stuff to this (this is already sorted)
   }

}
public static void main (String[] args) {
    TileFrame tileframe = new TileFrame();
    doSomethingWithTheTileFrame();

}

我要问的是如何检索这个tileframeJFrame)而不是每次都在doSomethingWithTheTileFrame 中创建一个新的?基本上我在问如何将 TileFrame 从 main 方法传递回 doSomethingWithTileFrame

非常感谢。

【问题讨论】:

  • What I am asking is how to I retrieve this tileframe (a JFrame)//all the usual jframe creation stuff here (not the issue) --> 这个 JFrame 是不是声明为局部变量(那么 getter/setter e.i. 的生活会更容易),为了更好地帮助尽快发布 SSCCE/MCVE,简短, 可运行, 可编译

标签: java swing class jframe


【解决方案1】:

我假设您的 TileFrame 类扩展了 JFrame。如果是这样,为什么不能像这样传递参考?

public static void main (String[] args) {
    TileFrame tileframe = new TileFrame();
    doSomethingWithTheTileFrame(tileframe );    
}

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 2021-02-17
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2013-06-09
    • 2012-03-24
    相关资源
    最近更新 更多