我正在尝试使用GUI在Java中创建计算器

时间:2014-06-11 23:52:57

标签: java swing user-interface

所以我正在尝试用Java制作一个计算器而且我已经拥有它,以便当你克服123456789或者。按钮,它会将其添加到文本字段。问题是,一旦我添加了6个字符,它将显示“null”。这是我的代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;


public class Calculator {

private JFrame frame;
private JTextField textField;

//Variables
private double plus;
private double minus;
private double divide;
private double multiply;

//Check for button clicks
private int plusClick;
private int munisClick;
private int multiplyClick;
private int divideClick;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Calculator window = new Calculator();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Calculator() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setResizable(false);
    frame.setBounds(100, 100, 242, 315);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton equalsButton = new JButton("=");
    equalsButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {



        }
    });
    equalsButton.setBounds(185, 202, 41, 74);
    frame.getContentPane().add(equalsButton);

    JButton additionButton = new JButton("+");
    additionButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {



        }
    });
    additionButton.setBounds(185, 163, 41, 38);
    frame.getContentPane().add(additionButton);

    JButton subtractionButton = new JButton("-");
    subtractionButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {



        }
    });
    subtractionButton.setBounds(185, 124, 41, 38);
    frame.getContentPane().add(subtractionButton);

    JButton divideButton = new JButton("/");
    divideButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {



        }
    });
    divideButton.setBounds(185, 86, 41, 38);
    frame.getContentPane().add(divideButton);

    JButton multiplyButton = new JButton("x");
    multiplyButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {



        }
    });
    multiplyButton.setBounds(185, 49, 41, 37);
    frame.getContentPane().add(multiplyButton);

    textField = new JTextField();
    textField.setHorizontalAlignment(SwingConstants.RIGHT);
    textField.setBounds(0, 0, 226, 49);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    final JButton zeroButton = new JButton("0");
    zeroButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText()+zeroButton.getText());

        }
    });
    zeroButton.setBounds(10, 231, 137, 34);
    frame.getContentPane().add(zeroButton);

    final JButton one = new JButton("1");
    one.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + one.getText());

        }
    });
    one.setBounds(10, 193, 41, 34);
    frame.getContentPane().add(one);

    final JButton two = new JButton("2");
    two.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + two.getText());

        }
    });

    two.setBounds(56, 193, 41, 34);
    frame.getContentPane().add(two);

    final JButton three = new JButton("3");
    three.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + three.getText());

        }
    });
    three.setBounds(106, 193, 41, 34);
    frame.getContentPane().add(three);

    final JButton four = new JButton("4");
    four.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + four.getText());

        }
    });
    four.setBounds(10, 153, 41, 34);
    frame.getContentPane().add(four);

    final JButton five = new JButton("5");
    five.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + five.getText());

        }
    });
    five.setBounds(56, 153, 41, 34);
    frame.getContentPane().add(five);

    final JButton six = new JButton("6");
    six.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getSelectedText() + six.getText());

        }
    });
    six.setBounds(106, 153, 41, 34);
    frame.getContentPane().add(six);

    final JButton seven = new JButton("7");
    seven.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + seven.getText());

        }
    });
    seven.setBounds(10, 116, 41, 34);
    frame.getContentPane().add(seven);

    final JButton eight = new JButton("8");
    eight.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + eight.getText());

        }
    });
    eight.setBounds(56, 116, 41, 34);
    frame.getContentPane().add(eight);

    final JButton nine = new JButton("9");
    nine.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText(textField.getText() + nine.getText());

        }
    });
    nine.setBounds(106, 116, 41, 34);
    frame.getContentPane().add(nine);

    JButton clearButton = new JButton("Clear");
    clearButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            textField.setText("");

        }
    });
    clearButton.setBounds(10, 67, 87, 38);
    frame.getContentPane().add(clearButton);

    final JButton decimalButton = new JButton(".");
    decimalButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){

            textField.setText(textField.getText() + decimalButton.getText());

        }
    });
    decimalButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
    decimalButton.setBounds(106, 67, 41, 38);
    frame.getContentPane().add(decimalButton);





    }

}

如何让它显示所有数字而不给我“null”。

谢谢!

2 个答案:

答案 0 :(得分:1)

final JButton six = new JButton("6");
six.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getSelectedText() + six.getText());

    }
});

应该是

final JButton six = new JButton("6");
six.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + six.getText());

    }
});

由于按下按钮时未选择任何文本,它将返回null,然后将其转换为字符串,然后具有" 6"附加到它。

答案 1 :(得分:1)

简单。如果您只是将6按钮与其余按钮进行比较,则会注意到您在ActionListener中执行了不同的操作。更改以下内容:

textField.setText(textField.getSelectedText() + six.getText());

textField.setText(textField.getText() + six.getText());

此外,我高度建议您在使用空布局遇到问题之前通过A Visual Guide to Layout Managers