/* * JDK 1.1.5 */ import java.awt.*; import java.awt.event.*; public class EventCode implements ActionListener, ItemListener{ Panel cards; TextField tfield = new TextField("Enter name:", 20); TextField name = new TextField("Name:", 25); TextField addr = new TextField("Address:", 25); Button cancel = new Button("Cancel"); Button OK = new Button("OK"); Button quit = new Button("Quit"); Button reset = new Button("Reset"); Button two = new Button("2 cols."); Button three = new Button("3 cols."); Button four = new Button("4 cols."); LayoutExample window; public EventCode(LayoutExample window) { System.out.println("EventCode constructor"); this.window=window; window.border.addActionListener(this); window.flow.addActionListener(this); window.card.addActionListener(this); window.grid.addActionListener(this); window.gridBag.addActionListener(this); OK.addActionListener(this); cancel.addActionListener(this); name.addActionListener(this); addr.addActionListener(this); window.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent event){ System.exit(0); } }); } public void actionPerformed(ActionEvent event){ Object source = event.getSource(); if( source == OK){ window.p2.removeAll(); window.p2.setLayout(new FlowLayout()); window.p2.add(new Label("<-- Select a layout to view ...")); window.p2.validate(); } if( source == cancel){ window.p2.removeAll(); gridBag(); window.p2.validate(); } if(source == two){ window.p2.removeAll(); window.p2.setLayout(new GridLayout(0,2)); window.p2.add(two); window.p2.add(three); window.p2.add(four); window.p2.add(OK); window.p2.validate(); } if( source == three){ window.p2.removeAll(); window.p2.setLayout(new GridLayout(0,3)); window.p2.add(two); window.p2.add(three); window.p2.add(four); window.p2.add(OK); window.p2.validate(); } if( source == four){ window.p2.removeAll(); window.p2.setLayout(new GridLayout(0,4)); window.p2.add(two); window.p2.add(three); window.p2.add(four); window.p2.add(OK); window.p2.validate(); } if( source == tfield){ window.p2.removeAll(); window.p2.setLayout(new FlowLayout()); window.p2.add(new Label("<-- Select a layout to view ...")); window.p2.validate(); } if( source == window.border){ System.out.println("Border"); window.p2.removeAll(); window.p2.setLayout(new BorderLayout()); Label dialogLabel = new Label(); dialogLabel.setAlignment(Label.CENTER); dialogLabel.setText("Border Layout"); List items = new List(6, true); items.add("Apple tree"); items.add("Orange tree"); items.add("Pear tree"); items.add("Peach tree"); items.add("Cherry tree"); items.add("Apricot tree"); window.p2.add("North", dialogLabel); window.p2.add("South", OK); window.p2.add("Center", items); window.p2.validate(); } if( source == window.flow){ window.p2.removeAll(); window.p2.setLayout(new FlowLayout()); window.p2.add(new Label("Flow Layout")); window.p2.add(new Button("An extremely long name")); window.p2.add(new Button("Short")); window.p2.add(new Button("Medium Length")); window.p2.add(OK); window.p2.add(new Scrollbar(Scrollbar.HORIZONTAL)); window.p2.validate(); } if( source == window.card){ Panel p3 = new Panel(); Panel p4 = new Panel(); Panel c2 = new Panel(); Choice c = new Choice(); Label cardLabel = new Label("Card Layout"); cards = new Panel(); window.p2.removeAll(); String BUTTONPANEL = "Show panel with Buttons"; String TEXTPANEL = "Show panel with TextField"; window.p2.setLayout(new BorderLayout()); c.add(BUTTONPANEL); c.add(TEXTPANEL); c2.add(c); cardLabel.setAlignment(Label.CENTER); window.p2.add("North", cardLabel); window.p2.add("Center", c2); cards.setLayout(new CardLayout()); p3.add(OK); p3.add(quit); p3.add(reset); p4.add(tfield); cards.add(BUTTONPANEL, p3); cards.add(TEXTPANEL, p4); window.p2.add("South", cards); c.addItemListener(this); tfield.addActionListener(this); window.p2.validate(); } if( source == window.grid){ window.p2.removeAll(); window.p2.setLayout(new GridLayout(5,1)); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); window.p2.add(new Label("Grid Layout 1 col.")); window.p2.add(two); window.p2.add(three); window.p2.add(four); window.p2.add(OK); window.p2.validate(); } if( source == window.gridBag){ window.p2.removeAll(); gridBag(); window.p2.validate(); } } public void itemStateChanged(ItemEvent event) { CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, (String)event.getItem()); } public void gridBag(){ System.out.println("gridBag"); GridBagConstraints c = new GridBagConstraints(); GridBagLayout gridbag = new GridBagLayout(); window.p2.setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.gridwidth = GridBagConstraints.REMAINDER; Label top = new Label("GridBag Layout"); top.setAlignment(Label.CENTER); gridbag.setConstraints(top, c); window.p2.add(top); c.gridwidth = GridBagConstraints.RELATIVE; gridbag.setConstraints(name, c); window.p2.add(name); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(addr, c); window.p2.add(addr); TextArea comments = new TextArea(3, 25); comments.setEditable(true); comments.setText("Comments:"); gridbag.setConstraints(comments, c); window.p2.add(comments); c.insets = new Insets(15, 0, 0, 0); c.fill=GridBagConstraints.NONE; c.gridwidth = GridBagConstraints.RELATIVE; gridbag.setConstraints(OK, c); window.p2.add(OK); c.gridheight=1; gridbag.setConstraints(cancel, c); window.p2.add(cancel); } }