📄 cardfileabstract.java
字号:
/* * Copyright 1996 John Wiley & Sons, Inc. All Rights Reserved. Reproduction * or translation of this work beyond that permitted in Section 117 of the 1976 * United States Copyright Act without the express written permission of the * copyright owner is unlawful. Requests for further information should be * addressed to Permissions Department, John Wiley & Sons, Inc. The * purchaser may make back-up copies for his/her own use only and not for * distribution or resale. The Publisher assumes no responsibility for errors, * omissions, or damages, caused by the use of this software or from the use * of the information contained herein. * */import java.awt.*;import java.lang.*;import java.util.*;public abstract class CardFileAbstract extends Frame { // the myTextField objects for each column myTextField txt_name, txt_address, txt_city, txt_state, txt_zip, txt_country, txt_phone, txt_id; String formstate; // indicates whether we are adding or editing // String name, address, city, state, zip, country, phone; // a layout manager for the Frame GridBagLayout layoutmangler = new GridBagLayout(); // each of the buttons and a Panel to hold them Button next, previous, quit, save, newrow, edit, cancel, delete; Panel actionbuttons; // this hash is used to map column names to the // corresponding objects (the myTextFields) Hashtable columnmap = new Hashtable(); // the default constructor public CardFileAbstract(String[] argv) { login(argv); CreateForm(); } public void CreateForm() { // set the layout for the frame this.setLayout(layoutmangler); // this is the offset within the GridBagLayout. If // I want the next object on a different line, I // postincrement. If not, I don't. int i = 0; // the name field // // first, I add an anonymous text label to the Frame using // the AddToFrame method. Note that it uses column 0, and // row i (currently zero). I don't want the textfield // to appear on a different row, so I don't postincrement i. AddToFrame(new Label("Name:"), 0, i); // now, I instantiate txt_name. This is a textfield with 20 // spaces. Then, I add it to the frame, and insert a reference // to it in the columnmap, keyed by 'name', which is the name // of the column in the CardFile table txt_name = new myTextField(20); txt_name.setEditable(false); AddToFrame(txt_name, 1, i++); columnmap.put("name", txt_name); // the rest of the fields. Everything is pretty much the same as above. // // the address field AddToFrame(new Label("Address:"), 0, i); txt_address = new myTextField(35); txt_address.setEditable(false); AddToFrame(txt_address, 1, i++); columnmap.put("address", txt_address); // the city field // don't postincrement i, so state will show up // on the same row. AddToFrame(new Label("City:"), 0, i); txt_city = new myTextField(20); txt_city.setEditable(false); AddToFrame(txt_city, 1, i); columnmap.put("city", txt_city); // the state field AddToFrame(new Label("State:"), 2, i); txt_state = new myTextField(2); txt_state.setEditable(false); AddToFrame(txt_state, 3, i++); columnmap.put("state", txt_state); // the zip field AddToFrame(new Label("Zip:"), 0, i); txt_zip = new myTextField(11); txt_zip.setEditable(false); AddToFrame(txt_zip, 1, i++); columnmap.put("zip", txt_zip); // the country field AddToFrame(new Label("Country:"), 0, i); txt_country = new myTextField(25); txt_country.setEditable(false); AddToFrame(txt_country, 1, i++); columnmap.put("country", txt_country); // the phone field AddToFrame(new Label("Phone:"), 0, i); txt_phone = new myTextField(20); txt_phone.setEditable(false); AddToFrame(txt_phone, 1, i++); columnmap.put("phone", txt_phone); // the id field. AddToFrame(new Label("Id:"), 0, i); txt_id = new myTextField(4); txt_id.setEditable(false); AddToFrame(txt_id, 1, i++); columnmap.put("id", txt_id); // create the button panel and give it a new FlowLayout actionbuttons = new Panel(); actionbuttons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); // The constraints for the button panel are a little // more complex than the constraints for the labels // and textfields, so we'll set it up here, and add // the actionbuttons panel GridBagConstraints c = new GridBagConstraints(); c.gridwidth = 3; c.gridheight = 1; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.CENTER; c.weightx = 0.0; c.weighty = 0.0; c.gridx = 0; c.gridy = i; ((GridBagLayout)this.getLayout()).setConstraints(actionbuttons, c); this.add(actionbuttons); // instantiate and add each of the buttons previous = new Button("Previous"); actionbuttons.add(previous); next = new Button("Next"); actionbuttons.add(next); quit = new Button("Quit"); actionbuttons.add(quit); newrow = new Button("New"); actionbuttons.add(newrow); edit = new Button("Edit"); actionbuttons.add(edit); delete = new Button("Delete"); actionbuttons.add(delete); // save and cancel are disabled until the user // is adding or editing. save = new Button("Save"); actionbuttons.add(save); save.setEnabled(false); cancel = new Button("Cancel"); actionbuttons.add(cancel); cancel.setEnabled(false); // call getRow() to display the first row in the table. getRow(); System.out.println("CardFileAbstract was started successfully."); } public abstract void login(String[] argv); public abstract void getRow(); public abstract void delRow(); public abstract void nextRow(); public abstract void prevRow(); public abstract void save(); public abstract void update(); /** * clearForm() * * Clear all of the input fields * */ protected void clearForm () { // blank the name field txt_name.setText(""); // blank the address field txt_address.setText(""); // blank the city field txt_city.setText(""); // blank the state field txt_state.setText(""); // blank the zip field txt_zip.setText(""); // blank the country field txt_country.setText(""); // blank the phone field txt_phone.setText(""); // blank the id field txt_id.setText(""); } /** * AddToFrame() * * A convenience method to wrap the living hell * that is GridBagConstraints() * */ protected void AddToFrame (Component item, int x, int y) { // some sane layout defaults. GridBagConstraints c = new GridBagConstraints(); c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.NORTHWEST; c.weightx = 0.0; c.weighty = 0.0; // set the grid coordinates c.gridx = x; c.gridy = y; // set the constraints, and add the item to the layout ((GridBagLayout)this.getLayout()).setConstraints(item, c); this.add(item); } /** * handleEvent * * Deal with things the user did... */ public boolean handleEvent(Event event) { switch(event.id) { // deal with action buttons case Event.ACTION_EVENT: if (event.target == next) { nextRow(); } if (event.target == previous) { prevRow(); } if (event.target == delete) { delRow(); } // if the user pressed quit, then let's get out of here! if (event.target == quit) { System.exit(0); } // if the user wants to edit the current row, // then set the formstate as appropriate, and // call the setEdit() method. if (event.target == edit) { formstate = "edit"; setEdit(); } // in case the user presses the new button, I // want to 1) clear the form, 2) set the formstate // to "new", 3) let them modify the id field, and // 4) call setEdit() if (event.target == newrow) { clearForm(); formstate = "new"; txt_id.setEditable(true); setEdit(); } // in case the user hits the save button, I need // to distinguish between "new" and "edit" mode, // so I know whether to call save() or update(). // Also, I need if (event.target == save) { if (formstate.equals("new")) { save(); txt_id.setEditable(false); // make the id field uneditable } if (formstate.equals("edit")) { update(); } // set the formstate to "browse", and call setBrowse() formstate = "browse"; setBrowse(); } // if the user pressed cancel, return the formstate to browse if (event.target == cancel) { // if it was new, make sure that they can't edit the // id field... if (formstate.equals("new")) { txt_id.setEditable(false); } // return the formstate to browse, call getRow() // to retrieve the row they were looking at // before editing or adding, and call setBrowse() formstate = "browse"; getRow(); setBrowse(); } break; // in case they closed the window, then take // it as a sign that they want to quit case Event.WINDOW_DESTROY: System.exit(0); break; // hmmm... // just pass these on case Event.MOUSE_DOWN: case Event.MOUSE_UP: case Event.MOUSE_DRAG: case Event.KEY_PRESS: case Event.KEY_ACTION: case Event.KEY_RELEASE: case Event.KEY_ACTION_RELEASE: case Event.GOT_FOCUS: case Event.LOST_FOCUS: case Event.MOUSE_ENTER: case Event.MOUSE_EXIT: case Event.MOUSE_MOVE: return false; } return true; } /** * setEdit() * * prepare the form for editing/adding * */ protected void setEdit () { // disable all these buttons next.setEnabled(false); previous.setEnabled(false); newrow.setEnabled(false); edit.setEnabled(false); delete.setEnabled(false); // set everything except the id to be editable txt_name.setEditable(true); txt_address.setEditable(true); txt_city.setEditable(true); txt_state.setEditable(true); txt_zip.setEditable(true); txt_country.setEditable(true); txt_phone.setEditable(true); // enable these two buttons save.setEnabled(true); cancel.setEnabled(true); } /** * setBrowse() * * prepare the form for viewing * */ protected void setBrowse() { // enable all these buttons next.setEnabled(true); previous.setEnabled(true); newrow.setEnabled(true); edit.setEnabled(true); delete.setEnabled(true); // disable the fields txt_name.setEditable(false); txt_address.setEditable(false); txt_city.setEditable(false); txt_state.setEditable(false); txt_zip.setEditable(false); txt_country.setEditable(false); txt_phone.setEditable(false); txt_id.setEditable(false); // disable these two buttons save.setEnabled(false); cancel.setEnabled(false); }}class myTextField extends TextField { public myTextField(int size) { super( size ); } public String getText() { String retval = super.getText(); if (retval.equals("")) { return " "; } else { return retval; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -