import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class OtakuHigh extends JFrame{

   private JLabel label, label1, label2, label3, label4, label5, label6, label7, label8;
   private JTextField text1, text2, text4, text5, text7, text8;
   private JComboBox Mbox, Dbox, Sbox, Ybox, SCbox;
   private JButton Qbutton;
   String[] Month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   String[] Day = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
   String[] Sex = {"Male", "Female"};
   String[] Year = {"Freshmen", "Sophomore", "Junior", "Senior"};
   String[] Sched = {"Morning", "Afternoon", "Evening"};

   public OtakuHigh(){
      super("Otaku High");
      setLayout(new FlowLayout());

      label = new JLabel("Otaku High Registration Form");
      label.setHorizontalAlignment(JLabel.CENTER);
      add(label);

      label1 = new JLabel("Name:");
      label1.setHorizontalAlignment(JLabel.CENTER);
      add(label1);

      text1 = new JTextField(10);
      text1.setHorizontalAlignment(JTextField.CENTER);
      add(text1);

      label2 = new JLabel("Age:");
      label2.setHorizontalAlignment(JLabel.CENTER);
      add(label2);

      text2 = new JTextField(10);
      text2.setHorizontalAlignment(JTextField.CENTER);
      add(text2);

      label3 = new JLabel("Sex:");
      label3.setHorizontalAlignment(JLabel.CENTER);
      add(label3);

      JComboBox<String> Sbox = new JComboBox<String>(Sex);
      add(Sbox);

      label4 = new JLabel("Birthday:");
      label4.setHorizontalAlignment(JLabel.CENTER);
      add(label4);

      JComboBox<String> Mbox = new JComboBox<String>(Month);
      add(Mbox);

      JComboBox<String> Dbox = new JComboBox<String>(Day);
      add(Dbox);

      text4 = new JTextField(10);
      text4.setHorizontalAlignment(JTextField.CENTER);
      add(text4);

      label5 = new JLabel("Address:");
      label5.setHorizontalAlignment(JLabel.CENTER);
      add(label5);

      text5 = new JTextField(10);
      text5.setHorizontalAlignment(JTextField.CENTER);
      add(text5);

      label6 = new JLabel("Year:");
      label6.setHorizontalAlignment(JLabel.CENTER);
      add(label6);

      JComboBox<String> Ybox = new JComboBox<String>(Year);
      add(Ybox);

      label7 = new JLabel("Schedule:");
      label7.setHorizontalAlignment(JLabel.CENTER);
      add(label7);

      JComboBox<String> SCbox = new JComboBox<String>(Sched);
      add(SCbox);

      label8 = new JLabel("Favorite Anime:");
      label8.setHorizontalAlignment(JLabel.CENTER);
      add(label8);

      text8 = new JTextField(10);
      text8.setHorizontalAlignment(JTextField.CENTER);
      add(text8);

        JButton Qbutton = new JButton("Quit");
        Qbutton.setBounds(50, 60, 80, 30);
        Qbutton.setHorizontalAlignment(JButton.LEFT);
        add(Qbutton);

        Qbutton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event){
            System.exit(0);
        }
        });

   }
}

Dani AI

Generated

A few concrete reasons the form never shows the expected output and how to fix them.

The class declares fields (for example Mbox, Dbox, Qbutton, the text* fields) but then re-declares local variables with the same names inside the constructor. That shadows the fields so the rest of the class still sees null instance variables — a common cause of no-op updates or NullPointerExceptions. The UI also uses setBounds while using FlowLayout, which setBounds will simply ignore. As posted by , there is no "Accept" button wired to assemble and display a formatted result.

As pointed out, the update must happen inside an ActionListener. Best practice: declare an Accept button and an output component (prefer JTextArea inside a JScrollPane for multiline) as instance fields and instantiate them once in the constructor (do not re-declare them locally). In the ActionListener read values with getText() for text fields and (String)combo.getSelectedItem() for combo boxes, validate inputs (parse age with try/catch), build a formatted string (use StringBuilder or String.format) and call outputField.setText(...). Mark the output component read-only with setEditable(false) if it is display-only.

A few UI/lifecycle notes: construct the GUI on the EDT (SwingUtilities.invokeLater), call pack() and setVisible(true) after building the frame, and use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) instead of relying only on System.exit listeners. Replace setBounds when using layout managers; for aligned label/field pairs consider GridBagLayout or nested JPanels with GridLayout.

On date handling, was correct — prefer JSpinner/SpinnerDateModel or java.time classes rather than parallel string arrays so invalid dates (e.g. 31 February) are prevented. Input validation (age numeric, non-empty required fields) completes a robust Accept action and prevents silent failures when calling setText.

Recommended Answers

All 2 Replies

It's just like the Quit button you already have, but in the ActionListener instead of the run() method just being system.exit(); itshould have the necessary code to set the text of the field you want to update.

  • use JSpinner, read Oracle tutorial for working code example instead of String[] Month and String[] Day, otherwise you will able to add funny Date, e.g. 31. february etc...

  • for output to use JTextArea, formatted output from all JTextFields, JSpinners, JComboBoxes (with combine in text from JLabels)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.