GUI's and inputting doubles or ints
hey, im working on a basic gui program and i need to input some numbers from the interface so i can calculate their average.
So far my program is working, except for that part
Code:
import javax.swing.*; // Required library
import java.awt.*; // Required library
import java.awt.event.*; // Required library
public class MyGUI extends JFrame {
private JTextField myExam = new JTextField(""); // Create a text field
private JLabel labelWithExam = new JLabel("Exam avg");
private JTextField myHW = new JTextField(""); // Create a text field
private JLabel labelWithHW = new JLabel("HW avg");
private JTextField myLab = new JTextField(""); // Create a text field
private JLabel labelWithLab = new JLabel("Lab Avg");
private JTextField myQuiz = new JTextField(""); // Create a text field
private JLabel labelWithQuiz = new JLabel("Quiz Avg");
private JButton myButton = new JButton("Calculate"); // Create a new button
// Constructor
public MyGUI() {
setLayout(new GridLayout(5, 3, 5, 5)); // Define layout as a grid
add(labelWithExam);
add(myExam);
add(labelWithHW);
add(myHW);
add(labelWithLab);
add(myLab);
add(labelWithQuiz);
add(myQuiz);
add(myButton); // Make button appear in window
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ex = myExam.getText(); // Get the text in the TextField
String h = myHW.getText(); // Get the text in the TextField
String l = myLab.getText(); // Get the text in the TextField
String q = myQuiz.getText(); // Get the text in the TextField