我可以在 Java 的 actionPerformed 方法中使用 switch - case

发布于 2024-08-09 17:49:45 字数 468 浏览 3 评论 0原文

我想检查 ActionEvent ee.getSource() 发生了哪个 actionEvent。我可以为此使用开关盒吗?

public void actionPerformed(ActionEvent e){
    switch(e.getSource()){
        case radius:
            double r = validate(radius.getText());
            break;
        case height:
            double h = validate(height.getText());
            break;
        case out:
            out.setText(String.valueOf(h*r));
            break;
    }
}

I would like to check on which actionEvent has occurred with ActionEvent e and e.getSource(). Can I use a switch case for this?

public void actionPerformed(ActionEvent e){
    switch(e.getSource()){
        case radius:
            double r = validate(radius.getText());
            break;
        case height:
            double h = validate(height.getText());
            break;
        case out:
            out.setText(String.valueOf(h*r));
            break;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅笑轻吟梦一曲 2024-08-16 17:49:45

不,你不能。 switch 语句中可以使用的类型非常有限。
请参阅switch 语句

当然,您可以将其写为一系列“if”和“else if”语句。

No, you can't. The types you can use in a switch statement is very limited.
See The switch Statement.

You can of course just write this as a series of "if" and "else if" statements.

焚却相思 2024-08-16 17:49:45

是的,您可以在 actionPerformed 中使用 switch。

不,您不能像此处所示那样使用它。

switch 仅支持原始类型和 enum(以及 String,但仅在 Java 7 及更高版本中)。

另一个问题是 case-values 值必须是编译时常量。

你需要这样的代码:

public void actionPerformed(ActionEvent e){
    if (e.getSource() == radius) {
        double r = validate(radius.getText());
    else if (e.getSource() == height) {
        double h = validate(height.getText());
    else if (e.getSource() == out) {
        out.setText(String.valueOf(h*r));
    }
}

Yes, you can use switch in actionPerformed.

No, you can't use it like you showed it here.

switch only supports primitive types and enums (and String, but only in Java 7 and later).

Another problem is that the case-values values must be compile time constants.

You'll need code like this:

public void actionPerformed(ActionEvent e){
    if (e.getSource() == radius) {
        double r = validate(radius.getText());
    else if (e.getSource() == height) {
        double h = validate(height.getText());
    else if (e.getSource() == out) {
        out.setText(String.valueOf(h*r));
    }
}
卖梦商人 2024-08-16 17:49:45

正如其他解决方案所指出的,您不能在这种情况下使用 switch。然而,为什么不为每个事件源实现单独的 ActionListeners 呢?这是一种更加基于面向对象的方法。

通常,您的 ActionListener 实现无论如何都是(小型)匿名内部类,因此您不必处理大量的 .java 文件。

As other solutions have pointed out, you cannot use switch in this context. However, rather than implementing one ActionListener containing a big if-then block, why not implement separate ActionListeners for each event source? This is a much more OO-based approach.

Typically your ActionListener implementations would be (small) anonymous inner classes anyway, and hence you wouldn't have to deal with a huge proliferation of .java files.

无法回应 2024-08-16 17:49:45

ActionEvent 包含一个 ActionCommand 字段,该字段是一个字符串。如果在创建按钮时未设置,则默认为按钮的文本。你可以用它来代替。

像这样的东西:

switch(e.getActionCommand()) {
    case "Radius":
          ....
    case "Height":
          ....
}

The ActionEvent contains an ActionCommand field which is a String. If not set while creating the Button it defaults to the text of the button. You can use that instead.

Something like:

switch(e.getActionCommand()) {
    case "Radius":
          ....
    case "Height":
          ....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文