作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在尝试用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/
我是一名优秀的程序员,十分优秀!