I have a problem with a project i'm working on, and I was wondering if it was possible to do what i'm thinking. Basically I have a simple mathematical GUI. I want it to take 2 inputs multiply them and output the answer. I am doing this all through the GUI. I have the GUIs set up, but everything I've tried has not been able to transfer the inputs from one GUI to the other (only want the values). Another thing i'm trying to do is have 2 GUI setups, one with 2 inputs and 2 buttons (I have that one) and another with 3 JTextFields (2 of which are invisible that hold the input values of the first GUI). I am doing this in eclipse. Posting Code below. Also I am new to Java programming, so if my code looks awful, that's why.
Basically I want 2 inputs from one GUI to transfer to another GUI that multiplies them together. Also if there is an easier way that I didn't realize that makes it all in one GUI and is super simple, that would also be helpful.
I also do not know how to make my code robust enough to only allow the user to input numbers. Again, new to Java. Thank you for your time for those who sit down to read all of this!
This is my Main code
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.io.*;
class Proejct extends button{
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws IOException {
SecondClass brandon = new SecondClass();
brandon.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
brandon.setSize(500, 500);
brandon.setVisible(true);
JButton button = new JButton("Close", new CloseAction());
brandon.add(button);
button.setBounds(85, 48, 241, 21);
button.setToolTipText("Click me!");
button.addActionListener(new CloseListener());
JButton button1 = new JButton("Calculate");
brandon.add(button1);
button1.setToolTipText("This will calculate your variables!");
button1.addActionListener(new Action());
}
}
(imports)
(my close class)
class Action extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) {
SecondClass second = new SecondClass();
second.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
second.setSize(500, 500);
second.setVisible(true);
JButton button = new JButton("Close", new CloseAction());
second.add(button);
button.setBounds(85, 48, 241, 21);
button.setToolTipText("Click me!");
button.addActionListener(new CloseListener());
}
}
(This is the part of my code that is causing me the most problems, I don't know how to trasnfer the inputs from the first GUI to the second. I have tried a lot of things, but nothing has worked)
NEW CLASS
import java.awt.FlowLayout;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class SecondClass extends JFrame {
public interface num1 {
JTextField num1 = new JTextField(10);
static String getText() {
// TODO Auto-generated method stub
return null;
}
}
public interface num2 {
JTextField num2 = new JTextField(10);
}
public JTextField closed;
public JTextField NS1;
public JTextField NS2;
public static dubleClass dubleClass;
public static Object num2;
public static Object num1;
public SecondClass() {
super("Newton's First Law");
setLayout(new FlowLayout());
JTextField num1 = new JTextField(10);
num1.setToolTipText("Type your Mass here!");
add(num1);
JTextField num2 = new JTextField(10);
num2.setToolTipText("Type your Acceleration here!");
add(num2);
/* public static final getnum1 {
}*/
}
public class dubleClass extends SecondClass {
public dubleClass() {
closed = new JTextField(("This is your answer!"));
closed.setEditable(false);
closed.setVisible(true);
add(closed);
// NS1.setText(((JTextComponent) num1).getText());
// dubleClass.NS1.setText(SecondClass.dubleClass.num1);
NS1.setVisible(false);
add(NS1);
NS2.setText(((JTextComponent) num2).getText());
// dubleClass.NS2.setText(this.num2);
NS2.setVisible(false);
add(NS2);
// double dub1 = Double.parseDouble(num1.getText());
// double dub2 = Double.parseDouble(num2.getText());
System.out.println(NS2);
System.out.println(NS1);
System.out.println(num2);
System.out.println(num1);
System.out.println(closed);
}
private void dubleClass(FlowLayout flowLayout) {
// TODO Auto-generated method stub
}
private dubleClass(FlowLayout flowLayout) {
}
}
}
Another failed attempt, but the code is still used.
NEW CLASS
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class dubbleClass extends JFrame{
private static final long serialVersionUID = 1L;
public JTextField item1; //2 text fields
public JTextField item2;
// make text fields so user can input values for mass and acceleration
public dubbleClass() {
super("Newton's First Law");
setLayout(new FlowLayout());
// layout of gui, as in title
//add(button)
JTextField num1 = new JTextField();
num1.setToolTipText("Type your Mass here!");
add(num1);
JTextField num2 = new JTextField();
num2.setToolTipText("Type your Acceleration here!");
add(num2);
// double dub1 = Double.parseDouble(num1.getText());
// double dub2 = Double.parseDouble(num2.getText());
// double dub3 = dub2 * dub1;
// String item3 = Double.toString(dub3);
// example texts for where to input what
// change my JTextField to a double
@SuppressWarnings("unused")
class button1 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public button1(){
double dub1 = Double.parseDouble(num1.getText());
double dub2 = Double.parseDouble(num2.getText());
double dub3 = dub2 * dub1;
String item3 = Double.toString(dub3);
new JTextField(item3);
// double num1 = Double.parseDouble(item1.getText());
// double num2 = Double.parseDouble(item2.getText());
// double num3 = Double.parseDouble(item3.getText());
// num3 = num1*num2;
System.out.println(item3);
// String Force = Double.toString(num3);
// System.out.println(Force);
}
// multiplies mass and acceleration to get force
}
class Action implements ActionListener {
public void actionPerformed(ActionEvent e) {
double dub1 = Double.parseDouble(num1.getText());
double dub2 = Double.parseDouble(num2.getText());
double dub3 = dub2 * dub1;
System.out.println(dub3);
String s = Double.toString(dub3);
new JTextField(s);
}
Basically i'm using my Proeject class to make a gui with the 2 text fields and 2 buttons. The first Gui called is SecondClass. When the "Calculate" button is presssed, I want it to make a new gui with dubbleClass and have 3 JTextFields where 2 are the inputs and invisible, and the other one is the two numbers multiplied together then outputted as a string saying something like "Your force is %d" or something like that. Again, thank you for your time.