【问题标题】:Wait to return until button is pushed等待返回,直到按下按钮
【发布时间】:2015-07-09 15:45:31
【问题描述】:

我正在开发我的第一个大型多文件 Java 程序。当程序启动时,我的主文件调用另一个创建 GUI、填充它并接受信息的文件。然后它应该返回一个ArrayList,我的主文件应该使用它。

问题是我的 GUI 方法会立即返回一个空的 ArrayList,而不是等到按下按钮之后。

这就是我的ActionListeners 目前的工作方式

close.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    System.exit(0);
  }
});

start.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    temp = pullInfo(txtOrder, txtRounds);
  }
});

temp 是我想在最后返回的ArrayList

这是我在主文件中的内容:

 JFrame launch = new LaunchWindow(); 
 ArrayList<Integer> temp = ((LaunchWindow) launch).createGUI();
 rounds = temp.get(0);
 temp.remove(0);
 order = temp;
 connect();

我不希望它运行rounds = temp.get(0);,直到填充ArrayList。如何让它等到按下按钮?

【问题讨论】:

  • GUI 命令通常是异步的。
  • @Powerlord 太棒了。但这不是很有帮助。你是说没有办法轻松地做我想做的事吗?
  • 将代码放入开始按钮的action lsitener
  • 这看起来像一个类似的问题。最高回复是否回答了您的问题? stackoverflow.com/questions/6935918/…
  • 这些rounds = temp.get(0); temp.remove(0); order = temp; connect();

标签: java user-interface jframe jpanel


【解决方案1】:

我最终将@Madhan 的答案与我自己的一些东西混合在一起(我认为)

我在主文件中调用 GUI 文件:

JFrame launch = new LaunchWindow(); 
ArrayList<Integer> temp = ((LaunchWindow) launch).createGUI();

然后更改了我的 start.addActionListener 方法,只从那里调用下一个方法。

start.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    temp = pullInfo(txtOrder, txtRounds);
    int rounds = temp.get(0);
    temp.remove(0);
    ArrayList<Integer> order = temp;
    connect(order, rounds);
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多