Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Trang 1 của 2 12 CuốiCuối
Kết quả 1 đến 10 của 11
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    bạn có thể tham khảo code này


    Mã:
    import java.awt.*;import java.lang.*;import java.awt.event.*;import java.applet.Applet;import java.awt.datatransfer.*; public class calculator extends Applet{ public void init() {  calf calWindow = new calf("Java Calculator");  calWindow.setSize(200, 250);  calWindow.setVisible(true);  calWindow.setResizable(false); }} class calf extends Frame implements WindowListener, ActionListener, KeyListener{  String command, copy, arg, chg, txt;  double result;  String number = "123456789.0";  String operator = "/*-+=";  CopyPaste cp;   Menu Menu1;  MenuBar Menubar1;  MenuItem menuitem1, menuitem2, menuitem3;  TextField entrytext;  Button numbut []; //Number buttons  Button combut []; //Command buttons  Panel companel, numpanel;   public static void main(String[] arguments)  {   calf calWindow = new calf("Java Calculator");   calWindow.setSize(200, 250);   calWindow.setVisible(true);  }   public calf(String title)  {   super(title);   addWindowListener(this);   addKeyListener(this);    cp = new CopyPaste();   //cp.clip = getToolkit().getSystemClipboard();    setBackground(Color.blue);   setLayout(new GridLayout(1, 1));   Menubar1 = new MenuBar();   Menu1 = new Menu("Edit");   menuitem1 = new MenuItem("&Copy");   Menu1.add(menuitem1);   menuitem1.addActionListener(this);   menuitem2 = new MenuItem("&Paste");   Menu1.add(menuitem2);   menuitem2.addActionListener(this);   menuitem3 = new MenuItem("&Exit");   Menu1.add(menuitem3);   menuitem3.addActionListener(this);   Menubar1.add(Menu1);   setMenuBar(Menubar1);   GridBagLayout gridbag = new GridBagLayout();   GridBagConstraints constraints = new GridBagConstraints();   setLayout(gridbag);   constraints.weighty = 1;   constraints.weightx = 1;   //constraints.fill = GridBagConstraints.BOTH;   Font bigFont = new Font("Courier",Font.BOLD, 14);   entrytext = new TextField(20);   constraints.gridwidth = GridBagConstraints.REMAINDER;   gridbag.setConstraints(entrytext,constraints);   add(entrytext);   entrytext.setFont(bigFont);   entrytext.setEditable(false);   entrytext.setForeground(Color.black);   entrytext.setBackground(Color.white);   entrytext.addKeyListener(this);   entrytext.requestFocus();    constraints.weighty = 1;   constraints.weightx = 1;   companel = new Panel();    constraints.gridwidth = GridBagConstraints.REMAINDER;   gridbag.setConstraints(companel,constraints);   /*    Command GridLayout    ---------------------    | Back |  CE  |  C |    ---------------------   */    companel.setLayout(new GridLayout(1,3,5,5));    // Create the buttons   Font comsFont = new Font("Arial",Font.BOLD, 12);   String[] coms = { "Back","CE","C"             };   combut = new Button[3];         for (int i=0; i<=2; i++)      {       combut[i] = new Button(coms[i]);       companel.add(combut[i]);    combut[i].addActionListener(this);    combut[i].setFont(comsFont);    combut[i].addKeyListener(this);    }   add(companel);   companel.addKeyListener(this);    constraints.weighty = 4;   constraints.weightx = 1;   numpanel = new Panel();   constraints.gridwidth = GridBagConstraints.REMAINDER;   gridbag.setConstraints(numpanel,constraints);   /*    Number GridLayout    ---------------------    | 7 | 8 | 9 | / |sqr|    ---------------------    | 4 | 5 | 6 | * | % |    ---------------------    | 1 | 2 | 3 | - |1/x|    ---------------------    | 0 |+/-| . | + | = |    ---------------------            */   numpanel.setLayout(new GridLayout(4,5,3,3));   // Create the buttons   String[] nums = { "7","8","9","/","sqrt",              "4","5","6","*","%",              "1","2","3","-","1/x",              "0","+/-",".","+","="             };   numbut = new Button[20];   for (int i=0; i<=19; i++)       {       numbut[i] = new Button(nums[i]);       numpanel.add(numbut[i]);    numbut[i].addActionListener(this);    numbut[i].addKeyListener(this);    if(operator.indexOf(nums[i]) > -1)    {     numbut[i].setForeground(Color.red);    }    else    {     numbut[i].setForeground(Color.blue);    }    }   add(numpanel);   numpanel.addKeyListener(this);    //initialize global variables.   command = "+";   copy = "";   chg = "N";   txt = "";   arg = "";   result = 0;  }   public void actionPerformed(ActionEvent e)  {   if (e.getActionCommand() == "&Exit")   {    setVisible(false);    System.exit(0);   }   else if (e.getActionCommand() == "&Copy")         {    String txt = entrytext.getText();    if (txt != null)          {              cp.doCopy(txt);          }   }   else if (e.getActionCommand() == "&Paste")         {          cp.doPaste();    if (cp.ctxt != null)          {     entrytext.setText(cp.ctxt);    }   }   else if (e.getActionCommand() == "Back") back_space();   else if (e.getActionCommand() == "CE") entrytext.setText("");   else if (e.getActionCommand() == "C")   {    result = 0;    command = "+";    entrytext.setText("");   }   else   {    arg = e.getActionCommand();    txt = entrytext.getText();    if(number.indexOf(arg) > -1)    {     if (chg == "Y") txt = "";     txt = txt + arg;     entrytext.setText(txt);     chg = "N";    }    else check_entry();   }  }   public void keyPressed(KeyEvent k){}  public void keyReleased(KeyEvent k)  {   int ikey = k.getKeyCode();   if (ikey == 127) entrytext.setText("");  }  public void keyTyped(KeyEvent k)  {   int ikey = k.getKeyChar();   if (ikey == 8) back_space();   else if (ikey == 10)   {    arg = "=";    txt = entrytext.getText();    display_ans();   }   else   {    txt = entrytext.getText();    char ckey = (char) ikey;    arg = String.valueOf(ckey);    if(number.indexOf(arg) > -1)    {     if (chg == "Y") txt = "";     txt = txt + arg;     entrytext.setText(txt);     chg = "N";    }    else check_entry();   }  }   public void back_space()  {   txt = entrytext.getText();   int l = txt.length();   if (l > 0)   {    txt = txt.substring(0,l-1);    entrytext.setText(txt);   }  }  public void display_ans()  {   entrytext.setText(compute_tot(txt,command));   chg = "Y";   command = "+";   result = 0;  }  public void check_entry()  {   if(arg.equals("=")) display_ans();   else if(arg.equals("%"))   {    entrytext.setText(compute_tot(txt,arg));    chg = "Y";   }   else if(operator.indexOf(arg) > -1)   {    entrytext.setText(compute_tot(txt,command));    command = arg;    chg = "Y";   }   else if(arg == "sqrt")   {    entrytext.setText(compute_tot(txt,arg));    chg = "Y";    command = "";   }   else if(arg == "+/-")   {    Double tnum = Double.valueOf(txt);    double num = tnum.doubleValue();    num = num * -1;    entrytext.setText(String.valueOf(num));    chg = "Y";   }   else if(arg == "1/x")   {    entrytext.setText(compute_tot(txt,arg));    chg = "Y";   }  }  String compute_tot(String t, String c)  {   Double tnum = Double.valueOf(t);   double num = tnum.doubleValue();    if (c.equals("+")) result = result + num;   else if (c.equals("-")) result = result - num;   else if (c.equals("*")) result = result * num;   else if (c.equals("/")) result = result / num;   else if (c.equals("%")) result = num / 100;   else if (c.equals("+/-")) result = num * -1;   else if (c.equals("1/x")) result = 1.000 / num;   else if (c.equals("sqrt")) result = Math.sqrt(num);   return String.valueOf(result);  }   public void windowClosing(WindowEvent we)  {   setVisible(false);   System.exit(0);  }  public void windowClosed(WindowEvent we) {}  public void windowDeiconified(WindowEvent we) {}  public void windowIconified(WindowEvent we) {}  public void windowOpened(WindowEvent we) {}  public void windowActivated(WindowEvent we) {}  public void windowDeactivated(WindowEvent we) {}}  class CopyPaste implements ClipboardOwner{     Clipboard clip;    String ctxt;     CopyPaste()    {  clip = new Clipboard("clip");        ctxt = null;    }     void doCopy(String txt)    {        StringSelection trans = new StringSelection(txt);        clip.setContents(trans, this);    }     void doPaste()    {  ctxt = null;        Transferable toPaste = clip.getContents(this);        if (toPaste != null)        {            try            {                ctxt = (String)toPaste.getTransferData(                    DataFlavor.stringFlavor);            }            catch (Exception e)            {             System.out.println("Error -- " + e.toString());   }        }     }     public void lostOwnership(Clipboard clip,        Transferable contents) {    }}

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Máy tính bỏ túi bằng lập trình Java?

    Mình vừa mới bước chân vào học lập trình Java, ông thầy đã cho làm bài tập là thiết kế và lập trình máy tính điện tử giống như của windows. Mình chưa biết làm thế nào cả, xin được mọi người tận tình giúp đỡ, nếu có code và thiết kế cho mình xin với nhé. Cảm ơn nhiều.

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    có ai không cho em hỏi những pm nào là cần thiết cho lập trình java ạ

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    doan code tren minh chay bang jdk sao no bao loi zay moi nguoi (ai biet chi minh voi

    mình chạy bằng JDK cũng bi lõi ai biết giúp mình với

  5. #5
    Ngày tham gia
    Sep 2015
    Đang ở
    Hà Nội
    Bài viết
    0
    Hoc Java thì chọn NetBean đi, bản beta 7.0 thì phải, làm ứng dụng giao diện như bên VS.bản 6.9 cài đặt chừng 1GB ổ cứng.

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi lethanhminh1789
    mình chạy bằng JDK cũng bi lõi ai biết giúp mình với
    Applet mà, chắc chạy app rồi

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    là sao bạn.mình mói học java .mình bật JDK lên la no vào phần viết code chứ mình co thấy chỗ nào chọn applycation hay applet gi đâu. hic

  8. #8
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi hoangtrungduc92
    có ai không cho em hỏi những pm nào là cần thiết cho lập trình java ạ
    Cũng không có gì nhiều bạn ạ! 1 gói JDK của Oracle, 1 trình editer và 1 hệ điều hành(Windows hoặc Linux) . Tất cả chỉ có thế! [IMG]images/smilies/lick.gif[/IMG]

  9. #9
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Máy tính của mình làm với các chức năng + - * /. Các bạn xem và góp ý nhé.

  10. #10
    Sao mình ấn Run File thì chạy mà Run Project thì không vậy ta? Ai biết trả lời giúp với

 

 
Trang 1 của 2 12 CuốiCuối

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •