practice_GUIs - Practice CSE 205 Fall 2008 Last Name First...

This preview shows page 1 - 3 out of 3 pages.

Practice– CSE 205 – Fall 2008Last Name ______________________ First__________________ Last Name ______________________ First__________________ Date : 9/29/081. Complete this unfinished class such that each time the user enters the year they began college, the graduation year is shown in the lower right corner of the window (leave the user's input in the text field). Here is a before and after view of the graphical user interface when the user enters 2001 for the first time. Hint: Think about what is missing. import javax.swing.*;import java.awt.*;import java.awt.event.*;public class GUIExample{public static void main(String[] args){//create a frame object//Create a GraduationPanel and add it to the frame content pane}}class GraduationPanel extends JPanel{private JTextField year;//gets the yearprivate JLabel graduationYear; // shows the graduation yearpublic GraduationPanel(){ }private class GraduateListener implements ActionListener{public void actionPerformed (ActionEvent e){}}
}//end of class
Answerimport javax.swing.*;import java.awt.*;import java.awt.event.*;public class Quiz3{public static void main(String[] args){JFrame frame = new JFrame("When do I graduate?");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);GraduationPanel panel = new GraduationPanel();frame.getContentPane().add(panel);frame.pack();frame.setVisible(true);}}class GraduationPanel extends JPanel{private JTextField year;private JLabel graduationYear;public GraduationPanel(){ add(new JLabel("Enter college in"));year = new JTextField(5);add(year);year.addActionListener(new GraduateListener());add(new JLabel("Graduation Year"));graduationYear = new JLabel("????");add(graduationYear);setPreferredSize(new Dimension(210, 80));}private class GraduateListener implements ActionListener{public void actionPerformed (ActionEvent e){String text = year.getText();int year = Integer.parseInt(text) + 4;graduationYear.setText("" +year);}}}

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture