首页 \ 问答 \ 如何与不同类中的swing组件进行交互(How to interact with swing components in different classes)

如何与不同类中的swing组件进行交互(How to interact with swing components in different classes)

我只想弄清楚一些关于从其他类中获取信息的信息。

我有一个主类使用几个类来构建一个swing gui。 我如何从另一个类向这些组件写入信息。 据我所知,我需要使用arraylist来存储对这些组件的引用,但我不确定如何做到这一点,有人可以帮助我吗?


I'm just trying to figure some things out in my head about getting information to and from swing components from other classes.

I have a main class that uses a few classes to build a swing gui. How do I go about writing information to these components from another class. As far as I understand I need to use an arraylist to store references to these components but I'm not exactly sure how to do this, can someone please help me out?


原文:https://stackoverflow.com/questions/10583477
更新时间:2023-08-28 08:08

最满意答案

虽然莱昂纳多的答案是好的,但除非你知道对象是非可选的,否则它会导致异常,这是一个更好的模式,应该被视为伪代码:

if let frame = NSScreen.screens()?.first?.frame {
   // Do something with frame.
}

换句话说,使用可选链接( ?. ),但只使用let与链的最后一部分。

如果要链接不同类型的选项,也可以创建这样的运算符,其中只返回最后一个的值:

infix operator ??? { associativity left }

func ??? <T1, T2>(t1: T1?, t2: @autoclosure () -> T2?) -> T2? {
    return t1 == nil ? nil : t2()
}

if let frame = self.window ??? NSScreen.screens()?.first?.frame {
    // Do something with frame
}

当然,这是受到Haskell的约束的启发。 但就像这样有趣,我不推荐它。 我认为这很明显是泥巴。 (顺便说一句, ?????之间的区别???前者不要求lhs和rhs属于同一类型(或超类型),而后者则需要。 ???总是返回其rhs或nil 。)


While Leonardo's answer is a good one, it can lead to exceptions unless you know the objects are non-optional, a better pattern is this, which should be regarded as pseudocode:

if let frame = NSScreen.screens()?.first?.frame {
   // Do something with frame.
}

In other words, use optional chaining (?.), but only use let with the very last part of the chain.

You could also create an operator like this if you want to chain optionals that are of a different type, in which only the value of the last one will be returned:

infix operator ??? { associativity left }

func ??? <T1, T2>(t1: T1?, t2: @autoclosure () -> T2?) -> T2? {
    return t1 == nil ? nil : t2()
}

if let frame = self.window ??? NSScreen.screens()?.first?.frame {
    // Do something with frame
}

This is, of course, inspired by Haskell's bind. But as fun as this is, I don't recommend it. I think it's clear as mud. (By the way, the difference between ??? and ?? is that the former does not require the lhs and rhs to be of the same type (or supertype), while the latter does. ??? always returns its rhs or nil.)

相关问答

更多
  • 如果通过从界面构建器中的元素拖动到源文件来创建IBOutlet,则它们默认为隐式展开的Optionals(IUO) ! 的。 您可以手动将它们更改为适当的选项? 这样你就可以小心处理它们并让编译器检查所有的情况。 当他们可能是nil你可能会感兴趣。 这些是我想到的一些,虽然我可能错过了一些。 在视图加载之前(在调用viewDidLoad之前),它们将为零。 如果你明确地将它们设置nil 。 如果该项已从Interface Builder中删除。 如果已从Interface Builder中删除插座。 如果在 ...
  • 虽然莱昂纳多的答案是好的,但除非你知道对象是非可选的,否则它会导致异常,这是一个更好的模式,应该被视为伪代码: if let frame = NSScreen.screens()?.first?.frame { // Do something with frame. } 换句话说,使用可选链接( ?. ),但只使用let与链的最后一部分。 如果要链接不同类型的选项,也可以创建这样的运算符,其中只返回最后一个的值: infix operator ??? { associativity left } ...
  • 好消息。 Swift 1.2 (XCode 6.3 beta,发布于2/9/15)现在支持单行展开多个可选项。 不需要更多的元组/开关模式匹配。 它实际上非常接近你原来的建议语法(感谢听,苹果!) if let email = emailField?.text, password = passwordField?.text { } 另一件好事是你也可以添加一个“守卫条件”: var email: String? = "baz@bar.com" var name: String? = "foo" if ...
  • 您可以将“if let”代码链接在一起,这样至少它看起来并不那么难看。 然后而不是; if let ... if let ... if let ... etc 你有 if let ..., let ..., let ... { // All good, do stuff } else { // Something went wrong } 那你只需要处理一个其他条件。 You can chain the "if let" code together s ...
  • 声明时[ var a:Int ]包含什么 没有。 它的价值是不确定的。 在分配值之前使用它是编译错误 。 您可以在不初始化的情况下声明它,但不能使用它。 这是Swift的安全哲学的一部分:在C中,你也可以保留一个未初始化的变量,它的值将是未定义的,但是如果你使用它,编译器将不会(默认情况下)报告错误。 那么我们为什么不将它声明为正常变量呢? 没有理由我可以想到使一个局部变量是一个隐式解包的可选项。 该功能适用于结构或类的属性 (“ivars”)。 您应该在无法在对象的init设置属性的情况下使用它们,但可以 ...
  • 您可以使用Optional Binding访问登录用户,以确保在将其用作查询参数之前不是nil 。 所以你应该改变这一行: query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!) 附: if let userObj = PFUser.currentUser()?.objectId { query.whereKey("follower", equalTo: userObj) } 现在,这将解决您的错误并防止您的应用程 ...
  • Swift需要显式声明可选的类型,因此第一个代码段是创建可空字符串的示例: var optionalString: String? = "Hello" optionalString = nil 为了使用可以为空的字符串,它需要实现它与它的作用! 后缀如此转换String? 你可以做一个String : var name : String = optionalName! 但Swift还提供了在条件块内检查和实现可为空的简写,例如: if let name = optionalName { gree ...
  • NSNull是一个特殊值,通常由JSON处理产生。 它与nil值非常不同。 并且您不能强制将对象从一种类型转换为另一种类型,这就是您的代码失败的原因。 你有几个选择。 这是一个: let action = self.info?["action"] // An optional if let action = action as? String { // You have your String, process as needed } else if let action = action as? ...
  • 如果您的回调有可选项,为什么要强制解包? 你没有详细说明。 这两个都是可选项,但你强制解开错误一。 像这样使用回调: callback(details, error) 至于进入的文本。只需在获取之前执行此操作: guard let itemText = itemTextField.text else { return } 或者,如果您想要使用空字符串调用该函数,则可以执行此操作 let itemText = itemTextField.text ?? "" 然后像这样使用itemText: servi ...
  • 这两行之间没有区别: var beaconGroup:GroupData = filteredArray.firstObject? as GroupData var beaconGroup:GroupData = filteredArray.firstObject as GroupData 在第一, ? 是不必要的 - firstObject已经返回一个Optional。 使用可选的链接运算符而不实际链接其他成员查找或访问表达式无效。 在Swift 1.2(目前在Xcode 6.3 beta中可用)中, ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。