java设计简易计算器窗口(怎样用JAVA语言编写一个简单的计算器界面)

本文目录
- 怎样用JAVA语言编写一个简单的计算器界面
- 用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算
- java、编写一个简易计算器界面,如图1所示,不需要添加任何事件
- java实现一个简单的计算器界面,加上适当的事件处理,完成计算功能
- JAVA图形用户界面设计一个简易计算器
- 用JAVA编写一个简单计算器界面做出来了
- 用Java编写一个简单的计算器界面,并且可以实现加减乘除计算,很简单一个就可以了,还需要一个归零按
- 如何使用java编写一个简易计算器面板
- 用Java设计一个简单的计算器
怎样用JAVA语言编写一个简单的计算器界面
import java.awt.FlowLayout;
import java.awt.TextArea;
import javax.swing.*;
public class MyFrame extends JFrame {
public static void main(String args) {
new MyFrame();
}
public MyFrame() {
用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算
下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
package aaa;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YunSuan implements ActionListener,ItemListener
{
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;
public static String fuhao=““;
public YunSuan()
{
jf=new JFrame(“加减乘除运算“);
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton(“=“);
}
public void Jiemian()
{
c.add(jtf1);
c2.addItemListener(this);
c2.add(““);
c2.add(“+“);
c2.add(“-“);
c2.add(“*“);
c2.add(“/“);
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);
jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
public static void main(String args)
{
YunSuan ys=new YunSuan();
ys.Jiemian();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals(“+“))
{
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals(“-“))
{
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals(“*“))
{
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals(“/“))
{
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c2)
{
String str1=c2.getSelectedItem();
fanhui(str1);
}
}
public String fanhui(String str2)
{
return fuhao=str2;
}
}
java、编写一个简易计算器界面,如图1所示,不需要添加任何事件
1、新建一个易语言的Windows窗口程序。
2、举一个简单的例子,做一个加法计算器。添加三个框、两个文本和一个按钮,界面如下:
3、此时会用到两个局部变量,定义为变量1、变量2,按CTRL+L弹出局部变量表(或点菜单栏上的插入,选择局部变量),如图。
4、点击变量名下面的框,输入变量1,点击类型,按下空格键,弹出如下菜单:
选择小数型。同样,添加变量2。
5、同样,全局变量也一样,只不过快捷键为CTRL+G,注意的是,要在窗口程序集里面插入变量。
java实现一个简单的计算器界面,加上适当的事件处理,完成计算功能
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
// public static final int EXIT_ON_CLOSE = 3;
private Button plus=new Button(“+“); //运算符
private Button minus=new Button(“-“);
private Button multip=new Button(“*“);
private Button divise=new Button(“/“);
private Panel pan=new Panel();
private JLabel opl1 = new JLabel(“操作数1“);
private JLabel opl2 = new JLabel(“操作数2“);
private JLabel resl = new JLabel(“结果:“);
private TextField op1=new TextField(10);
private TextField op2=new TextField(10);
private TextField result=new TextField(10);
public Calculator ()
{
super(“Calculator“);
initialize();
}
public void initialize()
{
this.setLayout(null);
this.setSize(360,300);
this.setResizable(false);
this.add(pan);
pan.setBounds(10,10,330,250);
pan.setLayout(new GridLayout(5,1));
pan.add(opl1);
// opl1.setBounds(10,10, 100, 40);
pan.add(op1);
// op1.setBounds(120,10,100,40);
pan.add(opl2);
// opl2.setBounds(21,0,100,40);
pan.add(op2);
// op2.setBounds(21,110 , 100, 40);
pan.add(resl);
// resl.setBounds(42,0 , 100, 40);
pan.add(result);
// result.setBounds(42,100,100,40);
plus.addActionListener(this);
minus.addActionListener(this);
multip.addActionListener(this);
divise.addActionListener(this);
pan.add(plus);
pan.add(minus);
pan.add(multip);
pan.add(divise);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==plus)
{
if(op1.getText().equals(““)||op2.getText().equals(““))
result.setText(“操作数不完整“);
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1+oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==minus)
{
if(op1.getText().equals(““)||op2.getText().equals(““))
result.setText(“操作数不完整“);
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1-oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()== multip)
{
if(op1.getText().equals(““)||op2.getText().equals(““))
result.setText(“操作数不完整“);
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1*oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==divise)
{
if(op1.getText().equals(““)||op2.getText().equals(““))
result.setText(“操作数不完整“);
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1/oprt2;
result.setText(temp.toString());
}
}
}
public static void main(String args) {
// TODO Auto-generated method stub
new Calculator();
}
}
JAVA图形用户界面设计一个简易计算器
下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
package aaa;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YunSuan implements ActionListener,ItemListener
{
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;
public static String fuhao=““;
public YunSuan()
{
jf=new JFrame(“加减乘除运算“);
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton(“=“);
}
public void Jiemian()
{
c.add(jtf1);
c2.addItemListener(this);
c2.add(““);
c2.add(“+“);
c2.add(“-“);
c2.add(“*“);
c2.add(“/“);
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);
jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
public static void main(String args)
{
YunSuan ys=new YunSuan();
ys.Jiemian();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals(“+“))
{
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals(“-“))
{
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals(“*“))
{
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals(“/“))
{
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c2)
{
String str1=c2.getSelectedItem();
fanhui(str1);
}
}
public String fanhui(String str2)
{
return fuhao=str2;
}
}
用JAVA编写一个简单计算器界面做出来了
import sun.plugin.security.Broken11ClassFixer;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class Test extends JFrame{
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JButton b1;
public Test(){
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT,4,3));
t1 = new JTextField(“0“,4);
t2 = new JTextField(“0“,4);
t3 = new JTextField(8);
b1 = new JButton(“=“);
p1.add(t1);
p1.add(new JTextField(“ + “));
p1.add(t2);
p1.add(new JLabel(“=“));
p1.add(t3);
p2.add(b1,BorderLayout.CENTER);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t3.setText(Integer.toString(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText())));
}
});
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}
public static void main(String args){
Test frame=new Test();
frame.setTitle(“计算器“);
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
帮你完善了下,其他来不及写了都是一样的,稍微改一下就行
用Java编写一个简单的计算器界面,并且可以实现加减乘除计算,很简单一个就可以了,还需要一个归零按
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Calculator {
public static void main(String args) {
Frame f = new Frame(“Calculator“);
final TextField tf = new TextField();
Panel p = new Panel();
f.setLayout(new BorderLayout());
f.add(tf, BorderLayout.NORTH);
f.add(p, BorderLayout.CENTER);
p.setLayout(new GridLayout(4, 4));
Button one = new Button(“1“);
Button two = new Button(“2“);
Button three = new Button(“3“);
Button four = new Button(“4“);
Button five = new Button(“5“);
Button six = new Button(“6“);
Button seven = new Button(“7“);
Button eight = new Button(“8“);
Button nine = new Button(“9“);
Button zero = new Button(“0“);
Button add = new Button(“+“);
Button sub = new Button(“-“);
Button mul = new Button(“*“);
Button div = new Button(“/“);
Button decimal = new Button(“.“);
final Button equal = new Button(“=“);
p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
p.add(six);
p.add(seven);
p.add(eight);
p.add(nine);
p.add(zero);
p.add(add);
p.add(sub);
p.add(mul);
p.add(div);
p.add(decimal);
p.add(equal);
final String ary = {““, null, ““};
class NumMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
String num = ((Button)e.getSource()).getLabel().trim();
if(ary == null){
ary = ary + ““ + num;
}else{
ary = ary + ““ + num;
}
tf.setText(tf.getText() + ““ + num);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
one.addMouseListener(new NumMouseListener());
two.addMouseListener(new NumMouseListener());
three.addMouseListener(new NumMouseListener());
four.addMouseListener(new NumMouseListener());
five.addMouseListener(new NumMouseListener());
six.addMouseListener(new NumMouseListener());
seven.addMouseListener(new NumMouseListener());
eight.addMouseListener(new NumMouseListener());
nine.addMouseListener(new NumMouseListener());
zero.addMouseListener(new NumMouseListener());
class OperatotrMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == equal){
try {
long result = calculate(Integer.parseInt(ary), Integer.parseInt(ary), ary);
tf.setText(String.valueOf(result));
ary = tf.getText();
ary = null;
ary = ““;
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
ary = ((Button) e.getSource()).getLabel();
if(ary.length() 》 0){
ary = ““;
}else{
ary = String.valueOf(tf.getText());
}
tf.setText(““);
}
}
private long calculate(int i, int j, String operator) throws Exception {
if(operator.equals(“+“)){
return i + j;
}else if(operator.equals(“-“)){
return i - j;
}else if(operator.equals(“*“)){
return i * j;
}else if(operator.equals(“/“)){
return i / j;
}else{
throw new Exception(“Invalid operator.(Only +-/* accepted“);
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
add.addMouseListener(new OperatotrMouseListener());
mul.addMouseListener(new OperatotrMouseListener());
div.addMouseListener(new OperatotrMouseListener());
sub.addMouseListener(new OperatotrMouseListener());
equal.addMouseListener(new OperatotrMouseListener());
f.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});
f.setVisible(true);
f.setBounds(200, 200, 250, 250);
f.validate();
}
}
如何使用java编写一个简易计算器面板
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.JPanel;
import javax.swing.JTextField;
public class Calculator2 extends JPanel{
String lables = {
{“CE“,“C“,“Back“,“%“},
{“7“,“8“,“9“,“/“},
{“4“,“5“,“6“,“*“},
{“1“,“2“,“3“,“+“},
{“0“,“.“,“=“,“-“}
};
JTextField display;
String commands = “CEBack%/*+-=“;
private double result;
private String lastCommand;
private boolean start;
public static void main(String args) {
JFrame f = new JFrame(“Test“);
f.setContentPane(new Calculator2());
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(3);
}
public Calculator2(){
result = 0;
lastCommand = “=“;
start = true;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setLayout(layout);
gbc.insets = new Insets(1, 1, 1, 1);
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
Dimension dimension = new Dimension(70, 30);
gbc.gridwidth = 4;
display = new JTextField();
display.setPreferredSize(dimension);
display.setHorizontalAlignment(JTextField.RIGHT);
this.add(display, gbc);
ActionListener insert = new InsertAction();
gbc.gridwidth = 1;
for (int i = 0; i 《 lables.length; i++) {
for (int j = 0; j 《 lables.length; j++) {
gbc.gridx = j;
gbc.gridy = i+1;
JButton b = new JButton(lables[i][j]);
if (j==3)
b.setForeground(Color.RED);
b.setPreferredSize(dimension);
b.addActionListener(insert);
this.add(b,gbc);
}
}
}
private class InsertAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if(commands.contains(input)){
if(start){
if(input.equals(“-“)){
display.setText(input);
start =false;
}
} else {
calculate(Double.parseDouble(display.getText()));
lastCommand = input;
start = true;
}
} else {
if(start){
display.setText(““);
start = false;
}
display.setText(display.getText() + input);
}
}
}
private void calculate(double x) {
if (lastCommand.equals(“+“)) result +=x;
else if (lastCommand.equals(“-“)) result -=x;
else if (lastCommand.equals(“*“)) result *=x;
else if (lastCommand.equals(“/“)) result /=x;
else if (lastCommand.equals(“=“)) result =x;
display.setText(““ + result);
}
}
用Java设计一个简单的计算器
无聊写了个,修复了下Bug:
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.JTextField;
public class Calculate extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton plus, reduce, multiply, divice, reset;
private JTextField one, two, result;
private boolean device_falg = false;
private final int width = 400, height = 300;
public Calculate() {
super(“修改密码“);
this.setLayout(null);
this.setSize(width, height);
init();
Layout();
}
public void init() {
plus = new JButton(“加 “);
reduce = new JButton(“减 “);
multiply = new JButton(“乘 “);
divice = new JButton(“除 “);
reset = new JButton(“清空“);
one = new JTextField();
two = new JTextField();
result = new JTextField();
}
public void Layout() {
this.add(new JLabel(“第一个数“)).setBounds(20, 10, 60, 80);
this.add(one).setBounds(100, 38, 100, 25);
this.add(new JLabel(“第二个数“)).setBounds(20, 40, 60, 80);
this.add(two).setBounds(100, 70, 100, 25);
this.add(new JLabel(“结果“)).setBounds(20, 85, 60, 80);
this.add(result).setBounds(100, 110, 100, 25);
this.add(plus).setBounds(70, 170, 80, 25);
this.add(reduce).setBounds(200, 170, 80, 25);
this.add(multiply).setBounds(70, 200, 80, 25);
this.add(divice).setBounds(200, 200, 80, 25);
this.add(reset).setBounds(300, 220, 80, 25);
plus.addActionListener(this);
reduce.addActionListener(this);
multiply.addActionListener(this);
divice.addActionListener(this);
reset.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public boolean Format() {
boolean FLAG = false;
boolean flag = false;
String one = this.one.getText().toString().trim();
String two = this.two.getText().toString().trim();
if (one == null || one.equals(““) || two == null || two.equals(““)) {
JOptionPane.showMessageDialog(getParent(), “请输入完整信息!“);
FLAG = false;
flag = true;
}
boolean boll_1 = one.matches(“[\\d]{1,100}“);
boolean boll_2 = two.matches(“[\\d]{1,100}“);
boolean boll_3 = one.matches(“[\\d]{1,100}+[.]+[\\d]{1,100}“);
boolean boll_4 = two.matches(“[\\d]{1,100}+[.]+[\\d]{1,100}“);
if (flag) {
return false;
}
if ((boll_1 && boll_2) || (boll_3 && boll_4) || (boll_1 && boll_4)
|| (boll_3 && boll_2)) {
FLAG = true;
} else {
JOptionPane.showMessageDialog(getParent(), “请输入数字!“);
FLAG = false;
}
if (FLAG && device_falg) {
if (Double.parseDouble(two) == 0) {
JOptionPane.showMessageDialog(getParent(), “被除数不能为0!“);
FLAG = false;
device_falg=false;
}
}
return FLAG;
}
public double Plus(double one, double two) {
return one + two;
}
public double Multiply(double one, double two) {
return one * two;
}
public double Divice(double one, double two) {
return one / two;
}
public double Reduce(double one, double two) {
return one - two;
}
public void Clear() {
one.setText(““);
two.setText(““);
result.setText(““);
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o == reset) {
Clear();
return;
}
if (o == divice) {
device_falg = true;
}
if (!Format()) {
return;
}
double one = Double.parseDouble(this.one.getText());
double two = Double.parseDouble(this.two.getText());
double result = 0;
if (o == plus) {
result = Plus(one, two);
} else if (o == reduce) {
result = Reduce(one, two);
} else if (o == multiply) {
result = Multiply(one, two);
} else if (o == divice) {
result = Divice(one, two);
}
this.result.setText(““ + result);
}
public static void main(String args) {
new Calculate();
}
}

更多文章:
dropdownlist 绑定(DropDownList 绑定所有项 并 显示指定项)
2026年10月11日 08:50
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00






