问题描述:

and thanks for taking a look at my question.

I am working on a program that accepts user input for height and width and, depending on which option is chosen from a drop down menu, either calculates the area of a rectangle or the circumference of a circle.

I have everything working, so far, but am having trouble with the ActionEvents. What I need guidance on is how to change the calculation formulas depending on which option is chosen from the drop down menu.

I'm also having trouble setting up the computebtn to compute the area or circumference depending on which formula has been selected, and getting the input from the JTextFields to be used in the formulas. If I try to use getText() I get this error:

 error: incompatible types: string cannot be converted to int

TL;DR: Need help implementing two ActionEvents. One to change labels and formulas and one to handle the computebtn in order to obtain an answer. Also need help getting the input from the user into the formulas.

Any and all help will be greatly appreciated.

my code so far:

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);

}

}

网友答案:
  1. Don't compare Strings with == since this compares for reference equality, i.e., if one String refers to the same String object as another variable, something you don't care about. You want to test if each String holds the same chars in the same order, and for that test for String equality with the equals(...) method. Or use the equalsIgnoreCase(...) if you don't care about case. So change if(arg == "Area") to if(arg.equals("Area"))
  2. Get your JTextField text with getText(), but then parse the String into an int with Integer.parseInt(...) before trying to use it as an int.

i.e.,

int width = Integer.parseInt(widthfld.getText());
相关阅读:
Top