十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
java计算器源程序
成都创新互联公司是一家专注于成都网站建设、成都网站设计与策划设计,双湖网站建设哪家好?成都创新互联公司做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:双湖等地区。双湖做网站价格咨询:028-86922220
java计算器
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class calculation extends JFrame
{public calculation() /*构造方法*/
{super("计数器");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initTextPanel(); /*文本框*/
initControlPanel(); /*控制键*/
initKeyPanel(); /*数字和运算符*/
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(TextPanel,BorderLayout.NORTH);
pane.add(ControlPanel,BorderLayout.CENTER);
pane.add(KeyPanel,BorderLayout.SOUTH);
pack();
try
{UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception ex)
{;} /*设置Windons观感*/
SwingUtilities.updateComponentTreeUI(this);
setResizable(false);
setVisible(true);
}
private void initTextPanel() /*设置文本框*/
{ TextPanel=new JPanel();
TextPanel.setLayout(new FlowLayout());
Resultarea =new JTextField("0",25);
Resultarea.setEditable(false); /*设置不可编辑*/
Color color=Color.white;
Resultarea.setBackground(color); /*颜色*/
Resultarea.setHorizontalAlignment(JTextField.RIGHT); /*设置显示位置*/
TextPanel.add(Resultarea);
}
private void initControlPanel() /*设置控制键*/
{ControlPanel=new JPanel();
ControlPanel.setLayout(new GridLayout(1,3,4,4));
JButton b1=new JButton("Backspace"); /*Backspace键*/
b1.setFont(font1);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{String s1=Resultarea.getText();
int l=s1.length();
Resultarea.setText(s1.substring(0 ,l-1));
}
});
ControlPanel.add(b1);
JButton b2=new JButton("CE"); /*CE键*/
b2.setFont(font1);
b2.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{Resultarea.setText("0");
isNew=true;
}
});
ControlPanel.add(b2);
JButton b3=new JButton("C"); /*C键*/
b3.setFont(font1);
b3.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{Resultarea.setText("0");
pnum="";
operation="";
isNew=true;
}
});
ControlPanel.add(b3);
}
private void initKeyPanel() /*设置数字键和运算符键*/
{String key[] = {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
KeyPanel = new JPanel();
KeyPanel.setLayout(new GridLayout(4,5,4,4));
for(int i=0;i20;i++)
{String label = key[i];
JButton b = new JButton(label);
b.setActionCommand(key[i]);
b.setFont(font2);
KeyPanel.add(b);
b.addActionListener(new ActionListener() /*无名监听器*/
{public void actionPerformed(ActionEvent e)
{Key_actionPerformed(e);
}
});
}
}
public void Key_actionPerformed(ActionEvent e) /*数字键和运算符键无名监听器*/
{String s=(e.getActionCommand());
String st=Resultarea.getText();
if(s.equals("0")) /*输入数为0*/
{if(st.equals("0"))
return;
else
{if(!isNew)
Resultarea.setText(st+"0");
else
Resultarea.setText("0");
}
isNew=false;
return;
}
if(s.equals("+/-")) /*输入数为+/-*/
{double k=Double.parseDouble(st);
{if(k!=0)
display(-k);
}
return;
}
if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")) /*输入1-9*/
{if(!isNew)
Resultarea.setText(st+s);
else
{ Resultarea.setText(s);
isNew=false;
}
return;
}
if(s.equals(".")) /*输入小数点*/
{if(Resultarea.getText().indexOf(".")==-1) /*不存在小数点*/
{if(isNew)
{Resultarea.setText("0.");
isNew=false;
}
else
Resultarea.setText(st+".");
}
return;
}
isNew=true; /*按下运算符,输入新的数*/
if(s.equals("="))
{compute(s);
operation="";
}
else
{if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) /*二目运算符号*/
{if(operation.equals(""))
{pnum=Resultarea.getText();
operation=s;
}
else
compute(s);
}
else /*一目运算*/
{Count count1=new Count(Double.parseDouble(st));
if(s.equals("sqrt"))
{
display(count1.sqrt());
}
else
{if(s.equals("1/x"))
{if(st.equals("0"))
{Resultarea.setText("除数不能为0.");
operation="";
pnum="";
}
else
display(count1.reciprocal()); /*求倒数*/
}
else
display(Double.parseDouble(st)/100); /*输入%,使当前显示的值除于100*/
}
}
}
}
private void compute(String s)
{if(s.equals("="))
{if(operation.equals(""))
return;
}
double data1=Double.parseDouble(pnum);
double data2=Double.parseDouble(Resultarea.getText());
Count count2=new Count(data1,data2); /*加减乘除计算*/
if(operation.equals("+"))
display((count2.plus()));
else
{if(operation.equals("-"))
display((count2.minus()));
else
{if(operation.equals("*"))
display((count2.multiply()));
else
if(operation.equals("/"))
{if(data2==0)
{Resultarea.setText("除数不能为0");
operation="";
pnum="";
return;
}
else
display((count2.divide()));
}
}
}
operation=""; /*符号为当前符*/
pnum=Resultarea.getText();/*运算数为当前文本数*/
}
public void display(double result) /*显示输出方法*/
{int a=(int)result;
if(a==result)
Resultarea.setText(String.valueOf(a));
else
Resultarea.setText(String.valueOf(result));
if(Resultarea.getText().equals("NaN"))
Resultarea.setText("输入函数无效");
}
private JPanel TextPanel; /*文本框棉板*/
private JTextField Resultarea; /*文本框*/
private JPanel ControlPanel; /*控制键键面板*/
private JPanel KeyPanel; /*数字键和运算符键面板*/
private Font font1=new Font("Dialog",0, 10); /*控制键字体*/
private Font font2 = new Font("Dialog",0,10); /*数字键和符号键字体*/
private String pnum=""; /*前操作数*/
private boolean isNew=true; /*控制是否是新数*/
private String operation=""; /*运算符*/
}
class tester /*测试类*/
{
public static void main(String[] args)
{
new calculation();
}
}
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
在Java中可以使用Math.abs()方法来方便的进行绝对值计算,例如
class test {
public static void main(String[] args) {
System.out.println(Math.abs(-8));
}
}
当然如果自己写的话也非常的简单,可以这样做:
public Integer abs(Integer a){return a0?a:-a;
}
当输入的是正数的时候直接返回即可,当是负数的时候返回它的相反数即可。使用三目运算符可以使用一行代码就能做到。如果需要输入Double或者Float类型的参数的话,代码基本一样。
界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenu menuFile1 = new JMenu();
JMenu menuFile2 = new JMenu();
JMenu menuFile3 = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("文件");
menuFile1.setText("编辑");
menuFile2.setText("查看");
menuFile3.setText("帮助");
menuFileExit.setText("退出");
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CounterFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile1);
menuBar.add(menuFile2);
menuBar.add(menuFile3);
setTitle("计算器");
setJMenuBar(menuBar);
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
for(int i=0;i20;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==17)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==18)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==4)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
text.setText(String.valueOf(Math.sqrt(cq)));
}
});
}
if(i==8)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==19)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
JButton jb1=new JButton("Backspace");
JButton jb2=new JButton(" CE ");
JButton jb3=new JButton(" C ");
this.add(jb1);
this.add(jb2);
this.add(jb3);
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String back=Tool.getinstance().getfield().getText();
text.setText(backmethod(back));
Centercenter.Vec.remove(Centercenter.Vec.size()-1);
}
});
jb3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
text.setText("0.");
Centercenter.Vec.clear();
Centercenter.Vec.add(".");
Centercenter.vc.add("a");
Centercenter.begin="yes";
Centercenter.vc1.clear();
Centercenter.what=null;
Centercenter.tool=0;
}
});
}
public String backmethod(String str)
{
return str.substring(0,str.length()-1);
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
this.setLayout(new GridLayout(4,1,3,3));
this.add(new JButton("MC"));
this.add(new JButton("MR"));
this.add(new JButton("MS"));
this.add(new JButton("M+"));
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
---------------------------------------------------------------------------
=============《按你要求特意后改过的最简单功能的代码如下》========================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter2 {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
setTitle("计算器");
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","4","5","6","*","1","2","3","-","0","=",".","+"};
for(int i=0;i16;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==14)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==11)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==7)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
------------------------------------------------------------
才子_辉祝您愉快!
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Sin extends JFrame{
public static float center_x=0;
public static float center_y=0;
public static JTextField text1;
public static JTextField text2;
public static JTextField text3;
public static double x=0;
public static double y=0;
public Sin(){
final Container con=this.getContentPane();
setLayout(null);
final canvasPanel panel=new canvasPanel();
JLabel lable1=new JLabel("y=asin(bx+c)");
JLabel lable2=new JLabel("a:");
JLabel lable3=new JLabel("b:");
JLabel lable4=new JLabel("c:");
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
JButton button=new JButton("确定");
//center_x=can.getAlignmentX()/2;
//center_y=can.getAlignmentY()/2;
panel.setBounds(55,10,380,220);
lable1.setBounds(100, 200, 200, 100);
lable2.setBounds(100, 270, 50, 30);
lable3.setBounds(200,270, 50, 30);
lable4.setBounds(300, 270, 50,30);
text1.setBounds(140, 270, 50, 30);
text2.setBounds(240, 270, 50,30);
text3.setBounds(340, 270, 50, 30);
button.setBounds(220, 320,90, 30);
con.add(panel);
con.add(lable1);
con.add(lable2);
con.add(lable3);
con.add(lable4);
con.add(text1);
con.add(text2);
con.add(text3);
con.add(button);
setBounds(350,150,500,450);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
panel.repaint();
}
});
}
class canvasPanel extends Canvas{
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
x=0;
String s1,s2,s3;
s1=text1.getText();s2=text2.getText();s3=text3.getText();
for(int i=0;i300;i++){
int a=10,b=20,c=30;
if(!s1.isEmpty()) a=Integer.parseInt(s1);
if(!s2.isEmpty()) b=Integer.parseInt(s2);
if(!s3.isEmpty()) c=Integer.parseInt(s3);
y=5*(a*Math.sin(b*x+c));
double x2=x+5;
double y2=5*(a*Math.sin(b*(x2)+c));
g2.setColor(Color.red);
g2.drawLine((int)x, (int)y+100, (int)x2, (int)y2+100);
x=x2;
}
}
}
public static void main(String[] args){
new Sin().setVisible(true);
}
}
还存在部分小问题,当a,b,c很大的时候图就有问题。。。新手。。。只能做到这样了。。。