Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'ve got a project almost done but I need help with one more task. As it

ID: 3819946 • Letter: H

Question

Hello, I've got a project almost done but I need help with one more task. As it is when the program compiles it opens a window to check 3 numbers to see if they make a right triangle. There are 3 text boxes, and 2 buttons. The problem I cant figure out is that I don't want any of the buttons in the window to appear until the user hits enter/return. Then the "check numbers" button shows. After the user clicks the "check numbers" button, then the confirmation message with the "clear everything" button shows up. Can someone help fix my problem please? Thank you!

//Triangle.java

package triangleChecker;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class Triangle extends JFrame {

   private JPanel contentPane;
   private JTextField tf1;
   private JTextField tf2;
   private JTextField tf3;

   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           @Override
           public void run() {
               try {
                   Triangle frame = new Triangle();
                   frame.setTitle("PA9: Right Triangle Checker");
                   frame.setResizable(false);

                   frame.setVisible(true);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the frame.
   */
   public Triangle() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 386, 239);
       contentPane = new JPanel();
       contentPane.setBackground(Color.YELLOW);
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       contentPane.setLayout(null);

       JLabel lblNewLabel = new JLabel("Enter your three numbers and press Enter:");
       lblNewLabel.setBounds(27, 24, 256, 14);
       contentPane.add(lblNewLabel);

       tf1 = new JTextField();
       tf1.setBounds(49, 56, 86, 20);
       contentPane.add(tf1);
       tf1.setColumns(10);

       tf2 = new JTextField();
       tf2.setBounds(153, 56, 86, 20);
       contentPane.add(tf2);
       tf2.setColumns(10);

       tf3 = new JTextField();
       tf3.setBounds(259, 56, 86, 20);
       contentPane.add(tf3);
       tf3.setColumns(10);

       JLabel reslt_lbl = new JLabel("New label");
       reslt_lbl.setBounds(27, 132, 269, 14);
       contentPane.add(reslt_lbl);
       reslt_lbl.setText("");

       JButton chk_btn = new JButton("Check Numbers");
       chk_btn.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent arg0) {
               double s1 = 0, s2 = 0, s3 = 0; // declaring side variables
               int flag = 0;
               // parsing the entered value to double and storing to side
               // variables
               try {
                   s1 = Double.parseDouble(tf1.getText());
                   s2 = Double.parseDouble(tf2.getText());
                   s3 = Double.parseDouble(tf3.getText());
               } catch (NumberFormatException ex) {
                   flag = 1;
                   JOptionPane.showMessageDialog(null, "All the fields must be filled and in numbers", "Error",
                           JOptionPane.ERROR_MESSAGE);
               }
               double hyp, sd1, sd2;

               if (flag == 0) {
                   // getting the hypotenuse
                   hyp = s1;
                   sd1 = s2;
                   sd2 = s3;
                   if (hyp < s2) {
                       hyp = s2;
                       sd1 = s1;
                       sd2 = s3;
                   }
                   if (hyp < s3) {
                       hyp = s3;
                       sd1 = s1;
                       sd2 = s2;
                   }
                   if (Math.pow(hyp, 2) == (Math.pow(sd1, 2) + Math.pow(sd2, 2)))
                   {
                       reslt_lbl.setText("Yes, these are the sides of a right triangle.");
                   } else
                       reslt_lbl.setText("No, these are not the sides of a right triangle");
               }

           }
       });
       chk_btn.setBounds(109, 98, 174, 23);
       contentPane.add(chk_btn);

       JButton clear_btn = new JButton("Clear Everything");
       clear_btn.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent arg0) {
               tf1.setText("");
               tf2.setText("");
               tf3.setText("");
               reslt_lbl.setText("");
           }
       });
       clear_btn.setBounds(109, 155, 174, 23);
       contentPane.add(clear_btn);
   }
}

Explanation / Answer

Code :

