gpt4 book ai didi

java - 我正在尝试用 Java 制作 GUI

转载 作者:行者123 更新时间:2023-12-02 05:55:08 27 4
gpt4 key购买 nike

所以我正在尝试用Java制作一个GUI。我对 Java GUI 还很陌生。这是我的代码:

private class thehandler implements  ActionListener {
public void actionPerformed(ActionEvent event){//this is what is going to handle an event
String string = "";

if(event.getSource() == item1)//if they click enter on item1
string=String.format("field 1: %s", event.getActionCommand());
else if(event.getSource() == item2)//if they click enter on item2
string = String.format("field 2: %s", event.getActionCommand());
else if(event.getSource() == item3)//if they click enter on item3
string = String.format("field 3: %s", event.getActionCommand());
else if(event.getSource() == passField)//if they click enter on passField
string = String.format("Password field is: %s", event.getActionCommand());
}
}

我在 string=String.format("field 1: %s", event.getActionCommand()); 上收到错误以及所有其他 String.format 行。它说“字符串类型中的方法格式(字符串,对象[])不适用于参数(字符串,字符串)”

我不知道如何解决这个问题。我刚刚下载了 JRE 和 JDK 8(如果有帮助的话)。谢谢!

最佳答案

简单地将其转换为字符串

event.getActionCommand().toString()

或者(没有 stringFormat):

string="field 1:" + event.getActionCommand();

关于java - 我正在尝试用 Java 制作 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174982/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com