求一份java计算器实验报告

2025-12-15 14:26:38
推荐回答(1个)
回答1:

呵呵很简单的...就一个class 就可以搞定...
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class jsq extends JFrame implements ActionListener
{
JButton a, b, c, d, e, g, n;
String gg;
JButton f[] = new JButton[10];
JTextField text;
JPanel p1, p2;

jsq()
{
p1 = new JPanel();
p2 = new JPanel();
p2.setLayout(new GridLayout(4, 5, 5, 5));
text = new JTextField(20);
a = new JButton("+");
b = new JButton("-");
c = new JButton("/");
d = new JButton("*");
g = new JButton("=");
n = new JButton("AC");
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
g.addActionListener(this);
n.addActionListener(this);
for (int i = 0; i < 10; i++)
{
f[i] = new JButton("" + i);
p2.add(f[i]);
f[i].addActionListener(this);
}
p1.add(text);

p2.add(a);
p2.add(b);
p2.add(c);
p2.add(d);
p2.add(g);
p2.add(n);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
setVisible(true);
setBounds(300, 300, 300, 300);
validate();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() != g)
{
for (int i = 0; i < 10; i++)
{
if (e.getSource() == f[i])
{
gg = text.getText();
text.setText(gg+i);
}
}
}
if(e.getSource()==a)
{

text.setText("+");
}
if(e.getSource()==b)
{

text.setText("-");
}
if(e.getSource()==c)
{

text.setText("*");
}
if(e.getSource()==d)
{

text.setText("/");
}
if(e.getSource()==g)
{
gg=text.getText();

if(gg.equals(""))
{
System.out.print("dd");
}
System.out.print(gg);

}
if (e.getSource() == n)
text.setText(null);

}
}

public class jisuanqu
{
public static void main(String[] args)
{
new jsq();
}
}

这个还要思路??实现步骤??
流程图: