java窗口的实现---计算器(算加法)
2004-12-09 15:50
609 查看
/***********************************************************************
* Module: Caculater.java
* Author: Administrator
* Created: 2004-12-7 23:48:21
* Purpose:
***********************************************************************/
package org.heixue.test.applet;
/**
* @author Administrator
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculater extends JApplet {
JMenu[] menus = { new JMenu("选项"),new JMenu("关于")};
JMenuItem[] items = {new JMenuItem("关于"),new JMenuItem("设置背景色") };
JButton b1 = new JButton("运算");
JOptionPane b2 = new JOptionPane("运算...");
JTextField txt1 = new JTextField("100",10);
JTextField txt2 = new JTextField("200",10);
JTextField txt3 = new JTextField(20);
JLabel[] label = {new JLabel("+"),new JLabel("=")};
JColorChooser jcc = new JColorChooser();
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e){
//String name = ((JButton)e.getSource()).getText();
int m = JOptionPane.showConfirmDialog(null, "继续进行运算吗?", "确定",JOptionPane.YES_NO_OPTION);
if(m==0){
String op1 = txt1.getText();
String op2 = txt2.getText();
txt3.setText(bussiness(op1,op2));
//txt3.setText(m+"");
}
}
};
ActionListener al2 = new ActionListener() {
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "作者:黑雪小组", "关于...",JOptionPane.INFORMATION_MESSAGE);
}
};
ActionListener al3 = new ActionListener() {
public void actionPerformed(ActionEvent e){
JColorChooser.createDialog(null,"背景色...",true,jcc,al4,null).show();
}
};
ActionListener al4 = new ActionListener() {
public void actionPerformed(ActionEvent e){
changeFace(0,jcc.getColor());
}
};
public void init() {
//initialize layout.
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
//set menu bar.
menus[0].add(items[1]);
items[1].addActionListener(al3);
menus[1].add(items[0]);
items[0].addActionListener(al2);
JMenuBar mb = new JMenuBar();
for(int i = 0; i < menus.length; i++)
mb.add(menus[i]);
setJMenuBar(mb);
//add 3 JTextfield.
cp.add(txt1);
cp.add(label[0]);
cp.add(txt2);
cp.add(label[1]);
cp.add(txt3);
//add 1 button with JOptionPane
cp.add(b1);
b1.addActionListener(al);
}
void changeFace(int i,Object o){
if(i==0){
//设置背景色
this.getContentPane().setBackground((Color)o);
//setBackground((Color)o);
}
}
String bussiness(String op1,String op2){
int result=0;
int op11=0;
int op21=0;
try{
op11=Integer.parseInt(op1);
}catch(Exception e1){
}
try{
op21=Integer.parseInt(op2);
}catch(Exception e1){
}
result=op11+op21;
return ""+result;
}
public static void main(String[] args) {
JApplet applet = new Calculater();
JFrame frame = new JFrame("计算器");
// To close the application:
//Console.setupClosing(frame);
frame.getContentPane().add(applet);
frame.setSize(260,140);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
* Module: Caculater.java
* Author: Administrator
* Created: 2004-12-7 23:48:21
* Purpose:
***********************************************************************/
package org.heixue.test.applet;
/**
* @author Administrator
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculater extends JApplet {
JMenu[] menus = { new JMenu("选项"),new JMenu("关于")};
JMenuItem[] items = {new JMenuItem("关于"),new JMenuItem("设置背景色") };
JButton b1 = new JButton("运算");
JOptionPane b2 = new JOptionPane("运算...");
JTextField txt1 = new JTextField("100",10);
JTextField txt2 = new JTextField("200",10);
JTextField txt3 = new JTextField(20);
JLabel[] label = {new JLabel("+"),new JLabel("=")};
JColorChooser jcc = new JColorChooser();
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e){
//String name = ((JButton)e.getSource()).getText();
int m = JOptionPane.showConfirmDialog(null, "继续进行运算吗?", "确定",JOptionPane.YES_NO_OPTION);
if(m==0){
String op1 = txt1.getText();
String op2 = txt2.getText();
txt3.setText(bussiness(op1,op2));
//txt3.setText(m+"");
}
}
};
ActionListener al2 = new ActionListener() {
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "作者:黑雪小组", "关于...",JOptionPane.INFORMATION_MESSAGE);
}
};
ActionListener al3 = new ActionListener() {
public void actionPerformed(ActionEvent e){
JColorChooser.createDialog(null,"背景色...",true,jcc,al4,null).show();
}
};
ActionListener al4 = new ActionListener() {
public void actionPerformed(ActionEvent e){
changeFace(0,jcc.getColor());
}
};
public void init() {
//initialize layout.
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
//set menu bar.
menus[0].add(items[1]);
items[1].addActionListener(al3);
menus[1].add(items[0]);
items[0].addActionListener(al2);
JMenuBar mb = new JMenuBar();
for(int i = 0; i < menus.length; i++)
mb.add(menus[i]);
setJMenuBar(mb);
//add 3 JTextfield.
cp.add(txt1);
cp.add(label[0]);
cp.add(txt2);
cp.add(label[1]);
cp.add(txt3);
//add 1 button with JOptionPane
cp.add(b1);
b1.addActionListener(al);
}
void changeFace(int i,Object o){
if(i==0){
//设置背景色
this.getContentPane().setBackground((Color)o);
//setBackground((Color)o);
}
}
String bussiness(String op1,String op2){
int result=0;
int op11=0;
int op21=0;
try{
op11=Integer.parseInt(op1);
}catch(Exception e1){
}
try{
op21=Integer.parseInt(op2);
}catch(Exception e1){
}
result=op11+op21;
return ""+result;
}
public static void main(String[] args) {
JApplet applet = new Calculater();
JFrame frame = new JFrame("计算器");
// To close the application:
//Console.setupClosing(frame);
frame.getContentPane().add(applet);
frame.setSize(260,140);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
相关文章推荐
- 用Java的布局和组件,并且设置监听器实现一个小的加法计算器
- 定义一个函数,在该函数中可以实现任意两个整数的加法。java实现
- Java实现QQ窗口自动输入
- java编程加载窗口,实现物体变速运动(台球的运动)
- objective-c 一个链式加法计算器实现
- java代码实例 使用switch实现简易的计算器(实现加减乘除)
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- 大数据的加法运算Java代码实现
- java实现计算器
- Java实现分数的加法
- java面试题--实现一个百亿的计算器
- Atitit. 悬浮窗口的实现 java swing c# .net c++ js html 的实现
- 后缀表达式的计算器Java实现
- [Java]利用javax.swing.Timer类在窗口上实现动画效果
- JavaWeb网页版计算器实现
- Java实现四则运算计算器(支持括号,小数,负数)
- 【LintCode-655】大整数加法(Java实现)
- CCF 窗口 Java实现
- 计算器算法实现java