當前位置:首頁 » 編程語言 » java計算器編寫

java計算器編寫

發布時間: 2023-09-01 10:24:44

⑴ 用java編寫一個計算器

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.GridLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.ImageIcon;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

importjavax.swing.JTextField;

importjavax.swing.SwingConstants;

{

/**

*

*/

=1L;

Resultresult=newResult();//定義text的面板

Number_Keynumber_key=newNumber_Key();//定義按鈕面板

//當點擊按鈕+、-、*、/時,com=true

booleancom=false;

//當i=0時說明是我們第一次輸入,字元串text不會累加

inti=0;

//存放text的內容

Stringtext="";

//存放點擊按鈕+、-、*、/之前的數值

doubledefbutton=0;

//+、-、*、/的代號分別為1,2,3,4

intsymbol=0;

//構造函數

Jisuanqi(){

super("WangJiao");//設定標題

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉窗體時退出程序

JPanelpane=newJPanel();//定義主面板

pane.setLayout(newBorderLayout());

setBounds(380,220,30,80);//前兩個參數是在屏幕上顯示的坐標,後兩個是大小

//替換圖標

ImageIconicon=newImageIcon("F:1.GIF");

//Jisuanqi.class.getResource("APPLE.GIF")

//);

setIconImage(icon.getImage());

pane.add(result,BorderLayout.NORTH);

pane.add(number_key,BorderLayout.CENTER);

pane.add(number_key.equal,BorderLayout.SOUTH);

number_key.one.addActionListener(this);//對1按鈕添加監聽事件

number_key.two.addActionListener(this);//對2按鈕添加監聽事件

number_key.three.addActionListener(this);//對3按鈕添加監聽事件

number_key.four.addActionListener(this);//對4按鈕添加監聽事件

number_key.five.addActionListener(this);//對5按鈕添加監聽事件

number_key.six.addActionListener(this);//對6按鈕添加監聽事件

number_key.seven.addActionListener(this);//對7按鈕添加監聽事件

number_key.eight.addActionListener(this);//對8按鈕添加監聽事件

number_key.nine.addActionListener(this);//對9按鈕添加監聽事件

number_key.zero.addActionListener(this);//對0按鈕添加監聽事件

number_key.ce.addActionListener(this);//對置零按鈕添加監聽事件

number_key.plus.addActionListener(this);//對+按鈕添加監聽事件

number_key.equal.addActionListener(this);//對=按鈕添加監聽事件

number_key.sub.addActionListener(this);//對-按鈕添加監聽事件

number_key.mul.addActionListener(this);//對*按鈕添加監聽事件

number_key.div.addActionListener(this);//對/按鈕添加監聽事件

number_key.point.addActionListener(this);//對.按鈕添加監聽事件

setContentPane(pane);

pack();//初始化窗體大小為正好盛放所有按鈕

}

//各個按鈕觸發的事件

publicvoidactionPerformed(ActionEvente){

/*

*如果是點擊數字按鈕那麼先要判斷是否在此之前點擊了+、-、*、/、=,如果是那麼com=true如果沒有com=

*false;或者是否點擊數字鍵,如果是i=1,如果沒有i=0;

*/

if(e.getSource()==number_key.one){

if(com||i==0){

result.text.setText("1");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"1");

}

}elseif(e.getSource()==number_key.two){

if(com||i==0){

result.text.setText("2");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"2");

}

}elseif(e.getSource()==number_key.three){

if(com||i==0){

result.text.setText("3");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"3");

}

}elseif(e.getSource()==number_key.four){

if(com||i==0){

result.text.setText("4");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"4");

}

}elseif(e.getSource()==number_key.five){

if(com||i==0){

result.text.setText("5");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"5");

}

}elseif(e.getSource()==number_key.six){

if(com||i==0){

result.text.setText("6");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"6");

}

}elseif(e.getSource()==number_key.seven){

if(com||i==0){

result.text.setText("7");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"7");

}

}elseif(e.getSource()==number_key.eight){

if(com||i==0){

result.text.setText("8");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"8");

}

}elseif(e.getSource()==number_key.nine){

if(com||i==0){

result.text.setText("9");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"9");

}

}

