我应该在Actions中创建Swing组件吗?(Should I create Swing components within Actions?)
我正在为我的Swing组件创建Actions,但是,他们经常需要生成一个确认JDialog。 我应该在Action本身内创建这个JDialog,我应该将它传递给Action,还是有替代方法?
例
public static class Create extends AbstractAction { @Override public void actionPerformed(ActionEvent event) { int selection = JOptionPane.showConfirmDialog(...); // ... processing } }
这个例子很难进行单元测试,因为它涉及自动选择JDialog(感觉就像代码味)。
解?
public static class Create extends AbstractAction { private JDialog dialog = null; public Create(JDialog dialog) { this.dialog = dialog; } @Override public void actionPerformed(ActionEvent event) { dialog.display(); // ... processing } }
I am creating Actions for my Swing components, however, they often need to generate a confirmation JDialog. Should I be creating this JDialog within the Action itself, should I pass it in to the Action, or is there an alternative method?
Example
public static class Create extends AbstractAction { @Override public void actionPerformed(ActionEvent event) { int selection = JOptionPane.showConfirmDialog(...); // ... processing } }
This example is quite hard to unit test since it involves automating the selection of the JDialog (which feels like a code smell).
Solution?
public static class Create extends AbstractAction { private JDialog dialog = null; public Create(JDialog dialog) { this.dialog = dialog; } @Override public void actionPerformed(ActionEvent event) { dialog.display(); // ... processing } }
原文:https://stackoverflow.com/questions/14760130
最满意答案
文本/扫描程序将过多的开销(在程序员时间)用于这种简单的格式。
net / http使用net / textproto ,你也可以使用它。 查找MIMEHeader和ReadMIMEHeader 。
如果您需要比MIMEHeader可以包含的更复杂的东西,我会考虑使用JSON。
text/scanner would be way too much overhead (in programmer time) to use for a format this simple.
net/http uses net/textproto, you might be able to use that too. Look for MIMEHeader and ReadMIMEHeader.
If you need something more complicated than what MIMEHeader can contain, I'd consider just using JSON.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
正如埃蒙所说,如果你知道<?xml>的东西永远在那里,那么就打破它。 否则,请查找结尾的文档级标签。 也就是说,扫描文本,统计你有多深。 每当您看到以“<”开头但不是“”且以“/>”结尾的标签时,请将深度数加1。 每当您看到以“”开头的标签时,减去1.每次减去1时,检查您现在是否为零。 如果是这样,你已经到达了XML文档的末尾。 As Eamon says, if you know the thing will always be there, just break on that. ...
-
http://www.javaspecialists.co.za/archive/Issue113.html 解决方案开始与您的int类似,作为枚举定义的一部分。 然后,他继续创建一个基于泛型的查找实用程序: public class ReverseEnumMap
& EnumConverter> { private Map map = new HashMap (); public ReverseEnumMap(Class ... -
您可以使用这样的导入和单行代码: import ctypes # An included library with Python install. ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1) 或者定义一个函数(Mbox),如下所示: import ctypes # An included library with Python install. def Mbox(title, text, style): ...
-
我会在黑暗中采取非常狂野的刺: phpMyAdmin实际上是正确的。 不起作用的是您的自定义界面,因为它没有正确设置连接编码 。 因此,您通过它插入的所有数据都会混淆在数据库中,但恰好在您的应用程序中有一个干净的往返。 phpMyAdmin插入实际的 UTF-8数据,这些数据在通过错误连接检索时会混乱。 一直查看UTF-8并在Web应用程序中处理Unicode前后 。 I'll just take a very wild stab into the dark: phpMyAdmin actually get ...
-
在MATLAB中,如何方便地为函数提供多个参数?(In MATLAB, how can I conveniently supply many arguments to a function?)[2022-05-22]
重写您的函数以接受这两个约定: function [] = myfun(v1,v2,v3,v4,v5,v6) if nargin==1 v2 = v1(:,2); v3 = v1(:,3); v4 = v1(:,4); v5 = v1(:,5); v6 = v1(:,6); v1 = v1(:,1); end %# use v1,v2,v3,v4,v5,v6 %# ... end ... -
通常情况下,Agda可以为您挑选的唯一方法是由上下文唯一确定的方式。 在EqReasoning的情况下,通常没有足够的信息来确定Setoid ,实际上更糟糕的是:你可以在同一个Carrier有两个不同的Setoid和_≈_ (例如考虑两个定义上不相等的_≈_证明isEquivalence字段)。 但是,Agda确实允许使用特殊形式的隐式参数,只要只有一个所需类型的值,就可以填充它们。 这些被称为实例参数(在Haskell类型类实例中认为实例)。 为了大致证明这是如何工作的: postulate A : ...
-
您可以使用静态在模板中调用randomUUID : ${statics["java.util.UUID"].randomUUID()} You can use statics to call randomUUID in your template: ${statics["java.util.UUID"].randomUUID()} This object exposes just about any class' static methods if it's used as a hash with ...
-
如何在Go中高效方便地解析简单的消息?(How can I efficiently and conveniently parse a simple message in Go?)[2022-05-04]
文本/扫描程序将过多的开销(在程序员时间)用于这种简单的格式。 net / http使用net / textproto ,你也可以使用它。 查找MIMEHeader和ReadMIMEHeader 。 如果您需要比MIMEHeader可以包含的更复杂的东西,我会考虑使用JSON。 text/scanner would be way too much overhead (in programmer time) to use for a format this simple. net/http uses net/ ...