Java 当按下按钮时,我的计算器程序只在文本字段中取一位数

Java 当按下按钮时,我的计算器程序只在文本字段中取一位数,java,swing,calculator,Java,Swing,Calculator,当按下按钮时,我的计算器程序只接受文本字段中的一位数。当按下按钮时,如何使其使用多个数字? 它将我按下的原始号码替换为新号码 这是我的代码: import javax.swing.*; import java.awt.*; import java.awt.event.*; class cal implements ActionListener { JFrame frame; JPanel grid,flow; JButton one,two,three,four,five,six,seven

当按下按钮时,我的计算器程序只接受文本字段中的一位数。当按下按钮时,如何使其使用多个数字? 它将我按下的原始号码替换为新号码

这是我的代码:

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

class cal  implements ActionListener
{
JFrame frame;
JPanel grid,flow;
JButton one,two,three,four,five,six,seven,eight,nine,zero,plus,sub,mul,div,equal,clear;
JTextField text;
JLabel em,em1,em2;
int b,c;

public cal()
{
frame=new JFrame("Calculator");     //frame
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(600,600);

flow=new JPanel();          //panel
grid=new JPanel(new GridLayout(5,4));

one=new JButton("1");           //button
one.addActionListener(this);
two=new JButton("2");           //button
two.addActionListener(this);
three=new JButton("3");         //button
three.addActionListener(this);
four=new JButton("4");          //button
four.addActionListener(this);
five=new JButton("5");          //button
five.addActionListener(this);
six=new JButton("6");           //button
six.addActionListener(this);
seven=new JButton("7");         //button
seven.addActionListener(this);
eight=new JButton("8");         //button
eight.addActionListener(this);
nine=new JButton("9");          //button
nine.addActionListener(this);
zero=new JButton("0");          //button
zero.addActionListener(this);
plus=new JButton("+");          //button
plus.addActionListener(this);
sub=new JButton("-");           //button
sub.addActionListener(this);
mul=new JButton("*");           //button
mul.addActionListener(this);
div=new JButton("/");           //button
div.addActionListener(this);
equal=new JButton("=");         //button
equal.addActionListener(this);
clear=new JButton("Clear");         //button
clear.addActionListener(this);

text=new JTextField();      //textfield

em=new JLabel("");          //empty label
em1=new JLabel("");
em2=new JLabel("");

grid.add(text);
grid.add(em);
grid.add(em1);
grid.add(em2);
grid.add(one);
grid.add(two);
grid.add(three);
grid.add(plus);
grid.add(four);
grid.add(five);
grid.add(six);
grid.add(sub);
grid.add(seven);
grid.add(eight);
grid.add(nine);
grid.add(mul);
grid.add(clear);
grid.add(zero);
grid.add(equal);
grid.add(div);

flow.add(grid);
frame.add(flow);
}

public void actionPerformed(ActionEvent a)
{
if(a.getSource()==one)
text.setText("1");

if(a.getSource()==two)
text.setText("2");

if(a.getSource()==three)
text.setText("3");

if(a.getSource()==four)
text.setText("4");

if(a.getSource()==five)
text.setText("5");

if(a.getSource()==six)
text.setText("6");

if(a.getSource()==seven)
text.setText("7");

if(a.getSource()==eight)
text.setText("8");

if(a.getSource()==nine)
text.setText("9");

if(a.getSource()==zero)
text.setText("0");

if(a.getSource()==plus)
{
b=Integer.parseInt(text.getText());
c=1;
text.setText("");
}

if(a.getSource()==sub)
{
b=Integer.parseInt(text.getText());
c=2;
text.setText("");
}

if(a.getSource()==mul)
{
b=Integer.parseInt(text.getText());
c=3;
text.setText("");
}

if(a.getSource()==div)
{
b=Integer.parseInt(text.getText());
c=4;
text.setText("");
}

if(a.getSource()==equal)
{
if(c==1)
{
int x=Integer.parseInt(text.getText());
int y=b+x;
text.setText(Integer.toString(y));
}

if(c==2)
{
int x=Integer.parseInt(text.getText());
int y=b-x;
text.setText(Integer.toString(y));
}

if(c==3)
{
int x=Integer.parseInt(text.getText());
int y=b*x;
text.setText(Integer.toString(y));
}

if(c==4)
{
int x=Integer.parseInt(text.getText());
int y=b/x;
text.setText(Integer.toString(y));
}
}

if(a.getSource()==clear)
{
text.setText("");
b=0;
}
}
public static void main(String[] args)
{
cal a =new cal();
}
}

使用单个数字可以正常工作,但不能使用多个数字。

对于多个数字,您需要将当前文本附加到现有文本中。下面是代码示例

if(a.getSource()==one)
    text.setText(text.getText()+"1");
if(a.getSource()==two)
    text.setText(text.getText()+"2");
......
    String valueText=text.getText();

    if (a.getSource() == one)
        valueText=valueText.concat("1");

    if (a.getSource() == two)
        valueText=valueText.concat("2");

    if (a.getSource() == three)
        valueText=valueText.concat("3");

    if (a.getSource() == four)
        valueText=valueText.concat("4");

    if (a.getSource() == five)
        valueText=valueText.concat("5");

    if (a.getSource() == six)
        valueText=valueText.concat("6");

    if (a.getSource() == seven)
        valueText=valueText.concat("7");

    if (a.getSource() == eight)
        valueText=valueText.concat("8");

    if (a.getSource() == nine)
        valueText=valueText.concat("9");

    if (a.getSource() == zero)
        valueText=valueText.concat("0");

    text.setText(valueText);
setText(…)方法用新文本替换现有文本

下面的示例演示了如何使用JTextField的
replaceSelection(…)
方法

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

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                display.setCaretPosition( display.getDocument().getLength() );
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(50, 50) );
            buttonPanel.add( button );

            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(KeyStroke.getKeyStroke(text), text);
            inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
//      UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );

        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.*;
公共类计算器面板扩展了JPanel
{
专用JTextField显示;
公共计算器面板()
{
Action numberAction=new AbstractAction()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
display.setCaretPosition(display.getDocument().getLength());
display.replaceSelection(例如getActionCommand());
}
};
setLayout(新的BorderLayout());
display=新的JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
添加(显示,BorderLayout.NORTH);
JPanel buttonPanel=新的JPanel();
setLayout(新的GridLayout(0,5));
添加(按钮面板、边框布局、中心);
对于(int i=0;i<10;i++)
{
String text=String.valueOf(i);
JButton按钮=新JButton(文本);
addActionListener(numberAction);
button.setOrder(新行边框(颜色为黑色));
按钮。设置首选尺寸(新尺寸(50,50));
按钮面板。添加(按钮);
InputMap InputMap=button.getInputMap(JComponent.WHEN_IN_FOCUSED_窗口);
inputMap.put(击键.getKeyStroke(文本),文本);
inputMap.put(KeyStroke.getKeyStroke(“NUMPAD”+text),text);
button.getActionMap().put(文本、数字操作);
}
}
私有静态void createAndShowUI()
{
//UIManager.put(“按钮边距”,新插图(10,10,10,10));
JFrame=新的JFrame(“计算器面板”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新计算器面板());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowUI();
}
});
}
}

该示例还显示了如何在不使用嵌套的if/else语句的情况下为所有按钮使用公共ActionListener。

我认为正确配置的
StringBuilder
会工作得更好,更不用说使用
else if
语句会获得更好的性能我同意。而且我认为字符串生成器用于处理大字符串,这就是为什么我把字符串放在这里。
StringBuilder
会更有效