2015-11-20 13 views
0
import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    class Event implements ActionListener{ 
     JButton jb1; 
     void setGui() 
     { 

     JFrame x=new JFrame("CALCULATOR"); 
     x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     x.setSize(800,800); 
     x.setVisible(true); 

     Container c=x.getContentPane(); 
     c.setLayout(null); 
     c.setBackground(Color.BLACK); 

    JLabel jl1=new JLabel("NUMBER 1"); 
     jl1.setSize(100,50); 
     jl1.setLocation(0,0); 
     jl1.setForeground(Color.YELLOW); 
     c.add(jl1); 

     JLabel jl2=new JLabel("NUMBER 2"); 
     jl2.setSize(100,50); 
     jl2.setLocation(0,150); 
     jl2.setForeground(Color.YELLOW); 
     c.add(jl2); 

    JLabel jl3=new JLabel("ANSWER"); 
     jl3.setSize(100,50); 
     jl3.setLocation(0,400); 
     jl3.setForeground(Color.YELLOW); 
     c.add(jl3); 

    JTextField jtf1=new JTextField(); 
     jtf1.setSize(100,50); 
     jtf1.setLocation(80,0); 
     jtf1.setBackground(Color.GREEN); 
     c.add(jtf1); 

    JTextField jtf2=new JTextField(); 
     jtf2.setSize(100,50); 
     jtf2.setLocation(80,150); 
     jtf2.setBackground(Color.GREEN); 
     c.add(jtf2); 

     JTextField jtf3=new JTextField(); 
     jtf3.setSize(100,50); 
     jtf3.setLocation(80,400); 
     jtf3.setBackground(Color.GREEN); 
     c.add(jtf3); 

     jb1=new JButton("ADD"); 
     jb1.setSize(100,100); 
     jb1.setLocation(300,500); 
     jb1.setBackground(Color.RED); 
     c.add(jb1); 

    JButton jb2=new JButton("MULTIPLY"); 
     jb2.setSize(100,100); 
     jb2.setLocation(450,500); 
     jb2.setBackground(Color.RED); 
     c.add(jb2); 

      jb1.addActionListener(this); 
      jb2.addActionListener(this); 
     } 



    public void actionPerformed(ActionEvent q) 
    { if(q.getSource()==jb1)(jb1=o,jb2=n) 
      { 

      }    
    } 
} 

     class DX 
{ 


    public static void main(String args[]) 
    { 


     Event et=new Event(); 
     et.setGui(); 
     } 

    } 

//這是我寫的Java代碼需要一點幫助。謝謝 // https://lh4.googleusercontent.com/-P9R0IIsAcz8/Vk6QQSs63SI/AAAAAAAAABk/d0q1jfpm9cE/w908-h809-no/fffffffff.PNG // https://lh4.googleusercontent.com/-5yjWTnQ8X8o/Vk6QVfroQtI/AAAAAAAAAB8/17k1HnHGmlc/w869-h629-no/hhhh.PNG有人可以告訴我如何從jtf1和jtf2獲得輸入並在jtf3上顯示輸出?

+1

看來,你應該去[這裏](http://docs.oracle.com/javase/7/docs/api/java/awt/TextField.html)而不是SO。 – TEXHIK

+0

當我去那裏? –

+0

就在你開始使用'TextField'之前 – TEXHIK

回答

0

請參見本,略低於代碼複製到你機器上並運行。 我希望這是你想要的。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class DX { 

    public static void main(String args[]) 
    { 
     System.out.println("Hello"); 
     Event et=new Event(); 
     et.setGui(); 
    } 
} 

class Event implements ActionListener{ 
    JButton jb1; 
    JTextField jtf1; 
    JTextField jtf2; 
    JTextField jtf3; 
    void setGui() 
    { 

     JFrame x=new JFrame("CALCULATOR"); 
     x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     x.setSize(800,800); 
     x.setVisible(true); 

     Container c=x.getContentPane(); 
     c.setLayout(null); 
     c.setBackground(Color.BLACK); 

     JLabel jl1=new JLabel("NUMBER 1"); 
     jl1.setSize(100,50); 
     jl1.setLocation(0,0); 
     jl1.setForeground(Color.YELLOW); 
     c.add(jl1); 

     JLabel jl2=new JLabel("NUMBER 2"); 
     jl2.setSize(100,50); 
     jl2.setLocation(0,150); 
     jl2.setForeground(Color.YELLOW); 
     c.add(jl2); 

     JLabel jl3=new JLabel("ANSWER"); 
     jl3.setSize(100,50); 
     jl3.setLocation(0,400); 
     jl3.setForeground(Color.YELLOW); 
     c.add(jl3); 

     jtf1=new JTextField(); 
     jtf1.setSize(100,50); 
     jtf1.setLocation(80,0); 
     jtf1.setBackground(Color.GREEN); 
     c.add(jtf1); 

     jtf2=new JTextField(); 
     jtf2.setSize(100,50); 
     jtf2.setLocation(80,150); 
     jtf2.setBackground(Color.GREEN); 
     c.add(jtf2); 

     jtf3=new JTextField(); 
     jtf3.setSize(100,50); 
     jtf3.setLocation(80,400); 
     jtf3.setBackground(Color.GREEN); 
     c.add(jtf3); 

     jb1=new JButton("ADD"); 
     jb1.setSize(100,100); 
     jb1.setLocation(300,500); 
     jb1.setBackground(Color.RED); 
     c.add(jb1); 

     JButton jb2=new JButton("MULTIPLY"); 
     jb2.setSize(100,100); 
     jb2.setLocation(450,500); 
     jb2.setBackground(Color.RED); 
     c.add(jb2); 

     jb1.addActionListener(this); 
     jb2.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent q) 
    { 
     System.out.println(q.getActionCommand()); 

     if((jtf1.getText() != null && jtf1.getText().length() > 0) && (jtf2.getText() != null && jtf2.getText().length() > 0)){ 
      try { 

       int val1 = Integer.parseInt(jtf1.getText()) ; 
       int val2 = Integer.parseInt(jtf2.getText()) ; 

       if("MULTIPLY".equals(q.getActionCommand())){ 
        int ans = (val1*val2); 
        jtf3.setText(ans+""); 
       }else if("ADD".equals(q.getActionCommand())){ 
        int ans = (val1+val2); 
        jtf3.setText(ans+""); 
       } 
      } catch (Exception e) { 
       jtf1.setText(""); 
       jtf2.setText(""); 
       jtf3.setText(""); 
       JOptionPane.showMessageDialog(null, "Please Enter Valid Value", "Error", 0); 
      } 
     }else{ 
      jtf1.setText(""); 
      jtf2.setText(""); 
      jtf3.setText(""); 
      JOptionPane.showMessageDialog(null, "Please Enter Text First", "Error", 0); 
     } 


    } 
} 
+0

是的,這是我想要做的。非常感謝你^^ –

相關問題