import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalculatorGUI {
private Frame f;
private Button bPoint, bAdd, bDec, bMul, bDiv, bCal;
private TextField tf;
private String s, op;
private Calculator cal = new Calculator();
private boolean ifOp;
public CalculatorGUI() {
f = new Frame("Calculator");
p1 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
tf.setEditable(false);
}
public void launchFrame() {
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
f.setVisible(true);
public void setTextFieldText_Temp() {
(tf.getText().indexOf(".") == -1 || !s.equals("."))) {
tf.setText(tf.getText() + s);
} else {
public void setTextFieldText() {
if (ifOp) {
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
class myWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
class setLabelText_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
s = tempB.getLabel();
setTextFieldText();
class setOperator_ActionListener implements ActionListener {
op = tempB.getLabel();
if (op.equals("+")) {
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
} else if (op.equals("-")) {
tf.setText(cal.opSubtract(tf.getText()));
} else if (op.equals("*")) {
tf.setText(cal.opMultiply(tf.getText()));
} else if (op.equals("/")) {
tf.setText(cal.opDivide(tf.getText()));
} else if (op.equals("=")) {
tf.setText(cal.opEquals(tf.getText()));
class Calculator {
private String result = "0";
private double stringToDouble(String x) {
double y = Double.parseDouble(x);
return y;
private void operate(String x) {
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op) {
case 0:
result = x;
break;
case 1:
result = String.valueOf(y + x1);
result = String.valueOf(y - x1);
result = String.valueOf(y * x1);
if (x1 != 0) {
result = String.valueOf(y / x1);
public String opAdd(String x) {
operate(x);
op = add;
return result;
public String opSubtract(String x) {
op = sub;
public String opMultiply(String x) {
op = mul;
public String opDivide(String x) {
public String opEquals(String x) {
op = 0;
public void opClean() {
result = "0";
import java.awt.*;
import javax.swing.*;public class CaculatorA {
private JFrame jf;
private JButton[] jbs;
private JTextField jtf;
private JButton clear;
private char c;
/**
* 构造方法实例化属性
*
*/
public CaculatorA(){
jf=new JFrame("我的计算器v1.0");
clear=new JButton("clear");
for(int i=0; istr.length(); i++){
jbs[i]=new JButton(str.charAt(i)+"");
init();
addEventHandler();
// setFont();
// setColor();
showMe();
可以通过创建一个圆的类完成计算圆周长和面积的功能.
假设这个圆的类名叫做Circle,因为根据圆的半径就可以求出圆的周长和面积,所以可以在这个类中定义一个半径属性mRadius,然后定义两个方法getLength和getArea分别实现计算圆周长和面积的功能.
java语言源代码如下:
public class Circle{
//圆的半径
private double mRadius;
public Circle(double mRadius){
this.mRadius = mRadius;
//获取圆的周长
public double getLength(){
//获取圆的面积
public double getArea(){
return Math.PI*mRadius*mRadius;
//注:由于测试类只是调用Circle类的方法,功能很简单,便没有写测试类.
import javax.swing.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
String s="",s1;
JFrame jf = new JFrame("小计算器by Graduate") ;
JTextField tf = new JTextField();
public void init()//实现计算器界面
Container c=jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);//文本框
c.add(tf,"North");
JPanel pn1 = new JPanel();//运算界面
//设置按钮
JButton b = new JButton("CLEAR");
b.setToolTipText("请按清除键!");//设置清零键
b.setForeground(Color.RED);//设置字体颜色
b.setBackground(Color.YELLOW);//设置背景色
b.addActionListener(this);
b = new JButton("OFF");
b.setForeground(Color.RED);//字体颜色
b.setBackground(Color.ORANGE);//背景色
b = new JButton("1");//add butten 1
pn1.add(b);
b = new JButton("+");//add butten +
b.setForeground(Color.BLUE);//设置字体颜色
b = new JButton("-");//add button -
b = new JButton("*");//add button *
b = new JButton("0");//add button 0
b = new JButton(".");//add button .
b = new JButton("=");//add button =
b = new JButton("\\");//add button \
jf.setVisible(true);//设置为可视
//处理按钮按下时的动作,进行相应的处理
public void actionPerformed(ActionEvent e)
String command = e.getActionCommand();
tf.setText(tf.getText()+command);
if(command.equals("CLEAR")) //清零键 按下时返回初始状态
s1=null;
s="";
tf.setText("");//记录输入值的变量清空
else if(command.equals("OFF")) System.exit(0);//off键 关闭应用程序
else if(!command.equals("*")!command.equals("\\")
!command.equals("+")!command.equals("-")
!command.equals("="))//判断输入是否为数字
if(s1==null)//判断输入是否为第一个
s1 = command;
else s1+=command;
d1 = new Double(s1).doubleValue();//字符串型转换为双精度型,还原输入数字
try
catch(Exception ex)
tf.setText("Error");//错误显示"Error"
System.out.println(ex.getMessage());
else if(!command.equals("=")) //判断输入是否为+ - * \
s = command;
s1 = null;
else//输入=时,显示运算结果
tf.setText(tf.getText()+d1);
public static void main(String [] args)
new Calculator().init();
代码如下,只是时间仓促有些简陋,没有坐标轴,而且大小比例问题也没有调好.不过功能实现了.嘎嘎,新手上路,腾云驾雾.
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Bbso extends JPanel{
int x,y,x1,y1,m=100;
double d;
public Bbso() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("画曲线");
f.getContentPane().add(this);
public static void main(String arg[]) {
new Bbso();
public void paint(Graphics g) {
super.paintComponent(g);
x1=0;
y1=0;
y=(int)d;
g.drawLine(x1,y1+m,x,y+m);
x1=x;
y1=y;
以上就是土嘎嘎小编为大家整理的java源代码曲线计算相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!