//Triangle.java
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class Triangle extends JFrame {
private JPanel contentPane;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Triangle frame = new Triangle();
frame.setTitle("PA9: Right Triangle Checker");
frame.setResizable(false);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//Check the values in textboxes and make the button visible.
private void checkValuesAndEnableCheckButton(JButton chk_btn) {
       if(!tf1.getText().isEmpty() && !tf2.getText().isEmpty() && !tf3.getText().isEmpty()) {
           chk_btn.setVisible(true);
       }
   }
  
/**
* Create the frame.
*/
public Triangle() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 386, 239);
contentPane = new JPanel();
contentPane.setBackground(Color.YELLOW);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final JButton chk_btn = new JButton("Check Numbers");
final JButton clear_btn = new JButton("Clear Everything");
JLabel lblNewLabel = new JLabel("Enter your three numbers and press Enter:");
lblNewLabel.setBounds(27, 24, 256, 14);
contentPane.add(lblNewLabel);
tf1 = new JTextField();
tf1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       checkValuesAndEnableCheckButton(chk_btn);
   }
});
tf1.setBounds(49, 56, 86, 20);
contentPane.add(tf1);
tf1.setColumns(10);
tf2 = new JTextField();
tf2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       checkValuesAndEnableCheckButton(chk_btn);
   }
});
tf2.setBounds(153, 56, 86, 20);
contentPane.add(tf2);
tf2.setColumns(10);
tf3 = new JTextField();
tf3.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       checkValuesAndEnableCheckButton(chk_btn);
   }
});
tf3.setBounds(259, 56, 86, 20);
contentPane.add(tf3);
tf3.setColumns(10);
final JLabel reslt_lbl = new JLabel("New label");
reslt_lbl.setBounds(27, 132, 269, 14);
contentPane.add(reslt_lbl);
reslt_lbl.setText("");
chk_btn.setVisible(false);
chk_btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
double s1 = 0, s2 = 0, s3 = 0; // declaring side variables
int flag = 0;
// parsing the entered value to double and storing to side
// variables
try {
s1 = Double.parseDouble(tf1.getText());
s2 = Double.parseDouble(tf2.getText());
s3 = Double.parseDouble(tf3.getText());
} catch (NumberFormatException ex) {
flag = 1;
JOptionPane.showMessageDialog(null, "All the fields must be filled and in numbers", "Error",
JOptionPane.ERROR_MESSAGE);
}
double hyp, sd1, sd2;
if (flag == 0) {
// getting the hypotenuse
hyp = s1;
sd1 = s2;
sd2 = s3;
if (hyp < s2) {
hyp = s2;
sd1 = s1;
sd2 = s3;
}
if (hyp < s3) {
hyp = s3;
sd1 = s1;
sd2 = s2;
}
if (Math.pow(hyp, 2) == (Math.pow(sd1, 2) + Math.pow(sd2, 2)))
{
reslt_lbl.setText("Yes, these are the sides of a right triangle.");
} else
reslt_lbl.setText("No, these are not the sides of a right triangle");
}
clear_btn.setVisible(true);
}
});
chk_btn.setBounds(109, 98, 174, 23);
contentPane.add(chk_btn);
clear_btn.setVisible(false);
clear_btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
tf1.setText("");
tf2.setText("");
tf3.setText("");
reslt_lbl.setText("");
chk_btn.setVisible(false);
clear_btn.setVisible(false);
}
});
clear_btn.setBounds(109, 155, 174, 23);
contentPane.add(clear_btn);
}
}

Comment: 1. Added an action listener to the textboxes for enter/return. In the listener the method "checkValuesAndEnableCheckButton" is called, there it will check for values in the 3 text boxes, is all the boxes have values then only show the "chk_btn".

2. When check button is clicked, made the "clear_btn" visible.

3. When "clear_btn" is clicked cleared all the values and made the buttons invisible.

Hope this will give what you wanted. All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote