Last Name ______________________ First__________________
Last Name ______________________ First__________________ Date : 9/29/08
1. 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 year
private JLabel graduationYear; // shows the graduation year
public GraduationPanel()
{
}
private class GraduateListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
}
}
Practice– CSE 205 – Fall 2008
Answer
import 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);
}
}
}
Powered by TCPDF (www.tcpdf.org)