Select a Chapter: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Back to the Main Page | ||
Chapter Ten listings: 12 classes |
import java.awt.Graphics2D; public class Painter extends javax.swing.JFrame { public Painter() { super ("Wysiwyg Car Rentals"); // put the title on it addWindowListener (new Closer()); setSize (760, 600); // 760 pixels wide, 600 pixels tall setVisible (true); // make it visible to the user repaint(); // update and paint } //====================== /** Draw a pattern on the frame's drawing area. */ public void paint (java.awt.Graphics g) { Graphics2D page = (Graphics2D) g; page.drawString ("pattern", 10, 40); for (int depth = 40; depth < 580; depth += 5) { page.draw (new java.awt.geom.Line2D.Double (depth, depth, depth + 30, depth)); // horizontal } } //====================== }
class PainterApp { /** Create and initialize the JFrame. */ public static void main (String[ ] args) { new Painter(); } //====================== }
import java.awt.event.WindowListener; import java.awt.event.WindowEvent; public class Closer implements WindowListener { /** Enable the closer icon to terminate the program. */ public void windowClosing (WindowEvent ev) { System.exit (0); } //====================== public void windowActivated (WindowEvent ev) { } public void windowDeactivated (WindowEvent ev) { } public void windowIconified (WindowEvent ev) { } public void windowDeiconified (WindowEvent ev) { } public void windowOpened (WindowEvent ev) { } public void windowClosed (WindowEvent ev) { } }
class CarRentalApp { /** Create and initialize the JFrame. */ public static void main (String[ ] args) { new CarRentalView(); } //====================== } import javax.swing.*; import java.awt.event.*; public class Gooey extends JPanel { private JTextField itsInput = new JTextField (15); private JTextArea itsOutput; private String itsData = ""; public Gooey (int rows, int columns) { this.add (new JLabel ("entry:")); itsInput.addActionListener (new TextFieldAL()); this.add (itsInput); JButton button = new JButton ("delete first character"); button.addActionListener (new ButtonAL()); this.add (button); itsOutput = new JTextArea (rows, columns); this.add (new JScrollPane (itsOutput)); } //====================== private class TextFieldAL implements ActionListener { public void actionPerformed (ActionEvent ev) { itsData += itsInput.getText(); itsInput.setText (""); itsOutput.append (itsData + "\n"); } } //====================== private class ButtonAL implements ActionListener { public void actionPerformed (ActionEvent ev) { if ( ! itsData.equals ("")) itsData = itsData.substring (1); itsOutput.append (itsData + "\n"); } } //====================== }
class GooeyApp { public static void main (String[] args) { JOptionPane.showMessageDialog (null, new Gooey (8, 20)); System.exit (0); } //====================== }
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CarRentalView extends JFrame { public CarRentalView() { super ("Wysiwyg Car Rentals"); // put the title on it this.addWindowListener (new Closer()); // enable closing this.setSize (760, 600); // cover most of the screen this.init(); setVisible (true); // make it visible to the user } //====================== public void init() { Container content = this.getContentPane(); content.setLayout (null); content.add (subViewOne()); content.add (subViewTwo()); content.add (subViewThree()); content.add (subViewFour()); content.add (subViewFive()); } //====================== // CarRentalView class, part 2 public static final double PRICE_PER_DAY = 30.00; ///////////////////////////////// private JTextField itsDaysRented = new JTextField ("1"); private JLabel itsBaseCost = new JLabel ("" + PRICE_PER_DAY); private JPanel subViewOne() { JPanel panel = new JPanel(); panel.setBounds (10, 25, this.getWidth() - 20, 40); panel.add (new JLabel ("Enter days rented:")); this.itsDaysRented.addActionListener (new DaysRentedAL()); panel.add (this.itsDaysRented); panel.add (new JLabel ("The base cost is:")); panel.add (this.itsBaseCost); return panel; } //====================== private class DaysRentedAL implements ActionListener { public void actionPerformed (ActionEvent ev) { int d = Integer.parseInt (itsDaysRented.getText()); itsBaseCost.setText ("" + (d - d / 7) * PRICE_PER_DAY); } } //====================== // CarRentalView class, part 3 private CarRentalModel itsModel = new CarRentalModel(); private JTextField itsCustomerName = new JTextField (18); private JTextField itsCreditCard = new JTextField (12); private JPanel subViewTwo() { JPanel panel = new JPanel(); panel.setBounds (10, 75, this.getWidth() - 20, 40); JButton clearButton = new JButton ("Clear"); clearButton.setMnemonic ('c'); clearButton.addActionListener (new ClearButtonAL()); panel.add (clearButton); JButton submitButton = new JButton ("Submit"); submitButton.setMnemonic ('s'); submitButton.addActionListener (new SubmitButtonAL()); panel.add (submitButton); panel.add (new JLabel ("Customer name:")); panel.add (this.itsCustomerName); panel.add (new JLabel ("Credit card:")); panel.add (this.itsCreditCard); return panel; } //====================== private class ClearButtonAL implements ActionListener { public void actionPerformed (ActionEvent ev) { CarRentalView.this.itsCustomerName.setText(""); CarRentalView.this.itsCreditCard.setText(""); } } //====================== private class SubmitButtonAL implements ActionListener { public void actionPerformed (ActionEvent ev) { itsModel.add (itsCustomerName.getText() + " " + itsCreditCard.getText()); } } //====================== // CarRentalView class, part 4 private static String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; private static String[] days = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}; private static String[] hours = {"7am", "8am", "9am", "10am", "11am", "noon", "1pm", "2pm", "3pm", "4pm", "5pm"}; // CarRentalView class, part 5 private JComboBox itsStartMonth = new JComboBox (months); private JComboBox itsStartDay = new JComboBox (days); private JComboBox itsStartHour = new JComboBox (hours); private JComboBox itsEndMonth = new JComboBox (months); private JComboBox itsEndDay = new JComboBox (days); private JComboBox itsEndHour = new JComboBox (hours); public JPanel subViewThree() { JPanel panel = new JPanel(); panel.setBounds (10, 125, this.getWidth() - 20, 40); panel.add (new Label ("starting month/day/hour:")); itsStartMonth.addActionListener (new StartMonthAL()); panel.add (itsStartMonth); panel.add (itsStartDay); itsStartHour.addActionListener (new StartHourAL()); panel.add (itsStartHour); return panel; } //====================== public JPanel subViewFour() { JPanel panel = new JPanel(); panel.setBounds (10, 175, this.getWidth() - 20, 40); panel.add (new Label ("ending month/day/hour:")); panel.add (itsEndMonth); panel.add (itsEndDay); panel.add (itsEndHour); return panel; } //====================== private class StartMonthAL implements ActionListener { public void actionPerformed (ActionEvent ev) { itsEndMonth.setSelectedIndex (itsStartMonth.getSelectedIndex()); } } //====================== private class StartHourAL implements ActionListener { public void actionPerformed (ActionEvent ev) { itsEndHour.setSelectedIndex (itsStartHour.getSelectedIndex()); } } //====================== // CarRentalView class, part 6 (completed) private JCheckBox itsManual = new JCheckBox ("manual", false); private JCheckBox itsAir = new JCheckBox ("air cond", true); private JCheckBox itsCD = new JCheckBox ("CD player", true); private String itsVehicle = "compact"; // the default choice public JPanel subViewFive() { JPanel panel = new JPanel(); panel.setBounds (10, 225, this.getWidth() - 20, 40); panel.add (itsManual); panel.add (itsAir); panel.add (itsCD); panel.add (new JLabel ("Choose one:")); ButtonGroup group = new ButtonGroup(); ActionListener alis = new ButtonGroupAL(); String[] text = {"compact", "full-size", "luxury", "SUV"}; for (int k = 0; k < text.length; k++) { JRadioButton car = new JRadioButton (text[k]); car.addActionListener (alis); group.add (car); panel.add (car); } return panel; } //====================== private class ButtonGroupAL implements ActionListener { public void actionPerformed (ActionEvent ev) { itsVehicle = ((JRadioButton) ev.getSource()).getText(); } } //====================== }
public class CarRentalModel { public void add (String given) { System.out.println ("Model got " + given); } //======================= }
import javax.swing.*; public class AddInputs extends JApplet { private JSlider itsInput = new JSlider (JSlider.HORIZONTAL); private JTextArea itsOutput = new JTextArea (40, 20); private int itsData = 0; public void init() { setVisible (true); java.awt.Container content = this.getContentPane(); content.add (itsOutput); itsInput.addChangeListener (new SliderCL()); itsInput.setToolTipText ("numbers ranging 0 to 100"); content.add (itsInput); JButton show = new JButton ("show total so far"); show.addActionListener (new ButtonAL()); content.add (show); } //====================== private class ButtonAL implements java.awt.event.ActionListener { public void actionPerformed (java.awt.event.ActionEvent ev) { itsOutput.append ("\nTotal so far is " + itsData); } } //====================== private class SliderCL implements javax.swing.event.ChangeListener { public void stateChanged (javax.swing.event.ChangeEvent ev) { itsOutput.append ("\nEntry: " + itsInput.getValue()); itsData += itsInput.getValue(); } } //====================== }
import javax.swing.*; import java.awt.event.*; public class Clock extends JLabel { private Timer itsTimer; private int itsMillis; // milliseconds per activation private int itsCounter; // milliseconds since it was started public Clock (int millis) { super ("0"); // create a JLabel saying "0" itsMillis = millis; itsTimer = new Timer (millis, new TimerAL()); } //====================== public void start() { itsCounter = 0; itsTimer.start(); } //====================== public void stop() { itsTimer.stop(); } //====================== private class TimerAL implements java.awt.event.ActionListener { public void actionPerformed (java.awt.event.ActionEvent ev) { Clock.this.itsCounter += itsMillis; Clock.this.setText("" + (Clock.this.itsCounter / 1000)); } } //====================== }
import java.awt.*; import javax.swing.*; public class Painter extends JFrame { private Timer itsTimer = new Timer (100, new TimerAL()); public Painter() { super ("Wysiwyg Car Rentals"); addWindowListener (new Closer()); setSize (760, 600); // 760 pixels wide, 600 pixels tall setVisible (true); paint (getGraphics()); } //====================== public void paint (Graphics page) { page.drawString ("pattern", 10, 40); itsTimer.start(); } //======================= private class TimerAL implements java.awt.event.ActionListener { private int depth = 40; public void actionPerformed (java.awt.event.ActionEvent ev) { Graphics2D page = (Graphics2D) getGraphics(); page.draw (new java.awt.geom.Line2D.Double (depth, depth, depth + 30, depth)); // horizontal depth += 5; if (depth >= 580) itsTimer.stop(); } } //======================= }
class Holder { private JLabel itsSound; private JLabel lab; private class MenuItemAL implements ActionListener { public void actionPerformed (ActionEvent ev) { String choice = ((JMenuItem) ev.getSource()).getText(); if (choice.equals ("dog")) itsSound.setText ("woof"); else if (choice.equals ("cat")) itsSound.setText ("meow"); else if (choice.equals ("fish")) itsSound.setText ("burble"); else if (choice.equals ("apple")) itsSound.setText ("red"); else if (choice.equals ("lemon")) itsSound.setText ("yellow"); else // unrecognized itsSound.setText ("what?"); } } //======================= private class ClickML implements MouseListener { public void mouseEntered (MouseEvent ev) { lab.setText ("enter " + ev.getX() + "," + ev.getY()); } public void mouseExited (MouseEvent ev) { lab.setText ("exit " + ev.getX() + "," + ev.getY()); } public void mousePressed (MouseEvent ev) { lab.setText ("press " + ev.getX() + "," + ev.getY()); } public void mouseReleased (MouseEvent ev) { lab.setText ("release " + ev.getX() + "," + ev.getY()); } public void mouseClicked (MouseEvent ev) { lab.setText ("click " + ev.getX() + "," + ev.getY()); } } //======================= } |
Layout and Design Copyright © Psumonix, LLC. All Rights Reserved. |