java编写计算器界面
答案:2 悬赏:80 手机版
解决时间 2021-11-20 05:30
- 提问者网友:温旧梦泪无声
- 2021-11-19 19:08
java编写计算器界面
最佳答案
- 五星知识达人网友:雾月
- 2021-11-19 19:16
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MathCalc extends JFrame{
JTextField textDisplay;
public String [] name={"1","2","3","4","5","6","7","8","9","0",".","c","+","-","*","/","1/x","+/-","="};
JButton [] button=new JButton[name.length];
JButton btn;
double Operand1;
double result=0;
boolean ClearDisplay;
String Operator;
public MathCalc(){
textDisplay=new JTextField(20);
for(int i=0;i button[i]=new JButton(name[i]);
for(int i=0;i button[i].setPreferredSize(new Dimension(52,28));
button[18].setPreferredSize(new Dimension(105,28));
layoutComponents(); //加入按钮
//registerEventHander(); //按钮注册事件监听程序
setTitle("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void layoutComponents(){ //布局按钮面板
Container contentPane=getContentPane();
contentPane.setLayout(new GridLayout(5,1));
JPanel TextPane=new JPanel();
TextPane.add(textDisplay);
JPanel buttonPane0 = new JPanel();
JPanel buttonPane1 = new JPanel();
JPanel buttonPane2 = new JPanel();
JPanel buttonPane3 = new JPanel();
for(int i=0;i<=2;i++)
{
buttonPane0.add(button[i]);
buttonPane0.add(button[10]);
buttonPane0.add(button[11]);
}
for(int i=3;i<=5;i++)
{
buttonPane1.add(button[i]);
buttonPane1.add(button[12]);
buttonPane1.add(button[13]);
}
for(int i=6;i<=8;i++)
{
buttonPane2.add(button[i]);
buttonPane2.add(button[14]);
buttonPane2.add(button[15]);
}
buttonPane3.add(button[9]);
buttonPane3.add(button[16]);
buttonPane3.add(button[17]);
buttonPane3.add(button[18]);
contentPane.add(TextPane);
contentPane.add(buttonPane0);
contentPane.add(buttonPane1);
contentPane.add(buttonPane2);
contentPane.add(buttonPane3);
}
public static void main(String args[]){ //main函数
JFrame.setDefaultLookAndFeelDecorated(true);
MathCalc mathcal=new MathCalc();
mathcal.setVisible(true);
}
import javax.swing.*;
import java.awt.event.*;
public class MathCalc extends JFrame{
JTextField textDisplay;
public String [] name={"1","2","3","4","5","6","7","8","9","0",".","c","+","-","*","/","1/x","+/-","="};
JButton [] button=new JButton[name.length];
JButton btn;
double Operand1;
double result=0;
boolean ClearDisplay;
String Operator;
public MathCalc(){
textDisplay=new JTextField(20);
for(int i=0;i
for(int i=0;i
button[18].setPreferredSize(new Dimension(105,28));
layoutComponents(); //加入按钮
//registerEventHander(); //按钮注册事件监听程序
setTitle("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void layoutComponents(){ //布局按钮面板
Container contentPane=getContentPane();
contentPane.setLayout(new GridLayout(5,1));
JPanel TextPane=new JPanel();
TextPane.add(textDisplay);
JPanel buttonPane0 = new JPanel();
JPanel buttonPane1 = new JPanel();
JPanel buttonPane2 = new JPanel();
JPanel buttonPane3 = new JPanel();
for(int i=0;i<=2;i++)
{
buttonPane0.add(button[i]);
buttonPane0.add(button[10]);
buttonPane0.add(button[11]);
}
for(int i=3;i<=5;i++)
{
buttonPane1.add(button[i]);
buttonPane1.add(button[12]);
buttonPane1.add(button[13]);
}
for(int i=6;i<=8;i++)
{
buttonPane2.add(button[i]);
buttonPane2.add(button[14]);
buttonPane2.add(button[15]);
}
buttonPane3.add(button[9]);
buttonPane3.add(button[16]);
buttonPane3.add(button[17]);
buttonPane3.add(button[18]);
contentPane.add(TextPane);
contentPane.add(buttonPane0);
contentPane.add(buttonPane1);
contentPane.add(buttonPane2);
contentPane.add(buttonPane3);
}
public static void main(String args[]){ //main函数
JFrame.setDefaultLookAndFeelDecorated(true);
MathCalc mathcal=new MathCalc();
mathcal.setVisible(true);
}
全部回答
- 1楼网友:十鸦
- 2021-11-19 19:22
具体需求是什么啊
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