Java 用swing实现简单计算器
发布网友
发布时间:2022-05-06 16:20
我来回答
共2个回答
热心网友
时间:2023-10-11 16:05
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(String args[])
{
new MainFrame("Calculator").setVisible(true);
}
}
class MainFrame extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -8354514836554448949L;
MainFrame(String winname)
{
super(winname);
CalPanel cp = new CalPanel();
this.add(cp);
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class CalPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 5927981350284503795L;
JLabel display = null;
JPanel panel = null;
CalculatorNum num = null;
CalculatorOp op = null;
double result;
String command;
boolean start;
public CalPanel()
{
result = 0;
command = "=";
start = true;
display = new JLabel();
panel = new JPanel();
num = new CalculatorNum();
op = new CalculatorOp();
display.setText("0");
display.setFont(new Font("Times New Roman", Font.BOLD, 30));
display.setHorizontalAlignment(SwingConstants.RIGHT);
this.setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);
panel.setLayout(new GridLayout(4, 4));
addButton("7", num);
addButton("8", num);
addButton("9", num);
addButton("+", op);
addButton("4", num);
addButton("5", num);
addButton("6", num);
addButton("-", op);
addButton("1", num);
addButton("2", num);
addButton("3", num);
addButton("*", op);
addButton("0", num);
addButton(".", num);
addButton("=", op);
addButton("/", op);
add(panel, BorderLayout.CENTER);
}
private void addButton(String name, ActionListener al)
{
JButton jb = new JButton(name);
jb.addActionListener(al);
jb.setFont(new Font("Times New Roman", Font.BOLD, 40));
panel.add(jb);
}
class CalculatorNum implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String in = e.getActionCommand();
if(start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + in);
}
}
class CalculatorOp implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String co = e.getActionCommand();
if(start)
{
if(co == "-")
{
display.setText(co);
start = false;
}
else
{
command = co;
start = true;
}
}
else
{
Calc(Double.parseDouble(display.getText()));
command = co;
start = true;
}
}
private void Calc(double d)
{
switch(command)
{
case "+":
result += d;
break;
case "-":
result -= d;
break;
case "*":
result *= d;
break;
case "/":
result /= d;
break;
case "=":
result = d;
break;
default:
break;
}
display.setText(result + "");
}
}
}
追问这个还能简单一点嘛。。。
热心网友
时间:2023-10-11 16:05
怎么发给你追问直接在这里打或者截图都行
追答一 三零 八八六六 九四六
热心网友
时间:2023-10-11 16:05
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(String args[])
{
new MainFrame("Calculator").setVisible(true);
}
}
class MainFrame extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -8354514836554448949L;
MainFrame(String winname)
{
super(winname);
CalPanel cp = new CalPanel();
this.add(cp);
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class CalPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 5927981350284503795L;
JLabel display = null;
JPanel panel = null;
CalculatorNum num = null;
CalculatorOp op = null;
double result;
String command;
boolean start;
public CalPanel()
{
result = 0;
command = "=";
start = true;
display = new JLabel();
panel = new JPanel();
num = new CalculatorNum();
op = new CalculatorOp();
display.setText("0");
display.setFont(new Font("Times New Roman", Font.BOLD, 30));
display.setHorizontalAlignment(SwingConstants.RIGHT);
this.setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);
panel.setLayout(new GridLayout(4, 4));
addButton("7", num);
addButton("8", num);
addButton("9", num);
addButton("+", op);
addButton("4", num);
addButton("5", num);
addButton("6", num);
addButton("-", op);
addButton("1", num);
addButton("2", num);
addButton("3", num);
addButton("*", op);
addButton("0", num);
addButton(".", num);
addButton("=", op);
addButton("/", op);
add(panel, BorderLayout.CENTER);
}
private void addButton(String name, ActionListener al)
{
JButton jb = new JButton(name);
jb.addActionListener(al);
jb.setFont(new Font("Times New Roman", Font.BOLD, 40));
panel.add(jb);
}
class CalculatorNum implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String in = e.getActionCommand();
if(start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + in);
}
}
class CalculatorOp implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String co = e.getActionCommand();
if(start)
{
if(co == "-")
{
display.setText(co);
start = false;
}
else
{
command = co;
start = true;
}
}
else
{
Calc(Double.parseDouble(display.getText()));
command = co;
start = true;
}
}
private void Calc(double d)
{
switch(command)
{
case "+":
result += d;
break;
case "-":
result -= d;
break;
case "*":
result *= d;
break;
case "/":
result /= d;
break;
case "=":
result = d;
break;
default:
break;
}
display.setText(result + "");
}
}
}
追问这个还能简单一点嘛。。。
热心网友
时间:2023-10-11 16:05
怎么发给你追问直接在这里打或者截图都行
追答一 三零 八八六六 九四六