|
Previous Next
Part A - Wake Up - Build the Look
In the first step, we will build the GUI interface. We've already said that our outermost panel will be divided into 3 other panels, which will be further divided.
dtTimePane - this top panel is built from the class DteTimePanel. It uses the paint method to build this portion of the screen. It sets the font and the foreground color for the type. It will display the current date and time.
panSetAlarmTime - this middle panel will be divided in 2 rows and 2 columns.
Left Top
- panWakeUpTime - wake up time for the alarm to go off.
Right Top
- panButtons - Find button (go to radio locator site)
- Play button (play current radio)
- Start button (start alarm process).
Left Bottom
- txtSound - sound to play when the alarm goes off.
We put in train.wav, but siren.wav may be
more effective for getting your attention.
Right Bottom
- txtSrchRadio - website for locating a radio station.
panBottom - this bottom panel is divided into 2 rows. The first row will contain a drop-down box with a list of radio stations to pick. The second row will contain an area to display status information.
The following is a listing of what this code might look like.
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class WakeUp extends JApplet {
DteTimePanel dtTimePane = new DteTimePanel();
JLabel lblWakeUpTime = new JLabel("Wake Up Time");
JTextField txtWakeUpTime = new JTextField("20:33", 5);
JTextField txtSound = new JTextField("train.wav");
JTextField txtSrchRadio =
new JTextField("http://www.radio-locator.com/");
JComboBox cboxPickRadio = new JComboBox();
JButton btnStartStop = new JButton("Start");
JButton btnSrchRadio = new JButton("Find");
JButton btnPlayRadio = new JButton("Play");
JTextField txtStatusInfo = new JTextField("Status information");
public void init() {
JPanel panOuter = new JPanel();
GridLayout gridlay1 = new GridLayout(3, 1);
panOuter.setLayout(gridlay1);
panOuter.add(dtTimePane);
JPanel panWakeUpTime = new JPanel();
FlowLayout flow1 = new FlowLayout(FlowLayout.LEFT);
panWakeUpTime.setLayout(flow1);
panWakeUpTime.add(lblWakeUpTime);
panWakeUpTime.add(txtWakeUpTime);
JPanel panButtons = new JPanel();
GridLayout gridlay2 = new GridLayout(1, 3);
panButtons.setLayout(gridlay2);
panButtons.add(btnSrchRadio);
panButtons.add(btnPlayRadio);
panButtons.add(btnStartStop);
JPanel panSetAlarmTime = new JPanel();
GridLayout gridlay3 = new GridLayout(2, 2);
panSetAlarmTime.setLayout(gridlay3);
panSetAlarmTime.add(panWakeUpTime);
panSetAlarmTime.add(panButtons);
panSetAlarmTime.add(txtSound);
panSetAlarmTime.add(txtSrchRadio);
panOuter.add(panSetAlarmTime);
cboxPickRadio.addItem("radio station 1");
cboxPickRadio.addItem("radio station 2");
cboxPickRadio.addItem("radio station 3");
cboxPickRadio.setEditable(true);
JPanel panBottom = new JPanel();
GridLayout gridlay4 = new GridLayout(2, 1);
panBottom.setLayout(gridlay4);
panBottom.add(cboxPickRadio);
panBottom.add(txtStatusInfo);
panOuter.add(panBottom);
setContentPane(panOuter);
repaint();
}
}
class DteTimePanel extends JPanel {
public void paint(Graphics screen) {
Color fgColor = new Color(40, 120, 160);
Font type = new Font("Monospaced", Font.BOLD, 20);
screen.setFont(type);
screen.setColor(fgColor);
screen.drawString("DDD MMM 31 99:99:99 EDT 2008", 5,15);
repaint();
}
}
Of course, since this is an applet, we will need HTML code to display it.
<html>
<head>
<title>Wake Up Applet</title>
</head>
<body bgcolor="#00ccaa">
<h2>An applet to wake up by:</h2>
<applet code="WakeUp.class" height="200" width="500">
This program requires a Java-enabled browser.
</applet>
</body>
</html>
Previous Next
|