在 Java 中遇到 ActionEvent 和 getText() 问题
Having trouble with ActionEvent and getText() in Java
感谢您查看我的问题。
我正在开发一个程序,该程序接受用户输入的高度和宽度,并根据从下拉菜单中选择的选项计算矩形的面积或圆的周长。
到目前为止,我一切正常,但在使用 ActionEvents 时遇到了问题。我需要指导的是如何根据从下拉菜单中选择的选项更改计算公式。
我在设置 computebtn 以根据所选公式计算面积或周长以及从 JTextFields 获取要在公式中使用的输入时也遇到了问题。如果我尝试使用 getText() 我会收到此错误:
error: incompatible types: string cannot be converted to int
TL;DR:需要帮助实现两个 ActionEvent。一个用于更改标签和公式,一个用于处理 computebtn 以获得答案。还需要帮助将用户的输入输入到公式中。
我们将不胜感激任何帮助。
到目前为止我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class areaOrCircumference extends JFrame implements ActionListener
{
int height;
int width;
int area;
int circumference;
JLabel label = new JLabel("");
JButton computebtn = new JButton("Compute");
JLabel widthlbl = new JLabel("Enter Width:");
JTextField widthfld = new JTextField(10);
JLabel heightlbl = new JLabel("Enter Height:");
JTextField heightfld = new JTextField(10);
JLabel outputlbl = new JLabel();
JPanel labelPanel = new JPanel();
JPanel inputPanel = new JPanel();
JPanel computePanel = new JPanel();
public areaOrCircumference()
{
setLayout(new BorderLayout());
add(labelPanel, BorderLayout.NORTH);
labelPanel.add(label);
add(inputPanel, BorderLayout.CENTER);
inputPanel.setLayout(new GridLayout(3,2));
inputPanel.add(widthlbl);
inputPanel.add(widthfld);
inputPanel.add(heightlbl);
inputPanel.add(heightfld);
inputPanel.add(outputlbl);
add(computePanel, BorderLayout.SOUTH);
computePanel.setLayout(new FlowLayout());
computePanel.add(computebtn);
computebtn.addActionListener(this);
}
public JMenuBar createMenuBar()
{
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
JMenu mnuType = new JMenu("Type", true);
mnuBar.add(mnuType);
JMenuItem mnuArea = new JMenuItem("Area");
mnuType.add(mnuArea);
mnuArea.setActionCommand("Area");
mnuArea.addActionListener(this);
JMenuItem mnuCirc = new JMenuItem("Circumference");
mnuType.add(mnuCirc);
mnuCirc.setActionCommand("Circumference");
mnuCirc.addActionListener(this);
return mnuBar;
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if(arg == "Area")
{
label.setText("Area of a rectangle");
area = width*height;
}
if(arg =="Circumference")
{
label.setText("Circumference of a Circle");
circumference = 2*width + 2*height;
}
}
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
areaOrCircumference f = new areaOrCircumference();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(f.createMenuBar());
f.setTitle("Area/Circumference Calculator");
f.setBounds(300,300,475,400);
f.setVisible(true);
}
}
- 不要将字符串与
==
进行比较,因为这是比较引用相等性,即,如果一个字符串引用与另一个变量相同的字符串对象,这是您不关心的。您想要测试每个 String 是否以相同的顺序包含相同的字符,并使用 equals(...)
方法测试 String 是否相等。或者如果您不关心大小写,请使用 equalsIgnoreCase(...)
。所以将 if(arg == "Area")
更改为 if(arg.equals("Area"))
- 使用
getText()
获取 JTextField 文本,然后使用 Integer.parseInt(...)
将字符串解析为 int,然后再尝试将其用作 int。
即
int width = Integer.parseInt(widthfld.getText());
感谢您查看我的问题。
我正在开发一个程序,该程序接受用户输入的高度和宽度,并根据从下拉菜单中选择的选项计算矩形的面积或圆的周长。
到目前为止,我一切正常,但在使用 ActionEvents 时遇到了问题。我需要指导的是如何根据从下拉菜单中选择的选项更改计算公式。
我在设置 computebtn 以根据所选公式计算面积或周长以及从 JTextFields 获取要在公式中使用的输入时也遇到了问题。如果我尝试使用 getText() 我会收到此错误:
error: incompatible types: string cannot be converted to int
TL;DR:需要帮助实现两个 ActionEvent。一个用于更改标签和公式,一个用于处理 computebtn 以获得答案。还需要帮助将用户的输入输入到公式中。
我们将不胜感激任何帮助。
到目前为止我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class areaOrCircumference extends JFrame implements ActionListener
{
int height;
int width;
int area;
int circumference;
JLabel label = new JLabel("");
JButton computebtn = new JButton("Compute");
JLabel widthlbl = new JLabel("Enter Width:");
JTextField widthfld = new JTextField(10);
JLabel heightlbl = new JLabel("Enter Height:");
JTextField heightfld = new JTextField(10);
JLabel outputlbl = new JLabel();
JPanel labelPanel = new JPanel();
JPanel inputPanel = new JPanel();
JPanel computePanel = new JPanel();
public areaOrCircumference()
{
setLayout(new BorderLayout());
add(labelPanel, BorderLayout.NORTH);
labelPanel.add(label);
add(inputPanel, BorderLayout.CENTER);
inputPanel.setLayout(new GridLayout(3,2));
inputPanel.add(widthlbl);
inputPanel.add(widthfld);
inputPanel.add(heightlbl);
inputPanel.add(heightfld);
inputPanel.add(outputlbl);
add(computePanel, BorderLayout.SOUTH);
computePanel.setLayout(new FlowLayout());
computePanel.add(computebtn);
computebtn.addActionListener(this);
}
public JMenuBar createMenuBar()
{
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
JMenu mnuType = new JMenu("Type", true);
mnuBar.add(mnuType);
JMenuItem mnuArea = new JMenuItem("Area");
mnuType.add(mnuArea);
mnuArea.setActionCommand("Area");
mnuArea.addActionListener(this);
JMenuItem mnuCirc = new JMenuItem("Circumference");
mnuType.add(mnuCirc);
mnuCirc.setActionCommand("Circumference");
mnuCirc.addActionListener(this);
return mnuBar;
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if(arg == "Area")
{
label.setText("Area of a rectangle");
area = width*height;
}
if(arg =="Circumference")
{
label.setText("Circumference of a Circle");
circumference = 2*width + 2*height;
}
}
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
areaOrCircumference f = new areaOrCircumference();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(f.createMenuBar());
f.setTitle("Area/Circumference Calculator");
f.setBounds(300,300,475,400);
f.setVisible(true);
}
}
- 不要将字符串与
==
进行比较,因为这是比较引用相等性,即,如果一个字符串引用与另一个变量相同的字符串对象,这是您不关心的。您想要测试每个 String 是否以相同的顺序包含相同的字符,并使用equals(...)
方法测试 String 是否相等。或者如果您不关心大小写,请使用equalsIgnoreCase(...)
。所以将if(arg == "Area")
更改为if(arg.equals("Area"))
- 使用
getText()
获取 JTextField 文本,然后使用Integer.parseInt(...)
将字符串解析为 int,然后再尝试将其用作 int。
即
int width = Integer.parseInt(widthfld.getText());