here i attached my question ... and i have done the first part and i stuck in the logic part ,can anybody help

Attachments
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class practical9 extends JFrame 
                       implements ActionListener {


  private JButton button1;
   private JButton button2;
  private JTextField field1;
  private JTextField field2;
  private JTextArea area;
 
  

  public practical9() {
     setSize(300,200);
     setLocation(250,150);
  	 
     button1 = new JButton("Multiply");
     button1.addActionListener(this);
     button2 = new JButton("Add");
     button2.addActionListener(this);
     field1 = new JTextField(8);
     field2= new JTextField(8);
     area = new JTextArea(3,20);
OO_A2.png 15.4 KB

Then post what you have done so far. The code that there is in the txt is not complete and doesn't compile. You are missing the closing brackets

i have corrected it.pls help me check is it correct .and i am stuck with the logic part

import javax.swing.*;
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class practical9 extends JFrame 
                       implements ActionListener {


  private JButton button1;
  private JButton button2;
  private int count = 0;
  private JTextField field1;
  private JTextField field2;
  private JTextArea area;
  private JLabel label1;
  private JLabel label2;
  
  

  public practical9() {
     setSize(300,200);
     setLocation(250,150);
  	 
     button1 = new JButton("Multiply");
     button1.addActionListener(this);
     button2 = new JButton("Add");
     button2.addActionListener(this);
     field1 = new JTextField(8);
     field2= new JTextField(8);
     area = new JTextArea(3,20);
     
      Container container;
     container = getContentPane();
     container.setLayout(new FlowLayout());
     label1 = new JLabel("First number");
     label2 = new JLabel("Second number");
     
     JPanel panel1 = new JPanel();
     panel1.add(label1);
     panel1.add(label2);
     panel1.add(field1);
     panel1.add(field2);
     panel1.add(button1);
     panel1.add(button2);
     
     JPanel panel2 = new JPanel();
     panel2.setLayout(new FlowLayout());
     panel2.add(area);
}
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Look at the ActionListener interface and implement the method that it has. Now whenever you click a button that method is called. You can find which button was clicked by doing:

public void actionPerformed(ActionEvent evt) {
   Object src = ect.getSource();
   if (src==button1) {
      // the button1 was clicked. Use the getText methods of the textfields to get the input and multiply them. Look at the API of the classes you are using. Once you get the result set it to the TextArea
  }
}

Once again look at the API of the classes. You will need the get/set Text methods.

public void actionPerformed(ActionEvent e){
if   (JButton button1=(JButton)e.getSource());
      input = new Input(Integer.parseInt(field1.getText()));
        area.setText(field1.getText() + " A+B = " + input);
        area.setText(field1.getText() + " A*B = " + input);
else
    (JButton button2=(JButton)e.getSource());

am my code correct ?i really dunno how 2 do it le ...

Edited 3 Years Ago by Reverend Jim: Fixed formatting

your code is wrong because you don't use code tags.
Maybe if you did it would be correct, I don't bother checking.

public void actionPerformed(ActionEvent e){
if (JButton button1=(JButton)e.getSource());
input = new Input(Integer.parseInt(field1.getText()));
area.setText(field1.getText() + " A+B = " + input);
area.setText(field1.getText() + " A*B = " + input);
else
(JButton button2=(JButton)e.getSource());


#what do u mean ?where is my error ?how should be the code ?
public void actionPerformed(ActionEvent e){
if (JButton button1=(JButton)e.getSource());
input = new Input(Integer.parseInt(field1.getText()));
area.setText(field1.getText() + " A+B = " + input);
area.setText(field1.getText() + " A*B = " + input);
else
(JButton button2=(JButton)e.getSource());

How is that remotely close with what I just posted. Don't ask if is wrong or not. If it doesn't compile, it is wrong and you will get error messages. Read them.
I gave you code on how to do the checks and what you posted has nothing to do with it. Is it so difficult to read both fields and apply the operators?

read text from field1
read text from field2

add them or multiply them

Use this to turn them into numbers:

String s = "1.23";
double d = Double.parseDouble(s);
public void actionPerformed(ActionEvent e){
        JButton field1=(JButton)e.getSource();
        String text=left.getText();

i really dunno how 2 write the code ...can you help out more pls?

Edited 3 Years Ago by Reverend Jim: Fixed formatting

public void actionPerformed(ActionEvent e){
JButton field1=(JButton)e.getSource();
String text=left.getText();

i really dunno how 2 write the code ...can you help out more pls?

public void actionPerformed(ActionEvent evt) {
   Object src = ect.getSource();
   if (src==button1) { // button1 is for multply from your code
      // look at the API for the classes JTextField, JTextArea
  }
}

Use the getText method of the JTextField class to get the input. Use the other method I told you to convert the input into a number: double d = Double.parseDouble(s); Then use the setText method of the JTextArea to set the result

This article has been dead for over six months. Start a new discussion instead.