/**
**************************************************************************************************************************
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
public class MyfileJan extends JFrame implements ActionListener
{
//Build the JLabels and JTextFields, and the JComboBox
JLabel codeJL=new JLabel("Code #:");
JComboBox codeJC=new JComboBox();
JLabel nameJL=new JLabel("Description:");
JTextField nameJT=new JTextField(12);
JLabel priceJL=new JLabel("Cost:");
JTextField priceJT= new JTextField(12);
JButton enterJB=new JButton("Enter");
JButton totalJB=new JButton("Total");
JLabel subtotalJL=new JLabel("Subtotal:");
JTextField subtotalJT=new JTextField(12);
JLabel taxJL=new JLabel("Tax:");
JTextField taxJT=new JTextField(12);
JLabel totalJL=new JLabel("Total:");
JTextField totalJT= new JTextField(12);
JButton showRcptJB = new JButton("Show Receipt");
JTextPane pane=new JTextPane();
//private subTotal will store a running total
private double subTotal=0;
NumberFormat myFormatter= NumberFormat.getCurrencyInstance(Locale.US);//create a decimal format object
//construct an instance of MyfileJan
public MyfileJan()
{
;//puts a label on the gui
}
//create 3 arrays to hold the values of the items in stock, These arrays will store the receipt items and data
String[] codeArray= {"105","110","120","160","200"};
String[] nameArray= {"doc1","doc2","doc3","doc4","doc5"};
String[] priceArray={"100.00","200.00","300.00","400.00","500.00"};
String[] codeRcptArray={"________________"};
String[] nameRcptArray={"________________"};
String[] priceRcptArray={"________________"};
}
//add items to editable text, and add an actionListener
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
public class SimpleWindow implements ActionListener {
JFrame f;
JPanel p;
JLabel 1itemCode, 1itemCode, 1itemCode, 1itemCode, 1itemCode;
JTextField t105, t110, t120, t160, t200;
JButton b1, b2;
public SimpleWindow() {
f = new JFrame("");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
p.setLayout(new GridLayout(4,2));
litemCode = new JLabel("105");
1itemCode = new JLabel("110");
litemCode = new JLabel("120");
litemCode = new JLabel("160");
1itemCode = new JLabel("200");
tdoc1 = new JTextField(15);
tdoc2 = new JTextField(5);
tdoc3 = new JTextField(20);
tdoc4 = new JTextField(15);
tdoc5 = new JTextField(5);
b1 = new JButton("OK");
b1.addActionListener(this);
b2 = new JButton("Clear");
b2.addActionListener(this);
f.getContentPane().add(p);
f.setSize(600, 200);
f.setVisible(true);
}
public static void main(String args[]) {
SimpleWindow w = new SimpleWindow();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
String name = tName.getText();
String age = tAge.getText();
String address = tAddress.getText();
String str = "Name: "+name+"\nAge: "+age+"\nAddress: "+address;
JOptionPane.showMessageDialog(f,str);
}
if (e.getSource() == b2) {
tName.setText("");
tAge.setText("");
tAddress.setText("");
}
}
}
//build contentPane
public Container buildPane()
{
// add items to the combo box, and add an actionListener
codeJC.addItem("105");
codeJC.addItem("110");
codeJC.addItem("120");
codeJC.addItem("160");
codeJC.addItem("200");
codeJC.addActionListener(this);
codeJC.setToolTipText("select an item, then press enter to display description and cost");
//build the firstJPanel
JPanel firstJPanel = new JPanel();
firstJPanel.add(codeJL);
firstJPanel.add(codeJC);
firstJPanel.add(nameJL);
firstJPanel.add(nameJT);
firstJPanel.add(priceJL);
firstJPanel.add(priceJT);
nameJT.setEditable(false);
priceJT.setEditable(false);
firstJPanel.setLayout(new GridLayout(3,2,4,4));
//build the middlePanel
JPanel middlePanel=new JPanel();
//create the JTextPlane for the middlePanel
setTabsAndStyles(pane);
JScrollPane sp=new JScrollPane(pane);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setPreferredSize(new Dimension(550, 250));
middlePanel.add(sp);//add the scroll pane to the middlePanel
//add the enter, total, and show receipt buttons to the middlePanel
middlePanel.add(enterJB);
middlePanel.add(totalJB);
middlePanel.add(showRcptJB);
middlePanel.setLayout(new FlowLayout());
//build the secondJPanel
JPanel secondJPanel=new JPanel();
secondJPanel.add(subtotalJL);
secondJPanel.add(subtotalJT);
secondJPanel.add(taxJL);
secondJPanel.add(taxJT);
secondJPanel.add(totalJL);
secondJPanel.add(totalJT);
subtotalJT.setEditable(false);
taxJT.setEditable(false);
totalJT.setEditable(false);
secondJPanel.setLayout(new GridLayout(3,2,4,4));
//Build a container to add the first, middle and second jpanels to
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.setLayout(new BorderLayout());
container.add(firstJPanel, new BorderLayout().EAST);
container.add(middlePanel, new BorderLayout().CENTER);
container.add(secondJPanel, new BorderLayout().WEST);
return container;
}
//method that creates tab stops and tab styles
protected void setTabsAndStyles(JTextPane pane)
{
//set Font Style
Style fontStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular= pane.addStyle("regular", fontStyle);
StyleConstants.setFontFamily(fontStyle, "timesNewRoman");
Style s=pane.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
s= pane.addStyle("large", regular);
StyleConstants.setFontSize(s,10);
//create the tab stops
TabStop[] tb= new TabStop[2];
tb[0]=new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
tb[1]=new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
TabSet ts=new TabSet(tb);
//create the tab styles
styleContext tabStyle=styleContext.DefaultStyleContext();
AttributeSet aset= tabStyle.addAttribute(SimpleAttributeSet.EMPTY, styleConstants.TabSet, ts);
pane.setParagraphAttributes(aset, false);
}
// adds the text to the textpane
public JTextPane addText()
{
Document doc=pane.getDocument();
try
{
//clear previous text
doc.remove(0, doc.getLength());
//insert title
doc.insertString(0, "CODE #\tDESCRIPTION\tCOST\n", pane.getStyle("large"));
//insert receipt information
for (int i=0; i<codeRcptArray.length; i++)
{
doc.insertString(doc.getLength(), codeRcptArray[i] + "\t", pane.getStyle("italic"));
doc.insertString(doc.getLength(), nameRcptArray[i] + "\t", pane.getStyle("italic"));
doc.insertString(doc.getLength(), "$" + priceRcptArray[i] + "\n", pane.getStyle("italic"));
}
}
catch(Exception ble)
{
System.err.println("current data not entered correctly. .");
}
return pane;
}
//event handler for clicks
public void actionPerformed(ActionEvent e)
{
String evt = e.getActionCommand();
//if the user "enter" button is pressed
if(evt == "Enter")
{
String newCode= codeJC.getSelectedItem().toString();
String newName= nameArray[codeJC.getSelectedIndex()];
String newPrice= priceArray[codeJC.getSelectedIndex()];
//enlarge the recpt arrays by 1
codeRcptArray=enlargeArray(codeRcptArray);
priceRcptArray=enlargeArray(priceRcptArray);
nameRcptArray=enlargeArray(nameRcptArray);
//add new data to the recpt arrays
codeRcptArray[codeRcptArray.length-1]=newCode;
priceRcptArray[priceRcptArray.length-1]=newPrice;
nameRcptArray[nameRcptArray.length-1]=newName;
//display item name, item price, and calculate running subtotal
nameJT.setText(nameArray[codeJC.getSelectedIndex()]);
priceJT.setText(myFormatter.format(Double.parseDouble(priceArray[codeJC.getSelectedIndex()])));
subTotal=subTotal + Double.parseDouble(priceArray[codeJC.getSelectedIndex()]);
}
//if the "show receipt" button is pressed
if(evt == "Recpt")
{
addText();
}
if(evt == "Ttl")
{
getTotal(subTotal);
}
}
//enlarge an array by 1
public String[] enlargeArray(String[] currentArray)
{
String[]newArray= new String[currentArray.length+1];
for (int i=0; i<currentArray.length; i++)
newArray[i]=currentArray[i];
return newArray;
}
public void getTotal(double rSubTotal)
{
double tax = rSubTotal *.05;
double total=rSubTotal + tax;
subtotalJT.setText("");
subtotalJT.setText(myFormatter.format(rSubTotal));
taxJT.setText("");
taxJT.setText(myFormatter.format(tax));
totalJT.setText("");
totalJT.setText(myFormatter.format(total));
}
public static void main(String args[])
{
MyfileJan frame= new MyfileJan();
//how to close an open browserWindow (this action required by user only)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(frame.buildPane());
frame.setSize(900,550);
frame.setVisible(true);
}
}
can not compile this. Can anyone see what is the problem or lead me in the right direction?
thank you very much

New Topic/Question
Reply




MultiQuote




|