0

我是java编程的新手。我想执行一个任务,当按下按钮时,即从附加代码中我的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);
        }
    }
};
4

1 回答 1

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);
    }

顺便说一句,您必须在发布之前缩进您的代码,让您的问题变得清晰是您的工作

于 2013-07-13T05:42:42.590 回答