【问题标题】:Access Gui in ActionListener Class to update it every x seconds with Timer在 ActionListener 类中访问 Gui 以使用 Timer 每 x 秒更新一次
【发布时间】:2014-02-04 12:20:57
【问题描述】:

我想每 30 秒更新一次 Swing Gui,其中一些标签将从数据库连接中获取新值。 到目前为止,我每次都尝试创建一个新的 Gui 并处理旧的 Gui,但这不是一个优雅的解决方案,无论如何也不起作用。

 public class Gui extends JFrame {

private boolean open;
//setter, getter
public Gui() {
    JPanel pane = new JPanel(new BorderLayout());
    int numberOfRows = 9;
    int numberOfColumns = 2;
    pane.setLayout(new GridLayout(numberOfRows, numberOfColumns));

    String wState;
    if(open){wState="Window open";}
    else{wState="Window closed";}
    JLabel windowState= new JLabel(wState);
    pane.add(windowState);
    new Timer(5000, new WindowState()).start(); }

private class WindowState implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Gui g = new Gui();
      g.setOpen(true);
    }

我知道它不会像这样工作,但我希望我想做什么变得很清楚。问题是我无法访问 actionPerformed() 方法中的 Gui 元素。我只是想用在 actionPerformed() 方法中检索到的新值更新 windowState 标签。

【问题讨论】:

  • 引用了几种方法here

标签: java swing user-interface


【解决方案1】:

“问题是我无法访问 actionPerformed() 方法中的 Gui 元素。”

您需要了解变量范围。目前,您的所有变量都在构造函数中本地作用域

public class GUI {
    public GUI {
        JPanel panel = new JPanel();  <--- Locally scoped
    }
}

你需要给你想要访问的对象一个全局范围

public class GUI {
    JPanel panel= new JPanel();     <--- global scope

    public GUI(){
    }
}

然后您可以在actionPerformed 中访问panel


看这里

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GUI extends JFrame {

    private boolean open;
    JPanel pane;
    JLabel windowState;
    int count = 1;

    public GUI() {

        int numberOfRows = 9;
        int numberOfColumns = 2;
        pane = new JPanel(new BorderLayout());
        pane.setLayout(new GridLayout(numberOfRows, numberOfColumns));

        String wState;
        if (open) {
            wState = "Window open";
        } else {
            wState = "Window closed";
        }

        windowState = new JLabel(wState);
        pane.add(windowState);

        add(pane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        new Timer(500, new WindowState()).start();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUI();
            }
        });
    }

    private class WindowState implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            count++;
            windowState.setText("Number " + count + "!");
        }
    }
}

【讨论】:

  • 它需要被标记为final,但是,才能从 ActionListener 内部类中访问。
  • @Gorb 谁说的?。您应该在公开发布之前测试您的理论。如果在构造函数中声明并且正在从构造函数中的内部 annonymousalso 访问,则它们只需要是最终的。这里也不是这样。
  • 我坐得更正了!我错过了你在同一行实例化它。不过,态度哟:/
  • @Gorb “还是,态度哟:/” 没关系。人们对你刚刚表现出的“我是珍贵的雪花”的任性很宽容。冷静.. ;)
  • 我很困惑,我没有表现出任何态度。我的意思是真诚的更正。对不起,伙计们,这个答案偏离了轨道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多