java设计简易计算器窗口(java设计简易计算器窗口代码)
本篇文章给大家谈谈java设计简易计算器窗口,以及java设计简易计算器窗口代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、用java语言,编写一个计算器
- 2、java、编写一个简易计算器界面,如图1所示,不需要添加任何事件。
- 3、用java编写一个简单计算器
- 4、java编写程序:创建一个 计算器 的 界面,包含 0-9 个 数字 , A-F个字母,采用 GridLayout布局
用java语言,编写一个计算器
有一个现成的.你看看吧
import java.awt.event.ActionEvent;
public class Application extends JFrame {
protected String str = "";
protected boolean isChar = true;
protected boolean isEqual = false;
protected JTextField textField;
public Application() {
Listener listerner = new Listener(this);
getContentPane().setLayout(null);
JButton button = new JButton("7");
button.addActionListener(listerner);
button.setBounds(12, 69, 43, 27);
getContentPane().add(button);
textField = new JTextField();
textField.setText("0");
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBounds(12, 22, 377, 27);
getContentPane().add(textField);
textField.setColumns(10);
JButton button_1 = new JButton("8");
button_1.addActionListener(listerner);
button_1.setBounds(103, 69, 43, 27);
getContentPane().add(button_1);
JButton button_2 = new JButton("9");
button_2.addActionListener(listerner);
button_2.setBounds(182, 69, 43, 27);
getContentPane().add(button_2);
JButton button_3 = new JButton("4");
button_3.addActionListener(listerner);
button_3.setBounds(12, 106, 43, 27);
getContentPane().add(button_3);
JButton button_4 = new JButton("5");
button_4.addActionListener(listerner);
button_4.setBounds(103, 106, 43, 27);
getContentPane().add(button_4);
JButton button_5 = new JButton("6");
button_5.addActionListener(listerner);
button_5.setBounds(182, 106, 43, 27);
getContentPane().add(button_5);
JButton button_6 = new JButton("1");
button_6.addActionListener(listerner);
button_6.setBounds(12, 143, 43, 27);
getContentPane().add(button_6);
JButton button_7 = new JButton("2");
button_7.addActionListener(listerner);
button_7.setBounds(103, 143, 43, 27);
getContentPane().add(button_7);
JButton button_8 = new JButton("3");
button_8.addActionListener(listerner);
button_8.setBounds(182, 143, 43, 27);
getContentPane().add(button_8);
JButton button_9 = new JButton("+");
button_9.addActionListener(listerner);
button_9.setBounds(269, 72, 43, 27);
getContentPane().add(button_9);
JButton button_10 = new JButton("-");
button_10.addActionListener(listerner);
button_10.setBounds(346, 72, 43, 27);
getContentPane().add(button_10);
JButton button_11 = new JButton("*");
button_11.addActionListener(listerner);
button_11.setBounds(269, 109, 43, 27);
getContentPane().add(button_11);
JButton button_12 = new JButton("/");
button_12.addActionListener(listerner);
button_12.setBounds(346, 109, 43, 27);
getContentPane().add(button_12);
JButton button_13 = new JButton("=");
button_13.addActionListener(listerner);
button_13.setBounds(346, 143, 43, 27);
getContentPane().add(button_13);
JButton button_14 = new JButton("0");
button_14.addActionListener(listerner);
button_14.setBounds(103, 180, 43, 27);
getContentPane().add(button_14);
JButton btnReset = new JButton("reset");
btnReset.addActionListener(listerner);
btnReset.setBounds(269, 180, 118, 27);
getContentPane().add(btnReset);
JButton button_15 = new JButton(".");
button_15.addActionListener(listerner);
button_15.setBounds(269, 146, 43, 27);
getContentPane().add(button_15);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(442, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Application();
}
}
class Listener implements ActionListener {
private Application app = null;
public Listener(Application app) {
this.app = app;
}
public void actionPerformed(ActionEvent e) {
String value = e.getActionCommand();
if (value.matches("[0-9.]")) {
if (app.isChar) {
app.textField.setText("");
app.isChar = false;
}
if (app.isEqual app.str.matches("[0-9.]*")) {
app.str = "";
app.isEqual = false;
}
app.str += value;
app.textField.setText(app.textField.getText() + value);
} else if (value.matches("[\\+\\-\\*/]")) {
if (!app.str.substring(app.str.length() - 1)
.matches("[\\+\\-\\*/]")) {
app.str += value;
app.isChar = true;
}
} else if ("=".equals(value)) {
app.isEqual = true;
if (app.str.substring(app.str.length() - 1).matches("[\\+\\-]")) {
app.str += "0";
} else if (app.str.substring(app.str.length() - 1)
.matches("[\\*/]")) {
app.str += "1";
}
Interpreter bsh = new Interpreter();
String obj = null;
try {
obj = bsh.eval(app.str).toString();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
System.out.println(app.str);
app.textField.setText(obj);
app.str = obj;
app.isChar = true;
} else {
app.str = "";
app.textField.setText("0");
}
}
}
java、编写一个简易计算器界面,如图1所示,不需要添加任何事件。
1、新建一个易语言的Windows窗口程序。
2、举一个简单的例子,做一个加法计算器。添加三个编辑框、两个文本和一个按钮,界面如下:
3、此时会用到两个局部变量,定义为变量1、变量2,按CTRL+L弹出局部变量表(或点菜单栏上的插入,选择局部变量),如图。
4、点击变量名下面的框,输入变量1,点击类型,按下空格键,弹出如下菜单:
选择小数型。同样,添加变量2。
5、同样,全局变量也一样,只不过快捷键为CTRL+G,注意的是,要在窗口程序集里面插入变量。
用java编写一个简单计算器
package swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Calculator extends JFrame{
JTextField jt1 ,jt2 ,jt3;
JLabel jLabel;
JButton equButton,addButton,reduceButton,mulButton,divButton;
public Calculator() {
super("简易计算器");
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new GridLayout(2, 0));
JPanel uJPanel = new JPanel();
Dimension preferredSize = new Dimension(50, 29);
GridBagConstraints gbc = new GridBagConstraints();
uJPanel.setLayout(new GridBagLayout());
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 2, 1, 2);
jt1 = new JTextField(4);
jLabel = new JLabel();
jLabel.setPreferredSize(new Dimension(10, 29));
jt2 = new JTextField(4);
equButton = new JButton("=");
ActionListener myActionListener = new MyActionListener();
equButton.addActionListener(myActionListener);
jt3 = new JTextField(8);
jt1.setPreferredSize(preferredSize);
jt2.setPreferredSize(preferredSize);
jt3.setPreferredSize(preferredSize);
uJPanel.add(jt1,gbc);
uJPanel.add(jLabel,gbc);
uJPanel.add(jt2,gbc);
uJPanel.add(equButton,gbc);
gbc.weightx = 1;
uJPanel.add(jt3,gbc);
contentPanel.add(uJPanel);
JPanel dJPanel = new JPanel();
dJPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 2));
addButton = new JButton("+");
reduceButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
addButton.addActionListener(myActionListener);
reduceButton.addActionListener(myActionListener);
mulButton.addActionListener(myActionListener);
divButton.addActionListener(myActionListener);
dJPanel.add(addButton);
dJPanel.add(reduceButton);
dJPanel.add(mulButton);
dJPanel.add(divButton);
contentPanel.add(dJPanel);
this.setContentPane(contentPanel);
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
class MyActionListener implements ActionListener{
Double d1,d2,d3;
String operator = "";
public void actionPerformed(ActionEvent e) {
String fun = e.getActionCommand();
if (!fun.equals("=")) {
jLabel.setText(fun);
operator = fun;
}else {
d1 = jt1.getText().equals("")?null:Double.valueOf(jt1.getText());
d2 = jt2.getText().equals("")?null:Double.valueOf(jt2.getText());
d3 = calculate(d1,d2,operator);
jt3.setText(d3==null ? "":d3.toString());
jt3.setCaretPosition(0);
}
}
}
Double calculate(Double d1,Double d2,String operator){
if (d1 == null || d2 == null) {
return null;
}
Double d3 = null;
switch (operator) {
case "+":
d3 = d1 + d2;
break;
case "-":
d3 = d1 - d2;
break;
case "*":
d3 = d1 * d2;
break;
case "/":
d3 = d1 / d2;
break;
}
return d3;
}
public static void main(String[] args) {
new Calculator();
}
}
java编写程序:创建一个 计算器 的 界面,包含 0-9 个 数字 , A-F个字母,采用 GridLayout布局
import java.awt.*;
import javax.swing.*;
public class MyCalculator extends JFrame{
public MyCalculator() {
String ss1 = "0123456789ABCDEF";
String[] ss2 = {"+","-","*","/","√ ̄","aⁿ","CE","="};//有些字符不在GBK中,所以文本使用了UTF-8编码。如果要用GBK编码,就把字符修改成汉字,比如平方根,乘方
JPanel jp0;//存放显示的文本框
JPanel jp1;//存放0~9 A~F
JPanel jp2;//存放运算符
//jp0
jp0 = new JPanel();
JTextField jtf = new JTextField(16);
jtf.setText("0");
jtf.setHorizontalAlignment(JTextField.RIGHT);//按照惯例,计算器的数字显示在右侧
jtf.setEditable(false);//按照惯例,计算器显示结果的文本框不许编辑
jp0.add(jtf);
JButton jb;//按钮
//jp1
jp1 = new JPanel(new GridLayout(4, 4,3,3));
for (int i = 0; i ss1.length(); i++) {
jb = new JButton(ss1.charAt(i)+"");
jp1.add(jb);
}
//jp2
jp2 = new JPanel(new GridLayout(3, 3,3,3));
for (int i = 0; i ss2.length; i++) {
jb = new JButton(ss2[i]);
jb.setForeground(Color.BLUE);//为了突出这些运算符,把颜色调整成蓝色
jp2.add(jb);
}
add(jp0);
add(jp1);
add(jp2);
setLayout(new FlowLayout());//把JFrame设置成流式布局
setTitle("计算器");//设置窗口标题
setSize(220, 285);//设置窗口大小
setResizable(false);//设置窗口不许缩放,因为流式布局,缩放会导致布局变化
setLocationRelativeTo(null);//设置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置点击关闭退出jvm虚拟机
setVisible(true);//设置窗口可见
}
public static void main(String[] args) {
new MyCalculator();//创建窗口
}
}
关于java设计简易计算器窗口和java设计简易计算器窗口代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。