/*

*對於0這個按鈕有一定的說法,在我的程序里不會出現如00000這樣的情況,我加了判斷條件就是

*如果text中的數值=0就要判斷在這個數值中是否有.存在?如果有那麼就在原來數值基礎之上添加0;否則保持原來的數值不變

*/

elseif(e.getSource()==number_key.zero){//result.text.getText()是得到text里內容的意思

if(com||i==0){

result.text.setText("0");

com=false;

i=1;

}else{

text=result.text.getText();

if(Float.parseFloat(text)>0||Float.parseFloat(text)<0){//Float.parseFloat(text)就是類型轉換了,下面都是一樣

result.text.setText(text+"0");

}else{

if(text.trim().indexOf(".")==-1){

result.text.setText(text);

}else{

result.text.setText(text+"0");

}

}

}

}elseif(e.getSource()==number_key.ce){

result.text.setText("0");

i=0;

com=true;

//text="";

defbutton=0;

}

/*

*本程序不會讓一個數值中出現2個以上的小數點.具體做法是:判斷是否已經存在.存在就不添加,不存在就添加.

*/

elseif(e.getSource()==number_key.point){

if(com||i==0){

result.text.setText("0.");

com=false;

i=1;

}else{

text=result.text.getText();

if(text.trim().indexOf(".")==-1){

result.text.setText(text+".");

}else{

result.text.setText(text);

}

}

}//獲得點擊+之前的數值

elseif(e.getSource()==number_key.plus){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

symbol=1;

}//獲得點擊-之前的數值

elseif(e.getSource()==number_key.sub){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

symbol=2;

}//獲得點擊*之前的數值

elseif(e.getSource()==number_key.mul){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

System.out.println(defbutton);

symbol=3;

}//獲得點擊/之前的數值

elseif(e.getSource()==number_key.div){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

symbol=4;

}elseif(e.getSource()==number_key.equal){

switch(symbol){

case1:{//計算加法

doublead=defbutton

+Double.parseDouble(result.text.getText());

result.text.setText(ad+"");

i=0;

text="";

break;

}

case2:{//計算減法

doublead=defbutton

-Double.parseDouble(result.text.getText());

result.text.setText(String.valueOf(ad));

i=0;

text="";

break;

}

case3:{//計算乘法

doublead=defbutton

*Double.parseDouble(result.text.getText());

result.text.setText(ad+"");

i=0;

text="";

break;

}

case4:{//計算除法

doublead=defbutton

/Double.parseDouble(result.text.getText());

result.text.setText(ad+"");

i=0;

text="";

break;

}

}

System.out.println(com);

}

System.out.println(result.text.getText());

}

@SuppressWarnings("deprecation")

publicstaticvoidmain(String[]args){

Jisuanqiloveyou=newJisuanqi();

loveyou.show();

}

}

//計算器數字按鈕定義面板

classNumber_KeyextendsJPanel{

/**

*

*/

=1L;

JButtonzero=newJButton("0");//數字鍵0

JButtonone=newJButton("1");//數字鍵1

JButtontwo=newJButton("2");//數字鍵2

JButtonthree=newJButton("3");//數字鍵3

JButtonfour=newJButton("4");//數字鍵4

JButtonfive=newJButton("5");//數字鍵5

JButtonsix=newJButton("6");//數字鍵6

JButtonseven=newJButton("7");//數字鍵7

JButtoneight=newJButton("8");//數字鍵8

JButtonnine=newJButton("9");//數字鍵9

JButtonplus=newJButton("+");

JButtonsub=newJButton("-");

JButtonmul=newJButton("*");

JButtondiv=newJButton("/");

JButtonequal=newJButton("=");

JButtonce=newJButton("清零");//置零鍵

JButtonpoint=newJButton(".");

Number_Key(){

setLayout(newGridLayout(4,4,1,1));//定義布局管理器為網格布局

setBackground(Color.blue);//設置背景顏色

//添加按鈕

add(one);

add(two);

add(three);

add(four);

add(five);

add(six);

add(seven);

add(eight);

add(nine);

add(zero);

add(plus);

add(sub);

add(mul);

add(div);

add(point);

add(equal);

add(ce);

}

}

