简体   繁体   English

高级应用程序中的JButton如何工作?

[英]How do JButtons in advanced applications work?

How do buttons in software written in Java work? 用Java编写的软件中的按钮如何工作?

http://i.stack.imgur.com/fCbRQ.png

For example the above screenshot: when the user clicks different buttons, different algorithms are run on user-inputted data (it's a data analysis application) and the output is displayed. 例如上面的屏幕截图:当用户单击不同的按钮时,对用户输入的数据(这是一个数据分析应用程序)运行不同的算法,并显示输出。 Just getting started writing Java GUI's though, it all seems like magic to me -- is there one ActionListener for every pane? 不过,刚开始编写Java GUI的时候,对我来说,这似乎就像魔术一样-每个窗格都有一个ActionListener吗? Does it listen for different ActionCommands of the different buttons and execute the algorithm right within the actionPerformed() method (it seems a little nonintuitive to me to execute an algorithm in a method independent of data...ie the button doesn't know what data it's dealing with?). 它是否监听不同按钮的不同ActionCommand并在actionPerformed()方法内执行算法(对我来说,以独立于数据的方法执行算法似乎有点不直观...即按钮不知道什么它正在处理的数据?)。 So far, all the action listener tutorials I've read online have merely printed something when the button is pressed... 到目前为止,我在网上阅读的所有动作侦听器教程都仅在按下按钮时才打印出一些内容...

What's the general structure for connecting button, actionlisteners, and actual actions performed in the background? 连接按钮,动作监听器和在后台执行的实际动作的一般结构是什么?

Thanks in advance. 提前致谢。

The usual way is to have one action listener per button. 通常的方法是每个按钮有一个动作监听器。 The Statistics panel has access (via one of its fields), to the data it needs to read and modify). Statistics面板可以访问(通过其字段之一)需要读取和修改的数据。 So, the handling of the first button in this panel could look like: 因此,此面板中第一个按钮的处理如下所示:

private void initButtonListeners() {
    this.averageDegreeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            computeAverageDegree();
        }
    }

    // other buttons...
}

And the computeAverageDegree() method could look like 而且computeAverageDegree()方法看起来像

private void computeAverageDegree() {
    double result = this.statistics.computeAverageDegree();
    this.averageDegreeLabel.setText(formatDoubleToString(result));
}

My personal preference is to do almost nothing in the UI, but move it all to the model/controller side (not sure what the best name is as it is seldom pure MVC). 我个人的喜好是在UI中几乎不执行任何操作,而是将所有操作移至模型/控制器端(不确定最好的名字是什么,因为它很少是纯MVC)。

I think that everything you do in the UI should be doable through the API as well. 我认为您在用户界面中所做的所有事情也应该可以通过API来完成。 Benefits are easier testing, redesign of the UI is possible without messing up your logic, easy to perform the heavy work on background threads, ... . 好处是易于测试,可以重新设计UI,而不会弄乱您的逻辑,易于在后台线程上执行繁重的工作,...。

A good read describing this is the Humble Dialog article. 拙劣的对话框文章很好地描述了这一点。 Not really Swing specific, but applicable to all sort of UI's. 并非特定于Swing,但适用于所有类型的UI。

To answer your questions: 要回答您的问题:

is there one ActionListener for every pane? 每个窗格都有一个ActionListener吗?

No, typically you have an Action (or ActionListener ) for each button. 不,通常每个按钮都有一个Action (或ActionListener )。 I prefer to use Action instances as they are far more reusable then the typical anonymous ActionListener (and easier to test as well) 我更喜欢使用Action实例,因为它们比典型的匿名ActionListener更可重用(并且也更易于测试)

Does it listen for different ActionCommands of the different buttons and execute the algorithm right within the actionPerformed() method 它是否监听不同按钮的不同ActionCommand并在actionPerformed()方法内执行算法

Certainly not. 当然不是。 Doing heavy calculations in that method will block the Swing UI thread (the Event Dispatch Thread), which results in a non-responsive UI while the calculations are ongoing. 在该方法中进行大量计算将阻塞Swing UI线程(事件调度线程),这会导致在进行计算时UI不响应。 Showing progress becomes also impossible. 显示进度也变得不可能。 Calculations are typically done on a worker thread, launched when your Action is triggered (for example using a SwingWorker ). 计算通常是在工作线程上完成的,该工作线程是在触发您的Action启动的(例如,使用SwingWorker )。 This is explained in the Concurrency in Swing tutorial. Swing中的并发教程对此进行了说明。

it seems a little nonintuitive to me to execute an algorithm in a method independent of data...ie the button doesn't know what data it's dealing with? 在我看来,以独立于数据的方法执行算法有点不直观……即按钮不知道它正在处理什么数据?

The button should not know about the data. 该按钮不应该知道数据。 The data is typically stored in the model. 数据通常存储在模型中。 The UI is only displaying it, but does not contain it (unless it is input just provided by the user). UI仅显示它,但不包含它(除非它只是由用户提供的输入)。 The button should just know what to call on the model. 该按钮应该只知道在模型上调用什么。 The model does whatever it has to do and fires an event. 该模型将执行其所有操作并触发一个事件。 The UI picks up that event and updates itself. UI拾取该事件并进行自我更新。

At least, that is how Swing is designed (for example JTable and its TableModel ). 至少,这就是Swing的设计方式(例如JTable及其TableModel )。 I so no good reason to not follow that model when making your own Swing UI's 我没有充分的理由在制作自己的Swing UI时不遵循该模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM