|
|
|
Java - ช่วยทำปุ่มคำนวณแล้ว Save ลง Text หน่อยครับ ขอร้องนะครับผู้รู้ |
|
|
|
|
|
|
|
ช่วยทำปุ่มคำนวณหน่อยครับ งงหมดแล้ว
Code (Java)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
class product {
String id;
String name;
float price;
int rec; //เก็บจำนวนรายการสินค้า
}
class AddPanel {
void addItem(JPanel p, JComponent c, int x, int y,
int width, int height, int align) {
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.insets = new Insets(5,5,5,5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c,gc);
}
}
public class ex30_000 extends JFrame {
String id; int count=0;
float pprice, pqty, ptotal;
String pid, pname;
JLabel idlbl, namelbl, pricelbl, qlbl, anslbl;
JTextField idtxt, nametxt, pricetxt, qtxt;
JPasswordField pwtxt;
JButton fbtn, calbtn, savebtn, resetbtn;
Font fn1 = new Font("Courier New",Font.BOLD,18);
Font fn2 = new Font("Microsoft San Serif",Font.BOLD,14);
product prod = new product();
product pr[] = new product[50];
public ex30_000(String title) {
setTitle(title);
idlbl = new JLabel("Product ID");
namelbl = new JLabel("Product Name");
pricelbl = new JLabel("Price/Unit");
qlbl = new JLabel("Q'ty");
idtxt = new JTextField(15);
nametxt = new JTextField(15);
nametxt.setEnabled(false);
pricetxt = new JTextField(15);
pricetxt.setEnabled(false);
qtxt = new JTextField(15);
idlbl.setFont(fn1);
namelbl.setFont(fn1);
pricelbl.setFont(fn1);
qlbl.setFont(fn1);
idtxt.setFont(fn1);
nametxt.setFont(fn1);
pricetxt.setFont(fn1);
qtxt.setFont(fn1);
fbtn = new JButton("Find");
calbtn = new JButton("Calculate");
savebtn = new JButton("Save");
resetbtn = new JButton("Reset");
anslbl = new JLabel("total = 0.00 baht",SwingConstants.CENTER);
anslbl.setOpaque(true);
anslbl.setBackground(Color.blue);
anslbl.setForeground(Color.white);
anslbl.setPreferredSize(new Dimension(400,25));
fbtn.setFont(fn1);
calbtn.setFont(fn1);
savebtn.setFont(fn1);
resetbtn.setFont(fn1);
anslbl.setFont(fn2);
JPanel panel = new JPanel();
panel.setLayout (new GridBagLayout ());
panel.setBackground(Color.white);
AddPanel y = new AddPanel();
y.addItem(panel,idlbl,0,0,2,1,GridBagConstraints.WEST);
y.addItem(panel,idtxt,2,0,2,1,GridBagConstraints.EAST);
y.addItem(panel,namelbl,0,1,2,1,GridBagConstraints.WEST);
y.addItem(panel,nametxt,2,1,2,1,GridBagConstraints.EAST);
y.addItem(panel,pricelbl,0,2,2,1,GridBagConstraints.WEST);
y.addItem(panel,pricetxt,2,2,2,1,GridBagConstraints.EAST);
y.addItem(panel,qlbl,0,3,2,1,GridBagConstraints.WEST);
y.addItem(panel,qtxt,2,3,2,1,GridBagConstraints.EAST);
y.addItem(panel,fbtn,0,4,1,1,GridBagConstraints.WEST);
y.addItem(panel,calbtn,1,4,1,1,GridBagConstraints.WEST);
y.addItem(panel,savebtn,2,4,1,1,GridBagConstraints.WEST);
y.addItem(panel,resetbtn,3,4,1,1,GridBagConstraints.WEST);
y.addItem(panel,anslbl,0,5,4,1,GridBagConstraints.CENTER);
add(panel);
fbtn.addActionListener(new ButtonListener());
calbtn.addActionListener(new ButtonListener());
savebtn.addActionListener(new ButtonListener());
resetbtn.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==fbtn) {
id = idtxt.getText();
if (!checkID(id)) {
String s = "Cannot Find " + id +" !!!";
anslbl.setText(s);
}
} //fbtn
if(e.getSource()==calbtn) {
pqty = Float.parseFloat(qtxt.getText());
ptotal = pprice * pqty;
String s = "total = " + ptotal + " baht";
anslbl.setText(s); savedata();
} //calbtn
if(e.getSource()==resetbtn) {
idtxt.setText(""); nametxt.setText("");
pricetxt.setText(""); qtxt.setText("");
anslbl.setText(""); idtxt.requestFocus();
} //reset
} //actionPerformed
} //ButtonListener
public void savedata() {
}
public boolean checkID(String id) {
boolean check = false;
File ifile = new File("product.txt");
FileReader f_in = null;
BufferedReader b_in = null;
product[] pr = new product[50];
String line; int i= 0;
try {
f_in = new FileReader(ifile);
b_in = new BufferedReader(f_in);
while ((line = b_in.readLine()) != null) {
pr[i] = new product();
String msg[] = line.split(";",2);
pr[i].id = msg[0].substring(0,4);
pr[i].name = msg[0].substring(4,msg[0].length());
pr[i].price = Float.parseFloat(msg[1]);
//JOptionPane.showMessageDialog(null,
//pr[i].id+" "+pr[i].name+" "+pr[i].price);
i++;
}
count = i;
}
catch (IOException e) {
System.out.println(e);
}
finally {
try {
if (b_in != null)
b_in.close();
for (int a=0;a<count;a++) {
if (id.equals(pr[a].id)) {
check = true;
nametxt.setText(pr[a].name);
String p = Float.toString(pr[a].price);
pricetxt.setText(p);
pid = pr[a].id;
pname = pr[a].name;
pprice = pr[a].price;
qtxt.requestFocus();
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
return check;
} //checkID
public static void main(String[] args) {
ex30_000 s = new ex30_000("ขายสินค้า");
s.setSize(500, 450);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setVisible(true);
}
}
Tag : JavaScript, JAVA
|
|
|
|
|
|
Date :
2013-09-28 11:26:02 |
By :
เด็กดี |
View :
1907 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันนี้ตัวอย่างการเขียนลง Text file ด้วยภาษา Java ครับ
Java Create and Write text file
|
|
|
|
|
Date :
2013-09-30 09:05:19 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|