import java.awt.*;
創(chuàng)新互聯(lián)專注于樂業(yè)企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城開發(fā)。樂業(yè)網(wǎng)站建設(shè)公司,為樂業(yè)等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class calculator
{
String str1="0"; //運(yùn)算數(shù)1 初值一定為0 為了程序的安全
String str2="0"; //運(yùn)算數(shù)2
String fh="+"; //運(yùn)算符
String jg="";//結(jié)果
//狀態(tài)開關(guān) 重要
int k1=1;//開關(guān)1 用于選擇輸入方向 將要寫入str2或 str2
int k2=1;//開關(guān)2 符號(hào)鍵 次數(shù) k21說明進(jìn)行的是2+3-9+8 這樣的多符號(hào)運(yùn)算
int k3=1;//開關(guān)3 str1 是否可以被清0 ==1時(shí)可以 !=1時(shí)不能被清0
int k4=1;//開關(guān)4 str2 同上
int k5=1;//開關(guān)5 控制小數(shù)點(diǎn)可否被錄入 ==1時(shí)可以 !=1 輸入的小數(shù)點(diǎn)被丟掉
JButton jicunqi; //寄存器 記錄 是否連續(xù)按下符號(hào)鍵
Vector vt=new Vector(20,10);
JFrame frame=new JFrame("sunshine---計(jì)算器");
JTextField jg_TextField=new JTextField(jg,20);//20列
JButton clear_Button=new JButton("清除");
JButton button0=new JButton("0");
JButton button1=new JButton("1");
JButton button2=new JButton("2");
JButton button3=new JButton("3");
JButton button4=new JButton("4");
JButton button5=new JButton("5");
JButton button6=new JButton("6");
JButton button7=new JButton("7");
JButton button8=new JButton("8");
JButton button9=new JButton("9");
JButton button_Dian=new JButton(".");
JButton button_jia=new JButton("+");
JButton button_jian=new JButton("-");
JButton button_cheng=new JButton("*");
JButton button_chu=new JButton("/");
JButton button_dy=new JButton("=");
public static void main(String[] args)
{
calculator calculator=new calculator();
}
calculator()
{
jg_TextField.setHorizontalAlignment(JTextField.RIGHT );//文本框 右對(duì)齊
JPanel pan=new JPanel();
pan.setLayout(new GridLayout(4,4,5,5));//四行四列 邊距為5像素
pan.add(button7);
pan.add(button8);
pan.add(button9);
pan.add(button_chu);
pan.add(button4);
pan.add(button5);
pan.add(button6);
pan.add(button_cheng);
pan.add(button1);
pan.add(button2);
pan.add(button3);
pan.add(button_jian);
pan.add(button0);
pan.add(button_Dian);
pan.add(button_dy);
pan.add(button_jia);
pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));//pan對(duì)象的邊距
JPanel pan2=new JPanel();
pan2.add(jg_TextField);
JPanel pan3=new JPanel(); //為什么要 多此一句呢? 因?yàn)槲也粫?huì)設(shè)置 按鈕的大小
pan3.setLayout(new FlowLayout());
pan3.add(clear_Button);
//clear_Button.setSize(10,10);//設(shè)置清零按鈕的大小 嗎的 不好使 ??!
frame.setLocation(300, 200); //主窗口 出現(xiàn)在位置
frame.setResizable(false); //不能調(diào)大小
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pan2,BorderLayout.NORTH);
frame.getContentPane().add(pan,BorderLayout.CENTER);
frame.getContentPane().add(pan3,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
//以上是 控件 和 布局
//下面是事件處理 程 序
//--------------- 數(shù) 字 鍵 ----------------
class JianTing implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1;//還原開關(guān)k5狀態(tài)
}
str1=str1+ss;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//顯示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //還原開關(guān)k5狀態(tài)
}
str2=str2+ss;
//k2=2;
k4=k4+1;
///////////////測(cè)試////////////////
jg_TextField.setText(str2);
}
}
}
//--------符 號(hào)-----------
class JianTing_fh implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss2=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k2==1)
{
k1=2;//開關(guān) k1 為1時(shí),向數(shù)1寫 為2時(shí),向數(shù)2寫
k5=1;
fh=ss2;
k2=k2+1;//按符號(hào)鍵的次數(shù)
}
else
{
int a=vt.size();
JButton c=(JButton)vt.get(a-2); if(!(c.getText().equals("+"))!(c.getText().equals("-"))!(c.getText().equals("*"))!(c.getText().equals("/")))
{
yuns();
str1=jg;
k1=2;//開關(guān) k1 為1時(shí),向數(shù)1寫 為2時(shí),向數(shù)2寫
k5=1;
k4=1;
fh=ss2;
} k2=k2+1;
}
}
}
//--------清除-------
class JianTing_clear implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
k5=1;
k2=1;
k1=1;
k3=1;
k4=1;
str1="0";
str2="0";
fh="";
jg="";
jg_TextField.setText(jg);
vt.clear();
}
}
//----------------等 于 ---------------------
class JianTing_dy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
yuns();
k1=1; //還原開關(guān)k1狀態(tài)
//str1=jg;
k2=1;
k3=1;//還原開關(guān)k3狀態(tài)
k4=1; //還原開關(guān)k4狀態(tài)
str1=jg; //為7+5=12 +5=17 這種計(jì)算做準(zhǔn)備
}
}
//----------------小數(shù)點(diǎn) ---------------------
class JianTing_xiaos implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k5==1)
{
String ss2=((JButton)e.getSource()).getText();
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1; //還原開關(guān)k5狀態(tài)
}
str1=str1+ss2;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//顯示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //還原開關(guān)k5狀態(tài)
}
str2=str2+ss2;
//k2=2;
k4=k4+1;
///////////////測(cè)試////////////////
jg_TextField.setText(str2);
}
}
k5=k5+1;
}
}
//注冊(cè) 監(jiān)聽器
JianTing_dy jt_dy=new JianTing_dy();
JianTing jt= new JianTing();//臨聽數(shù)字鍵
JianTing_fh jt_fh= new JianTing_fh();//臨 聽符 號(hào)鍵
JianTing_clear jt_c=new JianTing_clear(); //清除鍵
JianTing_xiaos jt_xs=new JianTing_xiaos();// 小數(shù)點(diǎn) 鍵
button7.addActionListener(jt);
button8.addActionListener(jt);
button9.addActionListener(jt);
button_chu.addActionListener(jt_fh);
button4.addActionListener(jt);
button5.addActionListener(jt);
button6.addActionListener(jt);
button_cheng.addActionListener(jt_fh);
button1.addActionListener(jt);
button2.addActionListener(jt);
button3.addActionListener(jt);
button_jian.addActionListener(jt_fh);
button0.addActionListener(jt);
button_Dian.addActionListener(jt_xs);
button_dy.addActionListener(jt_dy);
button_jia.addActionListener(jt_fh);
clear_Button.addActionListener(jt_c);
//關(guān)閉事件處理程序
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//---------------計(jì) 算------------------
public void yuns()
{
double a2,b2;//運(yùn)算數(shù)1,2
String c=fh;// 運(yùn)算符
double jg2=0 ;//結(jié)果
if (c.equals(""))
{
//System.out.println("請(qǐng)輸入運(yùn)算符");
jg_TextField.setText("請(qǐng)輸入運(yùn)算符");
}
else
{
System.out.println("str1:"+str1);//調(diào)試時(shí) 使 用
System.out.println("str2:"+str2);//調(diào)試時(shí) 使 用
System.out.println("運(yùn)算符:"+fh);//調(diào)試時(shí) 使 用
if (str1.equals(".")) //字符串 "." 轉(zhuǎn)換成double型數(shù)據(jù)時(shí) 會(huì)出錯(cuò) 所以手工轉(zhuǎn)
str1="0.0";
if (str2.equals("."))
str2="0.0";
a2=Double.valueOf(str1).doubleValue();
b2=Double.valueOf(str2).doubleValue();
System.out.println("double型的a2:"+a2); //調(diào)試時(shí) 使 用
System.out.println("double型的b2:"+b2); //調(diào)試時(shí) 使 用
if (c.equals("+"))
{
jg2=a2+b2;
}
if (c.equals("-"))
{
jg2=a2-b2;
}
if (c.equals("*"))
{
jg2=a2*b2;
}
if (c.equals("/"))
{
if(b2==0)
{
jg2=0;//0000000000000 by 0 cu!
}
else
{
jg2=a2/b2;
}
}
System.out.println("double型a2"+fh+"b2結(jié)果:"+jg2);
System.out.println();
jg=((new Double(jg2)).toString());
jg_TextField.setText(jg);
}
}
}
用Socket類去連接
String ip = "192.168.0.57";
int port=7000;
InputStream in;
OutputStream out;
Socket sock = null;
try {
sock = new Socket(ip,port);
sock.setSoTimeout(60*1000);//設(shè)置超時(shí)
this.in = sock.getInputStream();
this.out = sock.getOutputStream();
} catch (Exception e) {
throw new Exception("與終端連接失敗!");
}
下面兩個(gè)可以么,是我做實(shí)驗(yàn)答辯時(shí)用到的!
import?java.awt.*;//AWT核心包
import?java.awt.event.*;//提供事件類和監(jiān)聽器
public?class?Counter?extends?Frame?implements?ActionListener
{
TextField?t=new?TextField("");//文本框
Panel?p1=new?Panel();//new一個(gè)panel,用于存放數(shù)字鍵和符號(hào)鍵。
Panel?p2=new?Panel();//new一個(gè)panel,用于存放開方、平方、和清除鍵。
Button[]?b=new?Button[10];//實(shí)例化Button對(duì)象
Button?bAdd=new?Button("加");
Button?bSub=new?Button("減");
Button?bMul=new?Button("乘以");
Button?bPoint=new?Button(".");
Button?bDiv=new?Button("除以");
Button?bEqual=new?Button("等於");
Button?bSqrt=new?Button("開方");
Button?bPow=new?Button("平方");
Button?bNull=new?Button("清除");
String?str1="";?????//str1和str2存放兩個(gè)輸入的數(shù)
String?str2="";
String?operator=null;???//存放加減乘除以及開平方的符號(hào)
boolean?first=true;????//檢驗(yàn)輸入的是否為第一個(gè)數(shù)
int?countOper=0;????//累計(jì)輸入符號(hào)的個(gè)數(shù),連加連減等操作中會(huì)用到
double?result=0.0;????//暫存結(jié)果
double?num1=0.0,num2=0.0;??//兩個(gè)輸入的數(shù)做運(yùn)算時(shí)轉(zhuǎn)化為double存放
boolean?error=false;???//檢驗(yàn)除數(shù)是否為0
//構(gòu)造方法
public?Counter()
{
Frame?s=new?Frame("計(jì)算器");//創(chuàng)建Frame
for(int?i=0;i10;i++)//利用for循環(huán)將數(shù)字鍵添加進(jìn)p1中
{
b[i]=new?Button(String.valueOf(i));
p1.add(b[i]);
b[i].setActionCommand("number");
b[i].setForeground(new?Color(150,20,20));
b[i].addActionListener(this);//調(diào)用addActionListener()方法注冊(cè)事件監(jiān)聽器
}
p1.add(bPoint);
bPoint.setActionCommand("number");
p1.add(bAdd);?//數(shù)字鍵,符號(hào)鍵放置在panel的p1中
p1.add(bSub);
p1.add(bMul);
p1.add(bDiv);
p1.add(bEqual);
p2.add(bSqrt);//開方鍵,平方鍵,清除鍵放置在panel的p2中
p2.add(bPow);
p2.add(bNull);
bAdd.setActionCommand("oper");
bSub.setActionCommand("oper");
bMul.setActionCommand("oper");
bDiv.setActionCommand("oper");
bAdd.setForeground(Color.red);//為組鍵設(shè)計(jì)顏色
bSub.setForeground(Color.red);
bMul.setForeground(Color.red);
bDiv.setForeground(Color.red);
bPoint.setForeground(Color.black);
bEqual.setForeground(Color.red);
bSqrt.setForeground(Color.blue);
bPow.setForeground(Color.blue);
bNull.setForeground(Color.blue);
bAdd.addActionListener(this);//調(diào)用addActionListener()方法注冊(cè)事件監(jiān)聽器
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bPoint.addActionListener(this);
bEqual.addActionListener(this);
bSqrt.addActionListener(this);
bPow.addActionListener(this);
bNull.addActionListener(this);
p1.setLayout(new?GridLayout(4,4,5,5));//網(wǎng)格布局管理器,把容器根據(jù)行數(shù)和列數(shù)分成同樣大小的單元,
//每個(gè)單元可容納一個(gè)組件,并且此組件會(huì)填滿網(wǎng)格單元,不能控
//制其占據(jù)網(wǎng)格的大小。4、4為網(wǎng)格的行、列數(shù)。5、5為組建之間的
//間距
p2.setLayout(new?FlowLayout());//用FlowLayout布局管理器將組建默認(rèn)劇中排放,默認(rèn)間隙為5個(gè)像素
add(t,"North");??????//frame的north放置輸入框,panel放置在center和south
add(p1,"Center");//將p1添加到Center中
add(p2,"South");//將p2添加到South中
setLocation(400,200);//設(shè)計(jì)按鈕尺寸
setSize(200,200);//設(shè)計(jì)窗口尺寸
setBackground(new?Color(20,200,10));//設(shè)置Frame的背景,默認(rèn)為白色
setVisible(true);//設(shè)置Frame設(shè)置為可見
addWindowListener(new?WindowAdapter(){???//關(guān)閉窗口功能
public?void?windowClosing(WindowEvent?e)
{
System.exit(0);
}
});
}
//實(shí)現(xiàn)接口ActionListener
public?void?actionPerformed(ActionEvent?e)
{
Button?temp=(Button)e.getSource();
if(e.getActionCommand().equals("number"))
{
if(first)
{
str1=str1+temp.getLabel();
t.setText(str1);//將輸入的str1顯示在文本框中
}
else
{
str2=str2+temp.getLabel();
t.setText(str2);//將輸入的str2顯示在文本框中
}
}
else?if(e.getActionCommand().equals("oper"))
{
if(str1=="")????//如果還沒有輸入數(shù)就點(diǎn)擊運(yùn)算符執(zhí)行if
{
countOper=0;//若此,則將計(jì)數(shù)清零
first=true;
}
else
{
countOper++;//計(jì)算輸入符號(hào)的個(gè)數(shù)
if(countOper1)//若輸入的符號(hào)個(gè)數(shù)多余一個(gè),則可以進(jìn)行計(jì)算
{
getResult();
}
operator=temp.getLabel();//存放加減乘除以及開方、平方的符號(hào)
first=false;
}
}
else?if(e.getActionCommand().equals("開方"))
{
double?d=Math.sqrt(Double.parseDouble(str1));
str1=String.valueOf(d);//將計(jì)算出來的結(jié)果再次傳給str1,為連計(jì)算準(zhǔn)備
t.setText(String.valueOf(d));//將計(jì)算出來的結(jié)果傳至文本框中
first=false;//置為false,即已輸入第一個(gè)數(shù)
}
else?if(e.getActionCommand().equals("平方"))
{
double?f=Math.pow(Double.parseDouble(str1),2);
str1=String.valueOf(f);
t.setText(String.valueOf(f));
first=false;
}
else?if(e.getActionCommand().equals("清除"))
{
str1="";//清空
str2="";
t.setText("");//將文本框清空
countOper=0;//將按鍵計(jì)數(shù)器清零
first=true;
error=false;
}
else?if(e.getActionCommand().equals("等於"))
{
if((str1=="")||(str2==""))???//兩個(gè)數(shù)沒有輸全就點(diǎn)擊等號(hào),執(zhí)行if
{
countOper=0;//將按鍵計(jì)數(shù)器清零
first=true;
}
else
{
getResult();
countOper=0;
}
}
}
//運(yùn)算結(jié)果的方法
public?void?getResult()
{
num1=Double.parseDouble(str1);
num2=Double.parseDouble(str2);
if(operator.equals("加"))
{
result=num1+num2;
}
else?if(operator.equals("減"))
{
result=num1-num2;
}
else?if(operator.equals("乘以"))
{
result=num1*num2;
}
else?if(operator.equals("除以"))
{
if(num2==0.0)????//除數(shù)為0的處理方法
{
error=true;
}
else
{
result=num1/num2;
}
}
if(error)
{
t.setText("error");
}
else
{
t.setText(String.valueOf(result));
str1=String.valueOf(result);??//運(yùn)算后把結(jié)果放入str1中,str2清空,為連加連減等操作做準(zhǔn)備
str2="";
}
}
//主方法
public?static?void?main(String[]?args)
{
new?Counter();//創(chuàng)建一個(gè)對(duì)象"計(jì)算器"
}
}
import???java.awt.*;
import???java.awt.event.*;
import???javax.swing.*;
class???CalculatorPanel???extends???JPanel
implements???ActionListener
{?????public???CalculatorPanel()
{?????setLayout(new???BorderLayout());
display???=???new???JTextField("0");
display.setEditable(false);
add(display,???"North");
JPanel???p???=???new???JPanel();
p.setLayout(new???GridLayout(4,???4));
String???buttons???=???"789/456*123-0.=+";
for???(int???i???=???0;???i??????buttons.length();???i++)
addButton(p,???buttons.substring(i,???i???+???1));
add(p,???"Center");
}
private???void???addButton(Container???c,???String???s)
{?????JButton???b???=???new???JButton(s);
c.add(b);
b.addActionListener(this);
}
public???void???actionPerformed(ActionEvent???evt)
{?????String???s???=???evt.getActionCommand();
if???('0'???=???s.charAt(0)??????s.charAt(0)???=???'9'
||???s.equals("."))
{?????if???(start)???display.setText(s);
else???display.setText(display.getText()???+???s);
start???=???false;
}
else
{?????if???(start)
{?????if???(s.equals("-"))
{???display.setText(s);???start???=???false;???}
else???op???=???s;
}
else
{?????calculate(Double.parseDouble(display.getText()));
op???=???s;
start???=???true;
}
}
}
public???void???calculate(double???n)
{?????if???(op.equals("+"))???arg???+=???n;
else???if???(op.equals("-"))???arg???-=???n;
else???if???(op.equals("*"))???arg???*=???n;
else???if???(op.equals("/"))???arg???/=???n;
else???if???(op.equals("="))???arg???=???n;
display.setText(""???+???arg);
}
private???JTextField???display;
private???double???arg???=???0;
private???String???op???=???"=";
private???boolean???start???=???true;
}
public???class???CalculatorApplet???extends???JApplet
{?????public???void???init()
{?????Container???contentPane???=???getContentPane();
contentPane.add(new???CalculatorPanel());
}
}
界面漂亮堪比系統(tǒng)自帶計(jì)算器,功能完美加減乘除開平方等等全部具備,還有清零按鈕,小數(shù)點(diǎn)的使用,連加連乘功能完全參考系統(tǒng)官方計(jì)算器經(jīng)過長(zhǎng)期調(diào)試改進(jìn)而成,馬上拷貝代碼拿去試試看吧,絕不后悔!
代碼如下:
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("計(jì)算器");
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);
}
}
---------------------------------------------------------------------------
=============《按你要求特意后改過的最簡(jiǎn)單功能的代碼如下》========================
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("計(jì)算器");
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.util.Scanner;
/** 給定4個(gè)數(shù)字計(jì)算24 */
public class Core {
private double expressionResult = 24;
// private int maxLine=10;
private boolean error = true;
private double numbers[] = new double[4];
public Object resultReturn;
/**
* 該對(duì)象擁有3個(gè)私有變量 expressionResult,所需結(jié)果 maxLine,輸出結(jié)果每頁行數(shù) error,是否出錯(cuò)
* numbers[4],記錄用來運(yùn)算的4個(gè)數(shù)
*
* 其次,該對(duì)象擁有以下方法供外部調(diào)用 setNumbers(double[] 運(yùn)算的數(shù)) 輸入用來運(yùn)算的數(shù),4個(gè)時(shí)才能計(jì)算,無返回
* setMaxLine(int 行數(shù)) 輸入每頁的行數(shù),無返回 getMaxLine() 返回每頁的行數(shù),類型為int
* setExpressionResult(double 所需結(jié)果) 輸入所需結(jié)果,無返回 getExpressionResult()
* 返回所需結(jié)果,類型為double getExpression() 返回可得出所需結(jié)果的表達(dá)式,類型為字符串?dāng)?shù)組
*
* 最后,私有方法均為計(jì)算與表達(dá)式轉(zhuǎn)換部分
*/
// 測(cè)試使用
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[4];
System.out.print("輸入第一個(gè)數(shù):");
arr[0] = scanner.nextInt();
System.out.print("輸入第二個(gè)數(shù):");
arr[1] = scanner.nextInt();
System.out.print("輸入第三個(gè)數(shù):");
arr[2] = scanner.nextInt();
System.out.print("輸入第四個(gè)數(shù):");
arr[3] = scanner.nextInt();
Core s = new Core();
s.setNumbers(arr);
String[] output = s.getExpression();
for (int i = 0; i output.length; i++) {
System.out.println(output[i]);
}
}
/** 設(shè)定被計(jì)算的四個(gè)數(shù),由于是數(shù)組,所以具有容錯(cuò)功能(不為4個(gè)數(shù)) */
public void setNumbers(double[] n) {
if (n.length == 4) {
error = false;
numbers = n;
} else
error = true;
}
public void setNumbers(int[] n) {
if (n.length == 4) {
error = false;
for (int i = 0; i 4; i++) {
numbers[i] = n[i];
}
} else
error = true;
}
/** 設(shè)定每頁顯示的行數(shù) */
// public void setMaxLine(int n) {
// if (n0) {
// maxLine=n;
// }
// }
// /** 返回每頁顯示的行數(shù) */
// public int getMaxLine() {
// return maxLine;
// }
/** 設(shè)定需要得到的結(jié)果 */
public void setExpressionResult(double n) {
expressionResult = n;
}
/** 返回所需結(jié)果 */
public double expressionResult() {
return expressionResult;
}
/** 返回符合條件的表達(dá)式 */
public String[] getExpression() {
if (!error) {
String[] expression = calculate(numbers);
return expression;
} else
return new String[] { "出錯(cuò)了,輸入有誤" };
}
/** cal24(),輸出結(jié)果為24的表達(dá)式 */
private String[] calculate(double[] n) {
if (n.length != 4)
return new String[] { "Error" };
double[] n1 = new double[3];
double[] n2 = new double[2];
String[] resultString = new String[1024]; // 最多1000組解,暫時(shí)未溢出
int count = 0;
boolean isRepeat = false;
for (int t1 = 0; t1 6; t1++) {
for (int c1 = 0; c1 6; c1++) {
for (int t2 = 0; t2 3; t2++) {
for (int c2 = 0; c2 6; c2++) {
for (int c3 = 0; c3 6; c3++) {
if ((c1 / 3 == c2 / 3 (c1 % 3) * (c2 % 3) != 0)
|| (c2 / 3 == c3 / 3 (c2 % 3) * (c3 % 3) != 0)
|| (c1 / 3 == c3 / 3
(c1 % 3) * (c3 % 3) != 0 t2 == 2)) {
// 去除連減連除的解,因?yàn)閤/(y/z)=x*z/y
continue;
}
n1 = cal1(n, t1, c1);
n2 = cal2(n1, t2, c2);
double result = cal(n2[0], n2[1], c3);
if ((result - expressionResult) 0.00000001
(expressionResult - result) 0.00000001) {
resultString[count] = calString(n, t1, c1, t2,
c2, c3)
+ "=" + (int) expressionResult;
for (int i = 0; i count; i++) {
isRepeat = false;
if (resultString[i]
.equals(resultString[count])) { // 去除完全重復(fù)的解
isRepeat = true;
break; // 提前退出循環(huán)
}
}
if (c1 == c2 c2 == c3 c1 % 3 == 0
t1 + t2 != 0) { // 連加連乘
isRepeat = true;
}
if (!isRepeat) {
count++;
}
}
}
}
}
}
}
if (count == 0)
return new String[] { "該組數(shù)無解" };
String[] resultReturn = new String[count];
System.arraycopy(resultString, 0, resultReturn, 0, count);
return resultReturn;
}
/** cal1(),將4個(gè)數(shù)計(jì)算一次后返回3個(gè)數(shù) */
private double[] cal1(double[] n, int t, int c) { // t為原來的t1,c為原來的c1
double[] m = new double[3];
switch (t) {
case 0:
m[1] = n[2];
m[2] = n[3];
m[0] = cal(n[0], n[1], c);
break;
case 1:
m[1] = n[1];
m[2] = n[3];
m[0] = cal(n[0], n[2], c);
break;
case 2:
m[1] = n[1];
m[2] = n[2];
m[0] = cal(n[0], n[3], c);
break;
case 3:
m[1] = n[0];
m[2] = n[3];
m[0] = cal(n[1], n[2], c);
break;
case 4:
m[1] = n[0];
m[2] = n[2];
m[0] = cal(n[1], n[3], c);
break;
default:
m[1] = n[0];
m[2] = n[1];
m[0] = cal(n[2], n[3], c);
}
return m;
}
/** cal2(),將3個(gè)數(shù)計(jì)算一次后返回2個(gè)數(shù) */
private double[] cal2(double[] n, int t, int c) { // t為原來的t2,c為原來的c2
double[] m = new double[2];
switch (t) {
case 0:
m[1] = n[2];
m[0] = cal(n[0], n[1], c);
break;
case 1:
m[1] = n[1];
m[0] = cal(n[0], n[2], c);
break;
default:
m[1] = n[0];
m[0] = cal(n[1], n[2], c);
}
return m;
}
/** cal(),將2個(gè)數(shù)計(jì)算后返回結(jié)果 */
private double cal(double n1, double n2, int c) { // n1,n2為運(yùn)算數(shù),c為運(yùn)算類型
switch (c) {
case 0:
return n1 + n2;
case 1:
return n1 - n2;
case 2:
return n2 - n1;
case 3:
return n1 * n2;
case 4:
if (n2 == 0)
return 9999; // 使計(jì)算結(jié)果必不為24
else
return n1 / n2;
default:
if (n1 == 0)
return 9999; // 同上
else
return n2 / n1;
}
}
/** calString(),輸出表達(dá)式 */
private String calString(double[] n, int t1, int c1, int t2, int c2, int c3) {
String[] nString = new String[4];
switch (t1) {
case 0:
nString[0] = calString2("" + (int) n[0], "" + (int) n[1], c1);
nString[1] = "" + (int) n[2];
nString[2] = "" + (int) n[3];
break;
case 1:
nString[0] = calString2("" + (int) n[0], "" + (int) n[2], c1);
nString[1] = "" + (int) n[1];
nString[2] = "" + (int) n[3];
break;
case 2:
nString[0] = calString2("" + (int) n[0], "" + (int) n[3], c1);
nString[1] = "" + (int) n[1];
nString[2] = "" + (int) n[2];
break;
case 3:
nString[0] = calString2("" + (int) n[1], "" + (int) n[2], c1);
nString[1] = "" + (int) n[0];
nString[2] = "" + (int) n[3];
break;
case 4:
nString[0] = calString2("" + (int) n[1], "" + (int) n[3], c1);
nString[1] = "" + (int) n[0];
nString[2] = "" + (int) n[2];
break;
default:
nString[0] = calString2("" + (int) n[2], "" + (int) n[3], c1);
nString[1] = "" + (int) n[0];
nString[2] = "" + (int) n[1];
}
if ((c2 / 3 c1 / 3 (t2 != 2 || c2 / 3 == c3 / 3))
|| ((c3 / 3 c1 / 3 + c2 / 3) t2 == 2)
|| (c3 == 1 c1 / 3 == 0)) // 特定情況下加上一個(gè)括號(hào)*****************************
nString[0] = '(' + nString[0] + ')';
switch (t2) {
case 0:
nString[0] = calString2(nString[0], "" + nString[1], c2);
nString[1] = nString[2];
break;
case 1:
nString[0] = calString2(nString[0], nString[2], c2);
break;
default:
nString[3] = nString[0];
nString[0] = calString2(nString[1], nString[2], c2);
nString[1] = nString[3];
}
if (c3 / 3 c2 / 3 || (c3 == 2 nString[0].indexOf('+') = 0)) // 特定情況下加上一個(gè)括號(hào)*****************************
nString[0] = '(' + nString[0] + ')';
return calString2(nString[0], nString[1], c3);
}
/** calString(),根據(jù)符號(hào)輸出一部運(yùn)算表達(dá)式 */
private String calString2(String n1, String n2, int c) {
switch (c) {
case 0:
return n1 + '+' + n2;
case 1:
return n1 + '-' + n2;
case 2:
return n2 + '-' + n1;
case 3:
return n1 + '*' + n2;
case 4:
return n1 + '/' + n2;
default:
return n2 + '/' + n1;
}
}
}
可變參數(shù)列表。一般用在最后一個(gè)參數(shù)上。比如public static void main(String... args),它就可以匹配許多參數(shù)調(diào)用。像main();
main(null);
main(null,null);
main(null,null,null);
......
main("a");
main("a","b");
main("a","b","c");
......
main(new String[]{});這些形狀的調(diào)用都可以。一般在不固定參數(shù)輸入時(shí)使用。比如一個(gè)exe程序執(zhí)行時(shí),你可以帶若干個(gè)參數(shù)之類的。C里面的printf就是一個(gè)這樣的參數(shù),所以你打印時(shí),可以在前面格式字串中用轉(zhuǎn)義字符,后面跟相關(guān)的好幾個(gè)參數(shù)。個(gè)數(shù)不限,可以是一個(gè)也可以是兩個(gè)、三個(gè)……只要跟前面的轉(zhuǎn)義字符匹配即可。