//計算器顯示結果的窗體

classResultextendsJPanel{

/**

*

*/

=1L;

//text先是輸入和結果

JTextFieldtext=newJTextField("0");

@SuppressWarnings("deprecation")

Result(){//講輸入的數字或得到的結果在text的右邊顯示

text.setHorizontalAlignment(SwingConstants.RIGHT);

text.enable(false);//文本框不能編輯

setLayout(newBorderLayout());//設定布局管理器邊框布局

add(text,BorderLayout.CENTER);//text放置在窗體的中間

}

}

直接復制保存成Jisuanqi.java可以直接運行了

⑵ java編一個計算器的代碼

界面漂亮堪御尺比系統自帶穗拆兆計算器,功能完美加減乘除開平方等等猜租全部具備,還有清零按鈕,小數點的使用,連加連乘功能完全參考系統官方計算器經過長期調試改進而成,馬上拷貝代碼拿去試試看吧,絕不後悔!

代碼如下:

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;i<20;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;i<16;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);
}
}

------------------------------------------------------------

才子_輝祝您愉快!

⑶ 怎麼用JAVA編程編寫一個計算器

  1. 打開IED:打開自己java編程的軟體,採用的是eclipse軟體。

  2. 建立java工程。

  3. 編寫類。

⑷ 用JAVA編寫一個簡單的計算器,要求如下

太麻煩了 說個思路, 過去字元串後 先獲取+-*%的下標.然後然後分割,獲取到一個數組或list
然後循環獲取() 按照數學運算順序拼起來,
然後把公式拆分一步一步操作就得出結果啦

⑸ java設計一個計算器

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class jisuanqi extends Applet implements ActionListener
{
double x=0;
double y=0;
double w;
int h=0;
TextField t=new TextField(15);
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("9");
Button b0=new Button("0");
Button b10=new Button("+");
Button b11=new Button("-");
Button b12=new Button("*");
Button b13=new Button("/");
Button b14=new Button("C");
Button b15=new Button("=");

public void init()
{
setLayout(new BorderLayout());
add(t,"North");
Panel p1=new Panel();
add(p1,"Center");
p1.setLayout(new GridLayout(4,4,5,5));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b14);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b10);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b11);
p1.add(b0);
p1.add(b12);
p1.add(b13);
p1.add(b15);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b0.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
String ts=e.getActionCommand();
char ysf=ts.charAt(0);
if(ts=="C") t.setText("");
else if(ts=="+")
{
x=Integer.parseInt(t.getText());
t.setText("");
h=1;
}
else if(ts=="-")
{
x=Integer.parseInt(t.getText());
t.setText("");
h=2;
}
else if(ts=="*")
{
x=Integer.parseInt(t.getText());
t.setText("");
h=3;
}
else if(ts=="/")
{
x=Integer.parseInt(t.getText());
t.setText("");
h=4;
}
else if(ts.equals("="))
{
y=Integer.parseInt(t.getText());

switch(h)
{
case 1:w = x+y; break;
case 2:w = x-y; break;
case 3:w = x*y; break;
case 4:w = x/y;
}
t.setText(String.valueOf(w));
}
else
{
t.setText(t.getText()+ts);
}

}
}

熱點內容
手機刪除軟體為什麼存儲空間不足 發布:2025-03-13 17:18:37 瀏覽:606
wifi密碼怎麼顯示密碼錯誤 發布:2025-03-13 17:02:21 瀏覽:316
怎麼清理手機微信存儲空間 發布:2025-03-13 16:52:08 瀏覽:798
教學直播源碼 發布:2025-03-13 16:52:06 瀏覽:446
代碼是否能進行編譯 發布:2025-03-13 16:48:12 瀏覽:858
sql2005安裝組件 發布:2025-03-13 16:33:54 瀏覽:231
JavastreamList 發布:2025-03-13 16:32:57 瀏覽:125
多sql 發布:2025-03-13 16:09:04 瀏覽:240
按鍵精靈導入腳本失靈 發布:2025-03-13 15:59:15 瀏覽:548
看雪加密與解密 發布:2025-03-13 15:37:56 瀏覽:550