how do i get the answer to appear in textFieldD (line 92)
Code java:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Week02 {
private JFrame frame;
private JTextField textFieldA;
private JTextField textFieldB;
private JTextField textFieldC;
private JTextField textFieldD;
private JButton btnNewButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Week02 window = new Week02();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Week02() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 546, 412);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textFieldA = new JTextField();
textFieldA.setBounds(30, 31, 173, 53);
frame.getContentPane().add(textFieldA);
textFieldA.setColumns(10);
textFieldB = new JTextField();
textFieldB.setBounds(30, 119, 173, 53);
frame.getContentPane().add(textFieldB);
textFieldB.setColumns(10);
textFieldC = new JTextField();
textFieldC.setBounds(30, 206, 173, 58);
frame.getContentPane().add(textFieldC);
textFieldC.setColumns(10);
textFieldD = new JTextField();
textFieldD.setEditable(false);
textFieldD.setBounds(30, 288, 173, 53);
frame.getContentPane().add(textFieldD);
textFieldD.setColumns(10);
JButton compute = new JButton("Compute");
compute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String Input;
double textFieldA;
double textFieldB;
double textFieldC;
double textFieldD;
double Add;
double Multiply;
double Divide;
double Subtract;
if (textFieldC == Add) {
double a, b, c;
a = Double.parseDouble(Input);
b = Double.parseDouble(Input);
c = a + b;
textFieldD (+ c); //how do we send the answer to textFieldD???? this method isn't working
}
}
});
compute.setBounds(233, 224, 139, 29);
frame.getContentPane().add(compute);
btnNewButton = new JButton("Instructions");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "All possable imputs \n" +
"Add \n" +
"Subtract \n" +
"Multiply \n" +
"Divide \n");
}
});
btnNewButton.setBounds(382, 224, 123, 29);
frame.getContentPane().add(btnNewButton);
}
}
Re: how do i get the answer to appear in textFieldD (line 92)
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
text fields have methods you can call to set the text that is being shown. Read the API doc to see what methods the class has.
Java Platform SE 7
Re: how do i get the answer to appear in textFieldD (line 92)
i wrapped the code. i really don't know what that line needs to be to display the answer in a textfield. :/
Re: how do i get the answer to appear in textFieldD (line 92)
Read the API doc to see what methods the class has. You will need to use one of them to display the data.
Re: how do i get the answer to appear in textFieldD (line 92)
what if i dont know what class?
Re: how do i get the answer to appear in textFieldD (line 92)
I don't understand how you don't know the class of a variable.
One way to find the class of a variable is to use the editor on the source code and do a Find for the variable's name. Some where in the code there will be a definition for the variable. When you find the definition the name of the class will be on the line with the variable name to the left of the name.