Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java Forum > Java - ช่วยทำปุ่มคำนวณแล้ว Save ลง Text หน่อยครับ ขอร้องนะครับผู้รู้



 

Java - ช่วยทำปุ่มคำนวณแล้ว Save ลง Text หน่อยครับ ขอร้องนะครับผู้รู้

 



Topic : 101098

Guest




ช่วยทำปุ่มคำนวณหน่อยครับ งงหมดแล้ว

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







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-09-28 11:26:02 By : เด็กดี View : 1821 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

อันนี้ตัวอย่างการเขียนลง Text file ด้วยภาษา Java ครับ

Java Create and Write text file







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-30 09:05:19 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Java - ช่วยทำปุ่มคำนวณแล้ว Save ลง Text หน่อยครับ ขอร้องนะครับผู้รู้
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่