Java中的ActionEvent和getText()有问题

Java中的ActionEvent和getText()有问题,java,swing,awt,actionevent,Java,Swing,Awt,Actionevent,谢谢你看我的问题 我正在开发一个程序,该程序接受用户输入的高度和宽度,并根据从下拉菜单中选择的选项,计算矩形的面积或圆的周长 到目前为止,我一切正常,但在ActionEvents方面遇到了麻烦。我需要的指导是如何根据从下拉菜单中选择的选项更改计算公式 我还无法设置computebtn来计算面积或周长,这取决于选择了哪个公式,并且无法从要在公式中使用的JTextFields中获取输入。如果尝试使用getText(),则会出现以下错误: error: incompatible types:

谢谢你看我的问题

我正在开发一个程序,该程序接受用户输入的高度和宽度,并根据从下拉菜单中选择的选项,计算矩形的面积或圆的周长

到目前为止,我一切正常,但在ActionEvents方面遇到了麻烦。我需要的指导是如何根据从下拉菜单中选择的选项更改计算公式

我还无法设置computebtn来计算面积或周长,这取决于选择了哪个公式,并且无法从要在公式中使用的JTextFields中获取输入。如果尝试使用getText(),则会出现以下错误:

    error: incompatible types: string cannot be converted to int
TL;DR:需要帮助实现两个ActionEvents。一个用于更改标签和公式,另一个用于处理计算机以获得答案。还需要帮助将用户的输入输入到公式中

我们将非常感谢您的任何帮助

到目前为止,我的代码是:

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);
    }
}
  • 不要将字符串与
    ==
    进行比较,因为这会比较引用相等性,也就是说,如果一个字符串与另一个变量引用同一个字符串对象,那么这是您不关心的。您需要测试每个字符串是否以相同的顺序持有相同的字符,并使用
    equals(…)
    方法测试字符串是否相等。如果您不关心大小写,也可以使用
    equalsIgnoreCase(…)
    。因此,将
    if(arg==“Area”)
    更改为
    if(arg.equals(“Area”)
  • 使用
    getText()
    获取JTextField文本,然后使用
    Integer.parseInt(…)
    将字符串解析为int,然后再尝试将其用作int
  • i、 e


    这个,
    if(arg==“Area”)
    让我担心。不要将字符串与
    =
    进行比较。使用
    equals(…)
    @HovercraftFullOfEels这是下拉菜单中的简单操作命令,用于确定已做出的选择。这与选择的目的无关——这是错误的,可能导致程序逻辑无法正常运行。请查看编辑以回答。如果您对此有任何疑问,请询问。@AndrewThompson有一个下拉菜单“Type”,它有两个选项:“Area”或“personal”。用户首先选择其中一个选项,该选项将确定使用输入的数字进行什么计算。一旦选择了计算类型并输入了数据,computebtn就会计算用户输入的数据。我的问题是:我应该把公式放在哪里,使公式与菜单中的选项相对应,以及如何让计算机计算公式并显示到我的输出LBL?啊,我明白了。现在这更有意义了。我觉得自己很笨,因为我试图使用.parseInt(),但我忘记了“.”之前的整数。现在,我该如何处理computebtn的ActionEvent?@ebonymessiah:我不明白你上面的问题。计算机有什么不同?你到底卡住了什么?我不知道公式到底放在哪里,也不知道如何让计算机来计算所选的公式。换句话说,我已经拥有的ActionEvent是否可以处理选择公式和单击按钮进行计算?我是新来的java@ebonymessiah:我还是不明白你到底想让那个按钮做什么?什么计算?当按下该按钮时,用户如何告诉GUI要执行哪些计算?
    int width = Integer.parseInt(widthfld.getText());