Java计算器程序使用JTextField问题

时间:2015-10-22 19:51:38

标签: java swing

import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalField extends JPanel {
    private JTextField field;

    public CalField() {

        setLayout(new GridLayout(1, 1, 5, 5));
        field = new JTextField(20);
        field.setFont(new Font("SansSerif", Font.PLAIN, 20));
        field.setEditable(true);
        add(field);

    }

    public JTextField getField() {
        return field;
    }

    public void setField(String text) {
        field.setText(text);
        System.out.print(text);

    }
}

CalButton课程:

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

import javax.swing.JButton;
import javax.swing.JPanel;

public class CalButton extends JPanel {
    private JButton[] buttons;
    private static final String[] buttonNames = { "7", "8", "9", "/", "4", "5",
            "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
    private CalField calField;

    public CalButton() {
        buttons = new JButton[buttonNames.length];
        setLayout(new GridLayout(4, 4, 3, 3));
        calField = new CalField();
        ButtonListener handler = new ButtonListener();
        for (int i = 0; i < buttonNames.length; i++) {
            buttons[i] = new JButton(buttonNames[i]);
            buttons[i].setFont(new Font("SansSerif", Font.BOLD, 20));

            add(buttons[i]);
            buttons[i].addActionListener(handler);
        }

    }

    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            // TODO Auto-generated method stub

            if (buttons[0].getActionCommand() == event.getActionCommand()) {
                calField.setField(event.getActionCommand());
//              System.out.print(buttons[0].getActionCommand());
            } else if (buttons[1].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[1].getActionCommand());
            } else if (buttons[2].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[2].getActionCommand());
            } else if (buttons[3].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[3].getActionCommand());
            } else if (buttons[4].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[4].getActionCommand());
            } else if (buttons[5].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[5].getActionCommand());
            } else if (buttons[6].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[6].getActionCommand());
            } else if (buttons[7].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[7].getActionCommand());
            } else if (buttons[8].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[8].getActionCommand());
            } else if (buttons[9].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[9].getActionCommand());
            } else if (buttons[10].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[10].getActionCommand());
            } else if (buttons[11].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[11].getActionCommand());
            } else if (buttons[12].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[12].getActionCommand());
            } else if (buttons[13].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[13].getActionCommand());
            } else if (buttons[14].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[14].getActionCommand());
            } else if (buttons[15].getActionCommand() == event
                    .getActionCommand()) {
                System.out.print(buttons[15].getActionCommand());
            }
        }
    }
}

计算器类:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Calculator {
    public static void main(String[] args) {

        JFrame application = new JFrame("Calculator");
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(250, 200);

        CalField calField = new CalField();
        CalButton calButton = new CalButton();

        application.add(calField, BorderLayout.PAGE_START);
        application.add(calButton, BorderLayout.CENTER);

        application.setVisible(true);
    }
}

在这个程序中,在JButton的actionListener中,我调用了CalField类的setText()方法来设置{{1}中相应按钮的文本}}。

但是文本在控制台中显示但未显示在JTextField中。我现在能做什么?请帮我一个人。

2 个答案:

答案 0 :(得分:3)

因此,在您的Calculator课程中,您创建CalField的实例并将其添加到JFrame ...

public class Calculator {
    public static void main(String[] args) {

        JFrame application = new JFrame("Calculator");
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(250, 200);

        CalField calField = new CalField();
        CalButton calButton = new CalButton();

        application.add(calField, BorderLayout.PAGE_START);
        application.add(calButton, BorderLayout.CENTER);

        application.setVisible(true);
    }
}

然后,在CalButton课程中,您创建CalField的另一个实例...

public class CalButton extends JPanel {
    //...
    private CalField calField;

    public CalButton() {
        //...
        calField = new CalField();
        //...
    }

这永远不会添加到任何内容,但是您尝试更新的实例。 CalField的此实例与您对框架的CalField实例无关。

相反,您需要将CalField的实例传递给CalButton

public class CalButton extends JPanel {
    //...
    private CalField calField;

    public CalButton(CalField calField) {
        //...
        this.calField = calField;
        //...
    }

然后在Calculator课程中,您将您创建的CalField实例传递给CalButton的实例...

CalField calField = new CalField();
CalButton calButton = new CalButton(calField);

application.add(calField, BorderLayout.PAGE_START);
application.add(calButton, BorderLayout.CENTER);

这会在屏幕上实际显示的类之间创建一个关系

  

现在,当我点击按钮时,会显示文字,但是当我点击另一个按钮时,它会覆盖上一个文本。如何解决这个问题,以便覆盖不会发生

那是因为setText就是这样,它设置了字段文本,取代之前的字段。

相反,当您拨打setField时,您应该在字段中附加新的下一个字段,例如......

public void setField(String text) {
    field.setText(field.getText() + text);
    System.out.print(text);

}

但我可能会将方法更改为appendField,以便明确它的作用

另外,因为if-else中的大多数actionPerformed语句都没有做任何事情而且他们做的事情几乎是一样的,

if (buttons[0].getActionCommand() == event.getActionCommand()) {
    calField.setField(event.getActionCommand());
} else if (buttons[1].getActionCommand() == event.getActionCommand()) {
    System.out.print(buttons[1].getActionCommand());
} else if (buttons[2].getActionCommand() == ...

你可以做更像......的事情。

calField.setField(event.getActionCommand());

现在,您可能需要添加一些if语句以获取特殊键,例如=和clear,但所有其他数字按钮,只需要执行上述操作,这应该会大大减少重复代码并使代码更容易维护

答案 1 :(得分:-1)

actionPerformed方法中的大多数if子句都无法尝试设置文本。 您所需要的只是:

--summary

甚至:

public void actionPerformed(ActionEvent event) {
    Object src = event.getSource();
    calField.setField( ((JButton)src).getActionCommand());
}