十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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 Panel p1, p2;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
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();
p2 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
tf = new TextField(25);
tf.setEditable(false);
}
public void launchFrame() {
f.setSize(220, 160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(4, 4));
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.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());
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2, BorderLayout.SOUTH);
f.setVisible(true);
}
public void setTextFieldText_Temp() {
if (tf.getText().length() 15
(tf.getText().indexOf(".") == -1 || !s.equals("."))) {
tf.setText(tf.getText() + s);
} else {
tf.setText((tf.getText() + s).substring(0, 15));
}
}
public void setTextFieldText() {
if (ifOp) {
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
} else {
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 {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
op = tempB.getLabel();
if (op.equals("+")) {
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
} else if (op.equals("-")) {
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
} else if (op.equals("*")) {
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
} else if (op.equals("/")) {
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
} else if (op.equals("=")) {
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
class Calculator {
private String result = "0";
private int op = 0, add = 1, sub = 2, mul = 3, div = 4;
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);
break;
case 2:
result = String.valueOf(y - x1);
break;
case 3:
result = String.valueOf(y * x1);
break;
case 4:
if (x1 != 0) {
result = String.valueOf(y / x1);
} else {
result = "The divisor can't be zero!";
}
break;
}
}
public String opAdd(String x) {
operate(x);
op = add;
return result;
}
public String opSubtract(String x) {
operate(x);
op = sub;
return result;
}
public String opMultiply(String x) {
operate(x);
op = mul;
return result;
}
public String opDivide(String x) {
operate(x);
op = div;
return result;
}
public String opEquals(String x) {
operate(x);
op = 0;
return result;
}
public void opClean() {
op = 0;
result = "0";
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;public class CaculatorA {
private JFrame jf;
private JButton[] jbs;
private JTextField jtf;
private JButton clear;
private double num1,num2,jieguo;
private char c;
/**
* 构造方法实例化属性
*
*/
public CaculatorA(){
jf=new JFrame("我的计算器v1.0");
jtf=new JTextField(20);
clear=new JButton("clear");
jbs=new JButton[16];
String str="123+456-789*0./=";
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(){
return 2*Math.PI*mRadius;
}
//获取圆的面积
public double getArea(){
return Math.PI*mRadius*mRadius;
}
}
//注:由于测试类只是调用Circle类的方法,功能很简单,便没有写测试类。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
String s="",s1;
double d1,d2;
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 pn3 = new JPanel(new BorderLayout());
c.add(pn3,"Center");
JPanel pn2 = new JPanel();//功能键界面(清除键和关闭键)
pn2.setLayout(new BorderLayout());
JPanel pn1 = new JPanel();//运算界面
pn1.setLayout(new GridLayout(4,4));
pn3.add(pn2,"North");
pn3.add(pn1);
//设置按钮
JButton b = new JButton("CLEAR");
b.setToolTipText("请按清除键!");//设置清零键
b.setForeground(Color.RED);//设置字体颜色
b.setBackground(Color.YELLOW);//设置背景色
b.addActionListener(this);
pn2.add(b,"Center");
b = new JButton("OFF");
b.setToolTipText("请按退出键!");//设置off键,点击退出应用程序b.addActionListener(this);
b.setForeground(Color.RED);//字体颜色
b.setBackground(Color.ORANGE);//背景色
pn2.add(b,"East");
b = new JButton("1");//add butten 1
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");//add butten 2
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");//add butten 3
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");//add butten +
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");//add butten 4
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");//add butten 5
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");//add button 6
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");//add button -
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");//add button 7
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");//add button 8
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");//add button 9
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");//add button *
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");//add button 0
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");//add button .
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");//add button =
b.setForeground(Color.RED);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");//add button \
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
jf.setSize(300,300);//设置大小
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
{
if(s.equals("+")) d1 = d1+d2;//加法运算
else if(s.equals("-")) d1 = d2-d1;//减法运算
else if(s.equals("*")) d1 = d1*d2;//乘法运算
else if(s.equals("\\"))d1 = d2/d1;//除法运算
}
catch(Exception ex)
{
tf.setText("Error");//错误显示"Error"
System.out.println(ex.getMessage());
}
}
else if(!command.equals("=")) //判断输入是否为+ - * \
{
s = command;
s1 = null;
d2 = d1;
}
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.setBounds(100,100,500,300);
f.setTitle("画曲线");
f.setVisible(true);
f.getContentPane().add(this);
}
public static void main(String arg[]) {
new Bbso();
}
public void paint(Graphics g) {
super.paintComponent(g);
x1=0;
y1=0;
for(x=-250;x250;x++) {
d=-0.2045*x*x+100.41*x-6736.8; //这里填写公式
y=(int)d;
g.drawLine(x1,y1+m,x,y+m);
x1=x;
y1=y;
}
}
}