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

2. Write a Java application to display the following GUI. At this point you are

ID: 3660241 • Letter: 2

Question

2. Write a Java application to display the following GUI.

At this point you are only implementing the display. This program has the following requirements: 1. The textfield should accommodate 3 characters. 2. The X and Y labels should be next to the corresponding textfields. Hint: Put each label and its adjacent text field into a panel, then put both panels into another panel! 3. The buttons should have 10 pixels horizontal spacing and 5 pixels vertical spacing. 4. For the layout of the JFrame, use FlowLayout( FlowLayout.CENTER, 10, 5). 5. Your application should be implemented in a single class. The main method of the class does nothing more than create an object of the class. The constructor of the class creates and displays the GUI. The constructor may call other methods of the class if needed. 6. Do not use inheritance for this program. Use a JFrame member variable as the main window object for this program.

Explanation / Answer

Here you go mate. The exact spacing isnt correct, but you can easily change that. The class name is "Allign" btw, so make your java project have that name if you want to run.



import java.awt.BorderLayout;

public class Allign extends JFrame
{

   private JPanel contentPane;
   private JTextField textField;
   private JTextField textField_1;

   /**
    * Launch the application.
    */
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            try
            {
               Allign frame = new Allign();
               frame.setVisible(true);
            } catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      });
   }

   /**
    * Create the frame.
    */
   public Allign()
   {
      setTitle("Allign");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 334, 179);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(null);
     
      JCheckBox chckbxNewCheckBox = new JCheckBox("Snap to Grid");
      chckbxNewCheckBox.setBounds(6, 58, 97, 23);
      contentPane.add(chckbxNewCheckBox);
     
      JCheckBox chckbxShowGrid = new JCheckBox("Show Grid");
      chckbxShowGrid.setBounds(6, 101, 97, 23);
      contentPane.add(chckbxShowGrid);
     
      textField = new JTextField();
      textField.setText("8");
      textField.setBounds(144, 59, 40, 20);
      contentPane.add(textField);
      textField.setColumns(10);
     
      textField_1 = new JTextField();
      textField_1.setText("8");
      textField_1.setBounds(144, 102, 40, 20);
      contentPane.add(textField_1);
      textField_1.setColumns(10);
     
      JLabel lblX = new JLabel("X:");
      lblX.setBounds(109, 62, 17, 14);
      contentPane.add(lblX);
     
      JLabel lblY = new JLabel("Y:");
      lblY.setBounds(109, 105, 17, 14);
      contentPane.add(lblY);
     
      JButton btnOk = new JButton("OK");
      btnOk.setBounds(228, 40, 89, 23);
      contentPane.add(btnOk);
     
      JButton btnCancel = new JButton("Cancel");
      btnCancel.setBounds(228, 74, 89, 23);
      contentPane.add(btnCancel);
     
      JButton btnHelp = new JButton("Help");
      btnHelp.setBounds(228, 101, 89, 23);
      contentPane.add(btnHelp);
   }
}

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