国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java窗口的實現---計算器(算加法)

2019-11-17 05:55:12
字體:
來源:轉載
供稿:網友
 /***********************************************************************
* 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);
}
} ///:~

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄石市| 育儿| 泽普县| 鸡西市| 宜黄县| 余江县| 大厂| 綦江县| 土默特左旗| 嘉义县| 诏安县| 句容市| 宁阳县| 玉山县| 丽江市| 墨玉县| 镇安县| 台北县| 安多县| 区。| 南江县| 芜湖县| 凉山| 资兴市| 体育| 枞阳县| 星子县| 台江县| 诏安县| 乐昌市| 和硕县| 玛曲县| 阿克陶县| 微山县| 江永县| 太谷县| 成都市| 奇台县| 丹凤县| 名山县| 安仁县|