2013-07-13 56 views
0

我是java programming的新手。我想執行一個任務,當按下按鈕時,即從附加代碼的JFrame中顯示0-9時,該按鈕的值必須被分配給在按下按鈕之前選擇的JField。如何操作?從按鈕中獲取文本到選定的文本域

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

class calci2 extends JFrame implements ActionListener { 
    JFrame f1; 
    JPanel p; 
    JButton b[] = new JButton[10]; 
    JButton btnadd; 
    JButton btnmul; 
    JButton btndiv; 
    JButton btnsub; 
    public static JTextField t1; 
    JTextField t2; 
    JTextField t3; 
    JLabel no1; 
    JLabel no2; 
    JLabel res; 

    calci2() { 
     f1 = new JFrame(); 
     p = new JPanel(); 
     t1 = new JTextField(15); 
     t2 = new JTextField(15); 
     t3 = new JTextField(15); 
     no1 = new JLabel("Enter 1st number"); 
     no2 = new JLabel("Enter 2nd number"); 
     res = new JLabel(" Result is  "); 
     btnadd = new JButton("ADD"); 
     btnmul = new JButton("MUL"); 
     btndiv = new JButton("DIV"); 
     btnsub = new JButton("SUB"); 
     for (int i = 0; i < 10; i++) { 
      b[i] = new JButton("" + i); 
     } 
     btnadd.addActionListener(this); 
     btnmul.addActionListener(this); 
     btndiv.addActionListener(this); 
     btnsub.addActionListener(this); 

     for (int i = 0; i < 10; i++) { 
      b[i].addActionListener(this); 
     } 
     p.add(no1); 
     p.add(t1); 
     p.add(no2); 
     p.add(t2); 
     p.add(res); 
     p.add(t3); 
     p.add(btnadd); 
     p.add(btnmul); 
     p.add(btndiv); 
     p.add(btnsub); 

     for (int i = 0; i < 10; i++) { 
      p.add(b[i]); 
     } 
     this.add(p); 
    } 

    public static void main(String args[]) { 
     calci2 c = new calci2(); 
     c.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     c.setSize(300, 300); 
     c.setVisible(true); 
     c.setResizable(false); 
     c.setLocationRelativeTo(null); 
    } 

    public void actionPerformed(ActionEvent e) { 
     String str = e.getActionCommand(); 
     String s1 = new String(t1.getText()); 
     String s2 = new String(t2.getText()); 
     String s3 = new String(); 
     int a = Integer.parseInt(s1); 
     int b = Integer.parseInt(s2); 
     if (str.equals("ADD")) { 
      int c = a + b; 
      s3 = String.valueOf(c); 
      t3.setText(s3); 
     } 

     else if (str.equals("SUB")) { 
      int c = a - b; 
      s3 = String.valueOf(c); 
      t3.setText(s3); 
     } 

     else if (str.equals("MUL")) { 
      int c = a * b; 
      s3 = String.valueOf(c); 
      t3.setText(s3); 
     } 

     else if (str.equals("DIV")) { 
      int c = a/b; 
      s3 = String.valueOf(c); 
      t3.setText(s3); 
     } 
    } 
}; 

回答

1

要檢查哪些文本字段現在被選中,你可以在類T1和T2創建兩個布爾值,我不認爲你需要第三個用於T3

讓我們說:

boolean t1_selected = false; 
boolean t2_selected = false; 

然後在Const中添加另一個偵聽器。該文本字段是焦點監聽者,注重聽衆將火當文本字段獲得關注,那麼你就可以在這個文本的布爾值更改爲true有

 t1.addFocusListener(new FocusListener() { 

     @Override 
     public void focusLost(FocusEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void focusGained(FocusEvent arg0) { 
      // TODO Auto-generated method stub 
      t1_selected = true; 
      t2_selected = false; 
     } 
    }); 
    t2.addFocusListener(new FocusListener() { 

     @Override 
     public void focusLost(FocusEvent e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void focusGained(FocusEvent e) { 
      // TODO Auto-generated method stub 
      t1_selected = false; 
      t2_selected = true; 
     } 
    }); 

現在的按鈕,你需要檢查對於函數actionPerformed中事件的來源,對所有按鈕使用e.getSource()而不是e.getActionCommand()。

例如:

if(e.getSource() == this.b[0]){ 
     if(t1_selected) 
     { 
      t1.setText("0"); 
     } 
     if(t2_selected) 
     { 
      t2.setText("0"); 
     } 
    } 
    else if(e.getSource() == this.b[1]){ 
     if(t1_selected) 
     { 
      t1.setText("1"); 
     } 
     if(t2_selected) 
     { 
      t2.setText("1"); 
     } 
    } 
      //rest of cases 

還沒有把這些線在開始的功能

 String s1 = new String(t1.getText()); 
     String s2 = new String(t2.getText()); 
     String s3 = new String(); 
     int a = Integer.parseInt(s1); 
     int b = Integer.parseInt(s2); 

,他們將通過異常,如果文本字段爲空,把它們只在裏面ADD,SUB,DIV的情況下和MUL

例如:

else if (e.getSource() == btnadd) { 
     String s1 = new String(t1.getText()); 
     String s2 = new String(t2.getText()); 
     String s3 = new String(); 
     int a = Integer.parseInt(s1); 
     int b = Integer.parseInt(s2); 

     int c = a + b; 
     s3 = String.valueOf(c); 
     t3.setText(s3); 
    } 

順便說一句,你必須在發佈前縮進代碼,這是你的工作,使你的問題一個乾淨的

+0

感謝ü!!!!這麼多薩拉你的幫助....但只是一個疑問更多 - 我想添加按鈕的值,即點擊時,0-9,最近選定的用戶textfield,而不是使用t1.setText ...值應實現在textfield按下按鈕之前選擇。這是我的第一篇文章,我會再次小心縮進我的代碼,以便其他人可以很容易地理解..這次忍受..謝謝你 – Omkar

+0

沒關係,現在答案編輯再次檢查,,,和URW –

+0

是的,它爲我工作..再次感謝。 – Omkar

相關問題