The objective for this assignment is to be able to construct working GUI components following specifications. These components will be used to construct your next project. I have test program listings at the bottom of this page which can do some superficial testing of your components to see if they are working. Please note that the tests are not thorough and your code may still have problems even if it manages to pass the tests. You are expected to test and debug your own code.
You have two major tasks for this assignment. The first is to create the following keypad GUI:
Here are the requirements for the keypad:
When you are done with your keypad, you can test it with the driver listed below. It should display the text from the buttons you click in the console window.
/* TestKeypad.java CIS 260 Calculator 1 Assignmnet */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestKeypad extends JFrame implements ActionListener { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new TestKeypad(); } }); } public TestKeypad() { init(); } public void init() { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { e.printStackTrace(System.err); } setDefaultCloseOperation(EXIT_ON_CLOSE); Keypad k = new Keypad(); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(k); k.addActionListener(this); setSize(220,200); setVisible(true); } public void actionPerformed(ActionEvent e) { final ActionEvent event = e; EventQueue.invokeLater(new Runnable() { public void run() { JButton b = (JButton) event.getSource(); System.out.println(b.getActionCommand()); } }); } }
The second part of this project is to create a text field which will display numbers. Think of it as a display for a calculator. The tricky part is to only allow valid numbers.
Here are the requirements for the number field:
When you are done with your number field, you can test it with the driver listed below. It should display testing information in the console window.
/* TestNumField.java CIS 260 Calculator 1 assignment */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestNumField extends JFrame { NumField nf; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new TestNumField(); } }); } public TestNumField() { init(); } public void init() { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { e.printStackTrace(System.err); } Container c = getContentPane(); c.setLayout(new BorderLayout()); nf = new NumField(); c.add(nf); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(200,60); setVisible(true); start(); } public void start() { System.out.println("Testing Number Field..."); nf.setValue(123.89); if (nf.getText().equals("123.89")) System.out.println("...passed setting value test"); else System.out.println("...failed setting value test"); double d = nf.getValue(); if (d == 123.89) System.out.println("...passed getting value test"); else System.out.println("...failed getting value test"); nf.append('1'); System.out.println(nf.getText()); System.out.println("The line above should read: 123.891"); nf.append(".7"); System.out.println(nf.getText()); System.out.println("The line above should read: 123.8917"); nf.clear(); System.out.println(nf.getText()); System.out.println("The line above should be blank"); nf.append("124.43"); System.out.println(nf.getText()); System.out.println("The line above should read: 124.43"); System.out.println("Your GUI should also read: 124.43"); } }
Your test output should look like this:
Testing Number Field... ...passed setting value test ...passed getting value test 123.891 The line above should read: 123.891 123.8917 The line above should read: 123.8917 The line above should be blank 124.43 The line above should read: 124.43 Your GUI should also read: 124.43