Question

So, I have a Class called Math, that runs a small piece of code, and another class called GUI that holds a button and a text box. I need to make it so pressing the button runs the Math class.

In the Math Class:

public class Math {

 public static int com;

 public static void main (String[] args) {
     com = GUI.num1 * GUI.num2;
     GUI.TextBox3.setText(com);
    }
}

In the GUI class (It's jPanel, by the way):

public class GUI extends javax.swing.JFrame {
public static int num1;
public static int num2;

public GUI() {
    initComponents();
}
private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
      num1 = Integer.parseInt(TextBox1.getText());
      num1 = Integer.parseInt(TexBox2.getText());
      Math.main (Sring[] );//This is the part the doesn't work

}      

If you could help, that would be great, thanks!

Was it helpful?

Solution

class MyClass {

     public static int com;
     public static void Method()
       {
       com = GUI.num1 * GUI.num2;
       GUI.TextBox3.setText(""+com);
       }

    }

And

private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {        
          num1 = Integer.parseInt(TextBox1.getText());
          num2 = Integer.parseInt(TexBox2.getText());
          MyClass.Method();
    } 

This will be more Convenient.

Here

 GUI.TextBox3.setText(com);

TextBox3 also needs to be static.

NOTE: You even don't need the class for calculation directly calculate

TextBox3.setText(""+(num1*num2));

OTHER TIPS

You better don't call your class Math since java already have a class called like this, it might create some conflict.

What is your error?

Looking at your code, a don't understand how your Math class can access your GUI, is this all the code?

You better replace your Math.main(...) with :

private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    num1 = Integer.parseInt(TextBox1.getText());
    num2 = Integer.parseInt(TexBox2.getText());
    TextBox3.setText(String.valueOf(num1 * num2));
}

Edit, fixed an error

Here is a more appropriate answer

public class MyMath {

 public static int com;

 public int squareIt(int a, int b) {
     com = a * b;
     return com; // return a*b;
    }
}

This would make your life a whole lot easier and you could modify your front-end(GUI) like so:

public class GUI extends javax.swing.JFrame {
public static int num1;
public static int num2;

public GUI() {
    initComponents();
}
private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
      num1 = Integer.parseInt(TextBox1.getText());
      num2 = Integer.parseInt(TexBox2.getText());
      TextBox3.setText(""+new MyMath().squareIt(num1,num2));

}  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top