Java Reference
In-Depth Information
System.out.println(e.getActionCommand()
!
" action performed");
if
(lastInputField!
#
null
){
String text
#
lastInputField.getText();
if
(cmd.equals("C")){
if
(text.length()>0){
lastInputField.setText(text.
substring(0,text.length()-1));
}
}
else
{
lastInputField.setText(text
!
cmd);
}
lastInputField.requestFocus();
}
}
}
The
BarCodeListener
interface is quite simple; it contains one method that
is called when a new bar code is read.
package
CounterUI;
public interface
BarCodeListener {
void
readValue(BarCodeReaderEvent event);
}
The information relating to a new bar code read is stored in
BarCode
ReaderEvent
objects. In our case the only information is represented by the
code.
package
CounterUI;
public class
BarCodeReaderEvent {
String code;
BarCodeReaderEvent(String code){
this
.code
#
code;
}
String getCode() {
return
code; }
}
The simulated bar code reader is implemented as a frame:
BarCode
ReaderUI
. Each bar code is an image stored on the hard disk and used as an
icon for a button. When a button is pressed the listener is notified that the
corresponding code has been read.
package
CounterUI;
import
java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;
public class
BarCodeReaderUI
extends
JFrame {
private
JButton btProduct1
#
new
JButton();