十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
加点分,我帮你
在新绛等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、成都做网站 网站设计制作按需开发,公司网站建设,企业网站建设,成都品牌网站建设,成都营销网站建设,外贸网站建设,新绛网站建设费用合理。
算了,我给你写吧
放Ubuntu past了,有效期是1年:
网页链接
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Square {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Square window = new Square();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Square() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 235, 315);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
JLabel label = new JLabel("长:");
label.setBounds(27, 31, 27, 41);
frame.getContentPane().add(label);
textField = new JTextField();
textField.setBounds(88, 39, 86, 24);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("宽:");
label_1.setBounds(27, 85, 27, 41);
frame.getContentPane().add(label_1);
textField_1 = new JTextField();
textField_1.setBounds(88, 93, 86, 24);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JLabel label_2 = new JLabel("面积:");
label_2.setBounds(27, 154, 43, 18);
frame.getContentPane().add(label_2);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(88, 151, 86, 24);
frame.getContentPane().add(textField_2);
JButton button = new JButton("计算");
button.setBounds(46, 213, 113, 27);
frame.getContentPane().add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Double d1 = Double.valueOf(textField.getText());
Double d2 = Double.valueOf(textField_1.getText());
Double d3 = (int) (d1 * d2 * 1000 + 0.5) / 1000.0;
textField_2.setText(d3.toString());
} catch (Exception e2) {
// TODO: handle exception
textField_2.setText("请输入数字!");
}
}
});
}
}
JAVA 计算器程序
Cadev.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.LinkedList;
import java.text.NumberFormat;
import javax.swing.border.*;
public class Cadev extends JFrame implements ActionListener
{
Nbtn nbtn[];
Obtn obtn[];
Button dotbtn,PlusOrMinusbtn,Backspacebtn,
recbtn,equbtn,clbtn;
JPanel panel;
JTextField resultShow;
String Oper[]={"+","-","*","/"};
LinkedList link;
boolean sfcequ=false;
public Cadev()
{
super("计算器");
link=new LinkedList();
nbtn=new Nbtn[10];
for(int i=0;i=9;i++)
{
nbtn[i]=new Nbtn(i);
nbtn[i].addActionListener(this);
}
obtn=new Obtn[4];
for(int i=0;i4;i++)
{
obtn[i]=new Obtn(Oper[i]);
obtn[i].addActionListener(this);
}
dotbtn=new Button(".");
PlusOrMinusbtn=new Button("+/-");
equbtn=new Button("=");
recbtn=new Button("1/x");
Backspacebtn=new Button("退格");
clbtn=new Button("C");
recbtn.setForeground(Color.blue);
dotbtn.setForeground(Color.blue);
PlusOrMinusbtn.setForeground(Color.blue);
equbtn.setForeground(Color.red);
Backspacebtn.setForeground(Color.red);
clbtn.setForeground(Color.red);
dotbtn.addActionListener(this);
PlusOrMinusbtn.addActionListener(this);
equbtn.addActionListener(this);
recbtn.addActionListener(this);
Backspacebtn.addActionListener(this);
clbtn.addActionListener(this);
resultShow=new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setFont(new Font("TimesRoman",Font.PLAIN,14));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setBackground(Color.white);
resultShow.setEditable(false);
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
panel.add(nbtn[1]);
panel.add(nbtn[2]);
panel.add(nbtn[3]);
panel.add(obtn[0]);
panel.add(clbtn);
panel.add(nbtn[4]);
panel.add(nbtn[5]);
panel.add(nbtn[6]);
panel.add(obtn[1]);
panel.add(Backspacebtn);
panel.add(nbtn[7]);
panel.add(nbtn[8]);
panel.add(nbtn[9]);
panel.add(obtn[2]);
panel.add(recbtn);
panel.add(nbtn[0]);
panel.add(PlusOrMinusbtn);
panel.add(dotbtn);
panel.add(obtn[3]);
panel.add(equbtn);
add(panel,BorderLayout.CENTER);
add(resultShow,BorderLayout.NORTH);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,240,180);
setResizable(false);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() instanceof Nbtn)
{
Nbtn b=(Nbtn)e.getSource();
if(link.size()==0)
{
int num=b.getNum();
link.add(""+num);
resultShow.setText(""+num);
sfcequ=false;
}
else if(link.size()==1sfcequ==false)
{
int num=b.getNum();
String Number=(String)link.getFirst();
String s=Number.concat(""+num);
link.set(0,s);
resultShow.setText(s);
}
else if(link.size()==1sfcequ==true)
{
int num=b.getNum();
link.removeFirst();
link.add(""+num);
sfcequ=false;
resultShow.setText(""+num);
}
else if(link.size()==2)
{
int num=b.getNum();
link.add(""+num);
resultShow.setText(""+num);
}
else if(link.size()==3)
{
int num=b.getNum();
String Number=(String)link.getLast();
String s=Number.concat(""+num);
link.set(2,s);
resultShow.setText(s);
}
}
else if(e.getSource() instanceof Obtn)
{
Obtn b=(Obtn)e.getSource();
if(link.size()==1)
{
String sign=b.getOper();
link.add(sign);
}
else if(link.size()==2)
{
String sign=b.getOper();
link.set(1,sign);
}
else if(link.size()==3)
{
String sign=b.getOper();
String num1=(String)link.getFirst();
String num2=(String)link.getLast();
String Oper=(String)link.get(1);
try
{
double n1=Double.parseDouble(num1);
double n2=Double.parseDouble(num2);
double n=0;
if(Oper.equals("+"))
{
n=n1+n2;
}
else if(Oper.equals("-"))
{
n=n1-n2;
}
else if(Oper.equals("*"))
{
n=n1*n2;
}
else if(Oper.equals("/"))
{
n=n1/n2;
}
link.clear();
link.add(""+n);
link.add(sign);
resultShow.setText(""+n);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==equbtn)
{
sfcequ=true;
if(link.size()==1||link.size()==2)
{
String num=(String)link.getFirst();
resultShow.setText(""+num);
}
else if(link.size()==3)
{
String num1=(String)link.getFirst();
String num2=(String)link.getLast();
String sign=(String)link.get(1);
try
{
double n1=Double.parseDouble(num1);
double n2=Double.parseDouble(num2);
double n=0;
if(sign.equals("+"))
{
n=n1+n2;
}
else if(sign.equals("-"))
{
n=n1-n2;
}
else if(sign.equals("*"))
{
n=n1*n2;
}
else if(sign.equals("/"))
{
n=n1/n2;
}
resultShow.setText(""+n);
link.set(0,""+n);
link.removeLast();
link.removeLast();
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==dotbtn)
{
if(link.size()==0)
{
sfcequ=false;
}
else if(link.size()==1)
{
String dot=dotbtn.getLabel();
String num=(String)link.getFirst();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
link.set(0,s);
}
else
{
s=num;
}
link.set(0,s);
resultShow.setText(s);
}
else if(link.size()==3)
{
String dot=dotbtn.getLabel();
String num=(String)link.getLast();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
link.set(2,s);
}
else
{
s=num;
}
resultShow.setText(s);
}
}
else if(e.getSource()==Backspacebtn)
{
if(link.size()==1)
{
String num=(String)link.getFirst();
if(num.length()=1)
{
num=num.substring(0,num.length()-1);
link.set(0,num);
resultShow.setText(num);
}
else
{
link.removeLast();
resultShow.setText("0");
}
}
else if(link.size()==3)
{
String num=(String)link.getLast();
if(num.length()=1)
{
num=num.substring(0,num.length()-1);
link.set(0,num);
resultShow.setText(num);
}
else
{
link.removeLast();
resultShow.setText("0");
}
}
}
else if(e.getSource()==PlusOrMinusbtn)
{
if(link.size()==1)
{
String num1=(String)link.getFirst();
try
{
double d=Double.parseDouble(num1);
d=-1*d;
String str=String.valueOf(d);
link.set(0,str);
resultShow.setText(str);
}
catch(Exception ee)
{
}
}
else if(link.size()==3)
{
String num2=(String)link.getLast();
try
{
double d=Double.parseDouble(num2);
d=-1*d;
String str=String.valueOf(d);
link.set(2,str);
resultShow.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==recbtn)
{
if(link.size()==1||link.size()==2)
{
String num1=(String)link.getFirst();
try
{
double d=Double.parseDouble(num1);
d=1.0/d;
String str=String.valueOf(d);
link.set(0,str);
resultShow.setText(str);
}
catch(Exception ee)
{
}
}
else if(link.size()==3)
{
String num2=(String)link.getLast();
try
{
double d=Double.parseDouble(num2);
d=1.0/d;
String str=String.valueOf(d);
link.set(0,str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==clbtn)
{
sfcequ=false;
resultShow.setText("0");
link.clear();
}
}
public static void main(String agrs[])
{
new Cadev();
}
}
Nbtn.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Nbtn extends Button
{
int Num;
public Nbtn(int n)
{
super(""+n);
Num=n;
setForeground(Color.blue);
}
public int getNum()
{
return Num;
}
}
Obtn.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Obtn extends Button
{
String Oper;
public Obtn(String s)
{
super(s);
Oper=s;
setForeground(Color.red);
}
public String getOper()
{
return Oper;
}
}
package jisuanqi_new;
import java.awt.*;
import java.awt.event.*;
public class JiSuanQi_new implements ActionListener
{
Panel p1;//声明面板p1
TextField t1;//声明文本行t1
String[] label = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//声明标签数组label1存放按钮上的标签
Button[] b;//声明按钮数组存放16个按钮
private int i;//声明i以备后用
private String op1 = "0";//运算数备用
private String operator = "+";//运算符备用
private boolean append = false;//备用
public JiSuanQi_new()//构造方法
{
t1=new TextField();//初始化文本行t1
b = new Button[label.length];//初始化按钮数组b
p1=new Panel();//初始化面板p1
p1.setLayout(new GridLayout(4,4,4,4));//使面板选择网格布局管理器以备储存16个按钮(4行4列)
for(int i=0;ib.length;i++)//利用for循环把标签放在按钮上,使每个按钮添加事件监听器,在面板p1上添加上16个按钮
{
b[i] = new Button(label[i]);//把标签依次放在16个按钮上
b[i].addActionListener(this);//使每个按钮添加动作事件监听器
p1.add(b[i]); //分别将按钮添加到面板p1上
}
Frame f=new Frame("计算器1.0");//初始化窗口f,起名字计算器1.0
f.setLayout(new BorderLayout());//为窗口选择边界布局管理器
f.add(BorderLayout.NORTH,t1);//把文本行他添加到窗口的北部
f.add(BorderLayout.CENTER,p1);//把面吧p1添加到窗口的中间
f.addWindowListener(new WindowAdapter(){//给窗口f添加窗口事件监听器
public void windowClosing(WindowEvent eve){//运行窗口关闭方法
System.exit(0);//退出程序
}
});
f.setSize(250, 250);//设置窗口大小
f.setLocation(200,200);
f.setVisible(true);//显示窗口
}
public static void main(String args[])
{
new JiSuanQi_new(); //调用构造方法
}
public void actionPerformed(ActionEvent ae)
{//按钮被操作发生
String comm = ae.getActionCommand();//返回与此动作相关的命令字符串,即:使用者第一次点击的按钮是什么。
if("0123456789".indexOf(comm)!=-1)//如果相关命令字符串为0~9之间的数字则执行
{
if(append){
String temp = t1.getText();//新数字
t1.setText(temp+comm);
}else{ //因为此时append为false执行这个
t1.setText(comm); //将文本行t1设置为相关命令字符串(你按中的按钮代码)
append = true;//此时append=true若继续按按钮若继续按数字的话1.第一次的按话不会改变2.非第一次按得话会覆盖之前按得数字(即缺点:只能进行单个数字的计算)
}
}
else if(("+-*/".indexOf(comm)!=-1))//如果相关命令字符串为+-*/之间的数字则执行
{
//保存
//t1.setText(comm);
op1 = t1.getText();//把第一个数赋值给op1
operator = comm;//把命令字符串赋值给operator
append = false;//此时append为false即若继续按按钮若按数字的话将重复上面的动作,按符号的话将覆盖之前的符号
}
else if("=".equals(comm))//如果按的是=号,则按条件进行下面的运算
{
String op2 = t1.getText();//op2第二个数
double d1 = Double.parseDouble(op1);
double d2 = Double.parseDouble(op2);
if(operator.equals("+")){
d1 = d1 + d2 ;
}else if(operator.equals("-")){
d1 = d1 - d2;
}else if(operator.equals("*")){
d1 = d1 * d2;
}else {
d1 = d1 / d2;
}
t1.setText(d1+"");//显示计算结果
append = false;
}
else if(".".equals(comm))//若是.号继续按
{
String temp = t1.getText();
if(temp.indexOf(".")==-1){
t1.setText(temp+".");
append = true;
}
}
}
}