I am having trouble SAVING to a File and READING from a File. I have 6 TextFields and 5 JButtons. I have a save button in 2 of the panels. I would like to understand how to save to another file using JBUTTONS.
Any Examples with comments would be nice. I am mostly a visual learner. THANK YOU
In my JOptionPane.showMessage I get this.... When I do the FileWriter and PrintWriter for my 5th Button which is the Enter/OK button.
javax.swing.JTextField[,120,60,200x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@73e9743a,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=50,columnWidth=0,command=,horizontalAlignment=LEADING]
Here is my Code
package courseProject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class FlooringStore {
//global
JPanel P, P1, P2, P3;
JButton B, B1, B2, B3, B4;
JLabel JL;
JTabbedPane TP;
JTextField JT = new JTextField(50);
String woodType, carpetType;
double woodArea, carpetArea;
double wArea, cArea, totalWood, totalCarpet;
public FlooringStore(){
JTabbedPane TP = new JTabbedPane();
JFrame JF = new JFrame();
JF.setVisible(true); //made true so we can see the frame
JF.setTitle("FLOORING STORE");
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setSize(400,300); //set size Width/Height
JF.add(TP); //ands the TabbedPanel to frame
P = new JPanel();
P.setLayout(null);
JL = new JLabel("WELCOME TO THE FLOORING STORE");
JL.setSize(300,100);
JL.setLocation(85,50);
P.add(JL);
TP.add(P, "HOME PAGE"); //panel1 tab1 name
P1 = new JPanel();
P1.setLayout(null);
B = new JButton("CUSTOMER");
B.setSize(150,150);
B.setLocation(20,20);
B1 = new JButton("EMPLOYEE");
B1.setSize(150,150);
B1.setLocation(200,20);
P1.add(B);
P1.add(B1);
TP.add(P1, "ID"); //panel 2 tab2
class customerB implements ActionListener{
public void actionPerformed (ActionEvent e){
JFrame JF2 = new JFrame();
JF2.setVisible(true);
JF2.setTitle("CUSTOMER");
JF2.setSize(400,300);
JPanel JP = new JPanel();
JP.setLayout(null);
JF2.add(JP);
JLabel L2 = new JLabel("CUSTOMER NAME: ");
L2.setSize(300,100);
L2.setLocation(10,20);
JP.add(L2);
JTextField JT = new JTextField(50);
JT.setSize(200,20);
JT.setLocation(120, 60);
JP.add(JT);
JButton B5 = new JButton("ENTER");
B5.setSize(100,50);
B5.setLocation(100,100);
JP.add(B5);
class okB implements ActionListener{
public void actionPerformed (ActionEvent e){
try{
JTextField JT = new JTextField(50);
JT.setSize(200,20);
JT.setLocation(120, 60);
JP.add(JT);
String name = JT.getText();
FileWriter stream = new FileWriter("customer.txt", true);
BufferedWriter out = new BufferedWriter(stream);
out.write(name);
out.close();
}/*end try*/ catch (Exception ex){
System.out.println("ERROR!! %s");
}//end catch
}//end actionPerformed
}//end end action z
B5.addActionListener(new okB());
}// end actionPerformed
}//end end action
B.addActionListener(new customerB());
class employeeB implements ActionListener{
public void actionPerformed (ActionEvent e){
JFrame JF3 = new JFrame();
JF3.setVisible(true);
JF3.setTitle("EMPLOYEE");
JF3.setSize(400,300);
JPanel JP = new JPanel();
JP.setLayout(null);
JF3.add(JP);
JLabel L2 = new JLabel("EMPLOYEE NAME: ");
L2.setSize(300,100);
L2.setLocation(10,20);
JP.add(L2);
JTextField JT1 = new JTextField(50); //EmployeeName
JT1.setSize(200,20);
JT1.setLocation(120, 60);
JP.add(JT1);
JLabel L3 = new JLabel("EMPLOYEE ID: ");
L3.setSize(300,100);
L3.setLocation(10,60);
JP.add(L3);
JTextField JT2 = new JTextField(30); //EmployeeID
JT2.setSize(100,20);
JT2.setLocation(100, 100);
JP.add(JT2);
}// end actionPerformed
}//end end action
B1.addActionListener(new employeeB());
P2 = new JPanel();
P2.setLayout(null);
B2 = new JButton("WOOD");
B2.setSize(150,150);
B2.setLocation(20,20);
B3 = new JButton("CARPET");
B3.setSize(150,150);
B3.setLocation(200,20);
P2.add(B2);
P2.add(B3);
TP.add(P2, "FLOORING"); //panel 3 tab3
class woodB implements ActionListener{
public void actionPerformed (ActionEvent e){
JFrame JF4 = new JFrame();
JF4.setVisible(true);
JF4.setTitle("WOOD");
JF4.setSize(400,300);
JPanel JP = new JPanel();
JP.setLayout(null);
JF4.add(JP);
JLabel L4 = new JLabel("TYPE OF WOOD: ");
L4.setSize(300,100);
L4.setLocation(10,20);
JP.add(L4);
JTextField JT3 = new JTextField(50); //wood Type
JT3.setSize(250,20);
JT3.setLocation(120, 60);
JP.add(JT3);
JLabel L5 = new JLabel("AREA OF THE ROOM: ");
L5.setSize(300,100);
L5.setLocation(10,80);
JP.add(L5);
JTextField JT4 = new JTextField(50); //Area
JT4.setSize(100,20);
JT4.setLocation(130, 120);
JP.add(JT4);
}// end actionPerformed
}//end end action
B2.addActionListener(new woodB());
class carpetB implements ActionListener{
public void actionPerformed (ActionEvent e){
JFrame JF4 = new JFrame();
JF4.setVisible(true);
JF4.setTitle("CARPET");
JF4.setSize(400,300);
JPanel JP = new JPanel();
JP.setLayout(null);
JF4.add(JP);
JLabel L5 = new JLabel("COLOR OF CARPET: ");
L5.setSize(300,100);
L5.setLocation(10,20);
JP.add(L5);
JTextField JT4 = new JTextField(50); //carpet color
JT4.setSize(250,20);
JT4.setLocation(130, 60);
JP.add(JT4);
JLabel L6 = new JLabel("AREA OF THE ROOM: ");
L6.setSize(300,100);
L6.setLocation(10,80);
JP.add(L6);
JTextField JT5 = new JTextField(50); //Area
JT5.setSize(100,20);
JT5.setLocation(130, 120);
JP.add(JT5);
}// end actionPerformed
}//end end action
B3.addActionListener(new carpetB());
P3 = new JPanel();
P3.setLayout(null);
B4 = new JButton("TOTAL");
B4.setSize(150,150);
B4.setLocation(100,20);
P3.add(B4);
TP.add(P3, "CHECKOUT"); //panel 3 tab4
class totalB implements ActionListener{
public void actionPerformed (ActionEvent e){
JOptionPane.showMessageDialog(null,"Total Amount for "+JT+ "\nWood Type: "+woodType+"\n Carpet Type: "
+carpetType+"\n\n Total Amount: "+totalWood+" "+totalCarpet);
}// end actionPerformed
}//end end action
B4.addActionListener(new totalB());
}//end constructor
public static void main(String[] args) {
new FlooringStore();
}
}
Aucun commentaire:
Enregistrer un commentaire