import java.awt.*; import java.applet.*; import java.awt.event.*; // Requerido para controles de Interfaz con usuario /** * This class reads PARAM tags from its HTML host page and sets * the color and label properties of the applet. Program execution * begins with the init() method. */ // Lucio Gayosso 07/15/2000. Este programa muestra los diferentes controles de interfaz disponibles en Java // mediante el metodo de extension de Clase public class UICAplt extends Applet { /** * The entry point for the applet. */ String m_strDisplay="No 'Button' controls have been pressed."; String m_strDisplay2="No events on 'Checkboxes' controls"; // Texto para 'checkboxes' String m_strDisplay3="No choice selected on 'Choice' control"; // Texto para controles 'Choice' String m_strDisplay4="No items have been selected on 'List' control"; // Texto para el control tipo 'Lista' String m_strDisplay5="No event has occured on the 'TextField' control";// Texto para el control tipo 'Texto' String m_strDisplay6="No event has occured on the 'TextArea' control";// Texto para el control tipo 'Area de Texto' MyButton m_Button1=new MyButton("First Button"); MyButton m_Button2=new MyButton("Second Button"); MyButton m_Button3=new MyButton("Third Button"); MyCheckbox m_Checkbox1 = new MyCheckbox("One"); MyCheckbox m_Checkbox2 = new MyCheckbox("Two"); MyCheckbox m_Checkbox3 = new MyCheckbox("Three"); MyCheckbox m_Checkbox4 = new MyCheckbox("Four"); MyCheckbox m_Checkbox5 = new MyCheckbox("Five"); MyCheckbox m_Checkbox6 = new MyCheckbox("Six"); MyChoice m_Choice = new MyChoice(); MyList m_List = new MyList(5,false); MyTextField tf = new MyTextField("This is the TextField control! Type in and see the messages below"); MyTextArea ta = new MyTextArea("This is the TextArea control! Type in and see the messages below"); public void init() { //initForm(); // Se necesitan eliminar estas dos lineas para poder //usePageParams(); // desplegar los botones // TODO: Add any constructor code after initForm call. //Utilizacion del control 'Label' Label label1, label2, label3, label4; label1 = new Label(" Java's User Interface Controls. ",Label.CENTER); label2 = new Label(" (by Lucio Gayosso). ",Label.CENTER); Font font = new Font("Helvetica", Font.BOLD,14); label1.setFont(font); label2.setFont(font); add(label1); add(label2); //Adicion de los demas controles en el Applet add(m_Button1); add(m_Button2); add(m_Button3); add(m_Checkbox1); add(m_Checkbox2); add(m_Checkbox3); add(m_Checkbox4); add(m_Checkbox5); add(m_Checkbox6); m_Choice.addItem("Selection One"); // Agrega el elemento 'One' al control m_Choice m_Choice.addItem("Selection Two"); m_Choice.addItem("Selection Three"); add(m_Choice); m_List.addItem(" List Selection One"); // Agrega el elemento "List Selection One" al control de tipo lista m_List.addItem(" List Selection Two"); m_List.addItem(" List Selection Three"); add(m_List); add(tf); add(ta); } public void paint(Graphics g) { g.drawString(m_strDisplay,20,360); g.drawString(m_strDisplay2,20,375); g.drawString(m_strDisplay3,20,390); g.drawString(m_strDisplay4,20,405); g.drawString(m_strDisplay5,20,420); g.drawString(m_strDisplay6,20,435); } public class MyButton extends Button { String m_strText; MyButton(String s) { super(s); enableEvents(AWTEvent.ACTION_EVENT_MASK); m_strText=s; } public void processActionEvent(ActionEvent ae) { m_strDisplay="The '"+m_strText+"' was pressed"; getParent().repaint(); super.processActionEvent(ae); } } public class MyCheckbox extends Checkbox { String m_strText2; MyCheckbox(String s) { super(s); m_strText2=s; enableEvents(AWTEvent.ITEM_EVENT_MASK); } public void processItemEvent(ItemEvent ie) { m_strDisplay2="Checkbox '"+m_strText2+"' was just pressed."; getParent().repaint(); super.processItemEvent(ie); } } public class MyChoice extends Choice { MyChoice() { super(); enableEvents(AWTEvent.ITEM_EVENT_MASK); } public void processItemEvent(ItemEvent ie) { m_strDisplay3="Choice '"+getSelectedItem()+"' was selected."; getParent().repaint(); } } public class MyList extends List { MyList() { super(); enableEvents(AWTEvent.ITEM_EVENT_MASK); } MyList(int nLines, boolean bMultiselect) { super(nLines, bMultiselect); enableEvents(AWTEvent.ITEM_EVENT_MASK); } public void processItemEvent(ItemEvent ie) { int nStateChange = ie.getStateChange(); if(nStateChange == ItemEvent.SELECTED) { m_strDisplay4 = "'"+ getSelectedItem() + "' was selected."; getParent().repaint(); } } } public class MyTextField extends TextField { MyTextField(String s) { super(s); enableEvents(AWTEvent.ACTION_EVENT_MASK |AWTEvent.FOCUS_EVENT_MASK |AWTEvent.KEY_EVENT_MASK ); } public void processKeyEvent(KeyEvent ke) { if (ke.getID()==ke.KEY_RELEASED) { m_strDisplay5="A 'Key Event' ocurred on the 'TextField' control. The key released was " + ke.getKeyChar() + "."; getParent().repaint(); } super.processKeyEvent(ke); } public void processActionEvent(ActionEvent ae) { m_strDisplay5="An 'Action Event' ocurred."; getParent().repaint(); super.processActionEvent(ae); } public void processFocusEvent(FocusEvent fe) { if (fe.getID()==fe.FOCUS_GAINED) { m_strDisplay5="The 'Focus' was gained on the 'TextField' control"; getParent().repaint(); } else if(fe.getID()==fe.FOCUS_LOST) { m_strDisplay5="The 'Focus' was lost on the 'TextField' control"; getParent().repaint(); } super.processFocusEvent(fe); } } public class MyTextArea extends TextArea { MyTextArea(String s) { super(s); enableEvents(AWTEvent.FOCUS_EVENT_MASK |AWTEvent.KEY_EVENT_MASK ); } public void processKeyEvent(KeyEvent ke) { if (ke.getID()==ke.KEY_RELEASED) { m_strDisplay6="A 'Key Event' ocurred on the 'TextArea' control. The key released was " + ke.getKeyChar() + "."; getParent().repaint(); } super.processKeyEvent(ke); } public void processFocusEvent(FocusEvent fe) { if (fe.getID()==fe.FOCUS_GAINED) { m_strDisplay6="The 'Focus' was gained on the 'TextArea' control"; getParent().repaint(); } else if(fe.getID()==fe.FOCUS_LOST) { m_strDisplay6="The 'Focus' was lost on the 'TextArea' control"; getParent().repaint(); } super.processFocusEvent(fe); } } private final String labelParam = "label"; private final String backgroundParam = "background"; private final String foregroundParam = "foreground"; /** * Reads parameters from the applet's HTML host and sets applet * properties. */ private void usePageParams() { final String defaultLabel = "Default label"; final String defaultBackground = "C0C0C0"; final String defaultForeground = "000000"; String labelValue; String backgroundValue; String foregroundValue; /** * Read the , * , * and tags from * the applet's HTML host. */ labelValue = getParameter(labelParam); backgroundValue = getParameter(backgroundParam); foregroundValue = getParameter(foregroundParam); if ((labelValue == null) || (backgroundValue == null) || (foregroundValue == null)) { /** * There was something wrong with the HTML host tags. * Generate default values. */ labelValue = defaultLabel; backgroundValue = defaultBackground; foregroundValue = defaultForeground; } /** * Set the applet's string label, background color, and * foreground colors. */ label1.setText(labelValue); label1.setBackground(stringToColor(backgroundValue)); label1.setForeground(stringToColor(foregroundValue)); this.setBackground(stringToColor(backgroundValue)); this.setForeground(stringToColor(foregroundValue)); } /** * Converts a string formatted as "rrggbb" to an awt.Color object */ private Color stringToColor(String paramValue) { int red; int green; int blue; red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue(); green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue(); blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue(); return new Color(red,green,blue); } /** * External interface used by design tools to show properties of an applet. */ public String[][] getParameterInfo() { String[][] info = { { labelParam, "String", "Label string to be displayed" }, { backgroundParam, "String", "Background color, format \"rrggbb\"" }, { foregroundParam, "String", "Foreground color, format \"rrggbb\"" }, }; return info; } Label label1 = new Label(); /** * Intializes values for the applet and its components */ void initForm() { this.setBackground(Color.lightGray); this.setForeground(Color.black); label1.setText("label1"); this.setLayout(new BorderLayout()); this.add("North",label1); } }