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;
}
}
}
本文題目:java源代碼曲線計算,java畫曲線
分享URL:http://vcdvsql.cn/article44/heoiee.html
成都網站建設公司_創新互聯,為您提供網站設計、定制開發、動態網站、網站制作、做網站、網站內鏈
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