package example1; import java.awt.Dimension; import javax.swing.*; import java.util.*; import java.awt.event.*; public class Example1 extends JPanel { private static final int FRAME_WIDTH = 800; private static final int FRAME_HEIGHT = 500; private static final int XCOORD = 10; private static final int YCOORD = 10; private static final Dimension buttonDimensions = new Dimension(200, 30); private JFrame frame; private String title = "Example1"; private Box mainBox; private JScrollPane outputScrollPane; private JTextArea outputTextArea; public Example1() { frame = new JFrame(); frame.setContentPane(this); frame.setLocation(XCOORD, YCOORD); frame.setSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT)); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setTitle(title); /* Creating main box to hold other boxes */ mainBox = Box.createVerticalBox(); /* Adding the box to the JPanel */ add(mainBox); /* Adding a Panel to the mainBox */ mainBox.add(new TopPanel()); /* Creating an area for output */ outputTextArea = new JTextArea(); outputTextArea.setEditable(true); /* Adding scrollbar capabilities to output area */ outputScrollPane = new JScrollPane(outputTextArea); /* Setting the size of the scroll area */ /* NOTE: Scroll bars will appear after typing several lines */ outputScrollPane.setPreferredSize(new Dimension((int)(.50*FRAME_WIDTH), (int)(.50*FRAME_HEIGHT))); /* Adding output area to main Box */ mainBox.add(outputScrollPane); /* Making the frame visible */ frame.setVisible(true); } private class TopPanel extends JPanel { private Box horizontalButtonFormBox, verticalButtonBox; private JPanel actualFormDisplayed; public TopPanel() { /* This panel will have two main areas: to the left a set of buttons */ /* to decide whether we add a student or faculty, and to the right the form */ /* where users can input the data. Data will be read once the submit */ /* button is selected (there is no need to press enter for each field) */ /* Creating the main horizontal box for this top panel */ add(horizontalButtonFormBox = Box.createHorizontalBox()); /* Creating and initializing vertical box with buttons */ verticalButtonBox = Box.createVerticalBox(); /* Creating the buttons and adding them to the vertical box */ JButton addStudentButton = new JButton("AddStudent"); JButton addFacultyButton = new JButton("AddFaculty"); verticalButtonBox.add(addStudentButton); /* The next line adds some empty space (10 pixels) */ /* You can add horizontal space using createHorizontalStrut */ verticalButtonBox.add(Box.createVerticalStrut(10)); verticalButtonBox.add(addFacultyButton); /* Adding vertical box to horizontal box */ horizontalButtonFormBox.add(verticalButtonBox); /* By default the student form will appear */ actualFormDisplayed = new StudentForm(); /* Adding the actual form to the appropriate box */ horizontalButtonFormBox.add(actualFormDisplayed); /* Lets give some functionality to the buttons */ /* When user press addStudent button the add student form */ /* will be displayed */ addStudentButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { /* To change the form we remove the previous form */ /* and add the new one */ horizontalButtonFormBox.remove(actualFormDisplayed); actualFormDisplayed = new StudentForm(); horizontalButtonFormBox.add(actualFormDisplayed); /* Important: because we change the structure of the */ /* GUI we need to update it. */ updateUI(); } }); /* When user press addFaculty button the add student form */ /* will be displayed */ addFacultyButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { /* To change the form we remove the previous form */ /* and add the new one */ horizontalButtonFormBox.remove(actualFormDisplayed); actualFormDisplayed = new FacultyForm(); horizontalButtonFormBox.add(actualFormDisplayed); /* Important: because we change the structure of the */ /* GUI we need to update it. */ updateUI(); } }); } private class StudentForm extends JPanel { private JTextField name, id; private JButton submitButton; private JLabel nameLabel, idLabel; public StudentForm() { nameLabel = new JLabel("Student Name: "); idLabel = new JLabel("Student Id: "); int nameFieldLength = 10; int idFieldLength = 4; name = new JTextField(nameFieldLength); id = new JTextField(idFieldLength); submitButton = new JButton("SubmitInfo"); /* Changing the size of the submit button */ submitButton.setPreferredSize(buttonDimensions); /* Adding to this panel */ add(nameLabel); add(name); add(idLabel); add(id); add(submitButton); /* When the user presses the submit button they will get */ /* information displayed on the output area */ submitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /* Notice how we are able to get the data from the fields */ /* even though users did not press enter for those fields */ String results; results = "Student Name Provided is: " + name.getText() + "\n"; results += "Student Id Provided is: " + id.getText() + "\n"; /* Notice that to write I use the outputTextArea */ outputTextArea.setText(results); } }); } } private class FacultyForm extends JPanel { private JTextField name, age; private JButton submitButton; private JLabel nameLabel, ageLabel; public FacultyForm() { nameLabel = new JLabel("Faculty Name: "); ageLabel = new JLabel("Faculty Age: "); int nameFieldLength = 10; int ageFieldLength = 4; name = new JTextField(nameFieldLength); age = new JTextField(ageFieldLength); submitButton = new JButton("SubmitInfo"); /* Adding to this panel */ add(nameLabel); add(name); add(ageLabel); add(age); add(submitButton); /* When the user presses the submit button they will get */ /* information displayed on the output area */ submitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /* Notice how we are able to get the data from the fields */ /* even though users did not press enter for those fields */ String results; results = "Faculty Name Provided is: " + name.getText() + "\n"; results += "Faculty Age Provided is: " + age.getText() + "\n"; /* Notice that to write I use the outputTextArea */ outputTextArea.setText(results); } }); } } } /* Static block for feel and look */ /* Comment this block out and see how the feel and look changes */ /* You can also try this experiment in a Mac */ static { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { new Example1(); } }