/* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 10/1/2000 Description: This applet lays out buttons and labels to form a timetable. It is part of exercise 2 from the user interface design in Java course on: http://www.dcs.gla.ac.uk/~johnson/teaching/hci-java It uses the NewGridBagLayout class designed by G.W. Rouse in An Introduction to Data Structures and Algorithms with Java, Prentice Hall, Hemel Hemstead, 1998. pp 65-78. The details of this class will be introduced in later practicals - it provides a useful means of minimising the initial complexity in developing an AWT interface. */ import java.applet.*; // you should have met this before import java.net.*; import java.awt.*; // link to the AWT classes import java.awt.event.*; import NewGridBagLayout; // we will learn more about //layout managers in later practicals public class TimetableButtons extends Applet implements ActionListener{ // Panels are components that can contain other components, such as labels. // we will use a panel to contain the labels that make up the timetable. private Panel timetable = new Panel(); // The following lines provide access to a customised layout manager. // This provides a quick means of placing labels in particular // positions on the screen NewGridBagLayout layout = new NewGridBagLayout(); /* Note We now have to declare the interface objects outside of the init() method because we want to interact with the buttons later on... */ Button UID_slot1 = new Button("UID1"); // create new buttons Button UID_slot2 = new Button("UID2"); Button UID_tutorial = new Button("UID tutorial"); // We define sme string constants to help us identify when a button // is pressed. static final String UID1 = "uid1"; static final String UID2 = "uid2"; static final String UID_Tut = "uid_tutorial"; Label Time_0900 = new Label("09.00"); // create new labels Label Time_1000 = new Label("10.00"); Label Time_1100 = new Label("11.00"); Label Time_1200 = new Label("12.00"); Label Time_1300 = new Label("13.00"); Label Time_1400 = new Label("14.00"); Label Time_1500 = new Label("15.00"); Label Time_1600 = new Label("16.00"); Label Time_1700 = new Label("17.00"); Label Time_1800 = new Label("18.00"); Label Monday = new Label("Monday", Label.RIGHT); Label Tuesday = new Label("Tuesday", Label.RIGHT); Label Wednesday = new Label("Wednesday", Label.RIGHT); Label Thursday = new Label("Thursday", Label.RIGHT); Label Friday = new Label("Friday", Label.RIGHT); public void init() { timetable.setLayout(layout); // set up the layout manager /* These calls add the labels at appropriate places on the screen The first parameter is the name of the label component to be added eg Time_0900 The second parameter is the panel component that holds all of the label components. The third and fourth parameters sets the X and Y coordinates of the label. The fifth and sixth parameters sets width and height of the label */ layout.setPositionSizeAdd(Time_0900, timetable, 1, 1, 1, 1); layout.setPositionSizeAdd(Time_1000, timetable, 2, 1, 1, 1); layout.setPositionSizeAdd(Time_1100, timetable, 3, 1, 1, 1); layout.setPositionSizeAdd(Time_1200, timetable, 4, 1, 1, 1); layout.setPositionSizeAdd(Time_1300, timetable, 5, 1, 1, 1); layout.setPositionSizeAdd(Time_1400, timetable, 6, 1, 1, 1); layout.setPositionSizeAdd(Time_1500, timetable, 7, 1, 1, 1); layout.setPositionSizeAdd(Time_1600, timetable, 8, 1, 1, 1); layout.setPositionSizeAdd(Time_1700, timetable, 9, 1, 1, 1); layout.setPositionSizeAdd(Time_1800, timetable, 10, 1, 1, 1); layout.setPositionSizeAdd(Monday, timetable, 0, 3, 1, 1); layout.setPositionSizeAdd(Tuesday, timetable, 0, 4, 1, 1); layout.setPositionSizeAdd(Wednesday, timetable, 0, 5, 1, 1); layout.setPositionSizeAdd(Thursday, timetable, 0, 6, 1, 1); layout.setPositionSizeAdd(Friday, timetable, 0, 7, 1, 1); layout.setPositionSizeAdd(UID_slot1, timetable, 2, 4, 1, 1); layout.setPositionSizeAdd(UID_slot2, timetable, 2, 6, 1, 1); layout.setPositionSizeAdd(UID_tutorial, timetable, 8, 4, 3, 1); UID_slot1.setActionCommand(UID1); UID_slot2.setActionCommand(UID2); UID_tutorial.setActionCommand(UID_Tut); UID_slot1.addActionListener(this); UID_slot2.addActionListener(this); UID_tutorial.addActionListener(this); add(timetable); validate(); } public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); /* This implements an event handler to call the specified web page whenever a particular button is selected. If you add more buttons to the page then you will have to introduce further conditions in the if-statement. Just copy the lines up to the "end of UID if-case" but alter the URL to point to the relevant lecturer/course home page. */ if((command== UID1) || (command== UID2) || (command== UID_Tut)) { try { URL UID_home = new URL("http://www.dcs.gla.ac.uk/~johnson/teaching/hci-java"); getAppletContext().showDocument(UID_home); } catch (Exception e) { showStatus("Error " + e); } } // end of UID if-case } }