AboutDialog.javaimport java.io.*;
import java.awt.*;
import java.awt.event.*;public class AboutDialog extends Dialog implements ActionListener{
private Label l;
private Button b;
private TextArea ta;
private Panel p1;
private Panel p2;
private Panel p3;
private Canvas c1, c2, c3, c4;
private String filename = "xxx.txt";
private String title = "关于计算器";
private String content = "";
public AboutDialog(Frame f){
super(f,true);
this.setTitle(title);
l = new Label("About Calculator V 1.00");
l.setForeground(Color.blue);
l.setFont(new Font("Arial",Font.PLAIN,24));
b = new Button(" 确定 ");
b.addActionListener(this); //(new ButtonHandler());
ta = new TextArea("",30,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta.setBackground(Color.lightGray);
ta.setEditable(false);
c1 = new Canvas();
c2 = new Canvas();
c3 = new Canvas();
c4 = new Canvas();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p2.setLayout(new BorderLayout(5,5));
p1.setBackground(Color.lightGray);
p2.setBackground(new Color(130,254,240,200));
p3.setBackground(Color.lightGray);
p1.add(l);
p2.add(ta);
p2.add(c1,"North");
p2.add(c2,"South");
p2.add(c3,"West");
p2.add(c4,"East");
p3.add(b);
this.add(p1,"North");
this.add(p2,"Center");
this.add(p3,"South");
this.setLocation(f.getX() + f.getWidth(), f.getY());
this.addWindowListener(new WindowHandler());
}
public void display(){
this.setSize(350,350);
this.readfromFile(filename);
this.setTextArea();
this.setResizable(false);
this.setVisible(true);
}
public void readfromFile(String s){
try{
BufferedReader br = new BufferedReader(new FileReader(s));
String s1 = br.readLine();
String s2 = "";
while(s1 != null){
s2 = s2 + s1 + "\n";
s1 = br.readLine();
}
br.close();
content = s2;
}catch(IOException e){
e.printStackTrace();
}
}
public void setFilename(String s){
filename = s;
}
public void setLabel(String s){
l.setText(s);
}
public void setTextArea(){
ta.setText(content);
}
// private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
}
// }
private class WindowHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
dispose();
//System.exit(1);
}
}
}
import java.awt.*;
import java.awt.event.*;public class AboutDialog extends Dialog implements ActionListener{
private Label l;
private Button b;
private TextArea ta;
private Panel p1;
private Panel p2;
private Panel p3;
private Canvas c1, c2, c3, c4;
private String filename = "xxx.txt";
private String title = "关于计算器";
private String content = "";
public AboutDialog(Frame f){
super(f,true);
this.setTitle(title);
l = new Label("About Calculator V 1.00");
l.setForeground(Color.blue);
l.setFont(new Font("Arial",Font.PLAIN,24));
b = new Button(" 确定 ");
b.addActionListener(this); //(new ButtonHandler());
ta = new TextArea("",30,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta.setBackground(Color.lightGray);
ta.setEditable(false);
c1 = new Canvas();
c2 = new Canvas();
c3 = new Canvas();
c4 = new Canvas();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p2.setLayout(new BorderLayout(5,5));
p1.setBackground(Color.lightGray);
p2.setBackground(new Color(130,254,240,200));
p3.setBackground(Color.lightGray);
p1.add(l);
p2.add(ta);
p2.add(c1,"North");
p2.add(c2,"South");
p2.add(c3,"West");
p2.add(c4,"East");
p3.add(b);
this.add(p1,"North");
this.add(p2,"Center");
this.add(p3,"South");
this.setLocation(f.getX() + f.getWidth(), f.getY());
this.addWindowListener(new WindowHandler());
}
public void display(){
this.setSize(350,350);
this.readfromFile(filename);
this.setTextArea();
this.setResizable(false);
this.setVisible(true);
}
public void readfromFile(String s){
try{
BufferedReader br = new BufferedReader(new FileReader(s));
String s1 = br.readLine();
String s2 = "";
while(s1 != null){
s2 = s2 + s1 + "\n";
s1 = br.readLine();
}
br.close();
content = s2;
}catch(IOException e){
e.printStackTrace();
}
}
public void setFilename(String s){
filename = s;
}
public void setLabel(String s){
l.setText(s);
}
public void setTextArea(){
ta.setText(content);
}
// private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
}
// }
private class WindowHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
dispose();
//System.exit(1);
}
}
}
解决方案 »
- 我在JCreator中写的代码,在eclipse中都能运行,但是在MyEclipse报了好多错
- 有关《java核心开发技术从入门到精通》的问题:子类继承父类....
- Exception以及其他类的构造方法是?
- JTable交替显示
- servlet中文编码出现乱码的问题
- 用java的jdk做的比分程序,做了一半,数据库入库没做完,谁来接?
- 遇到一个非常奇怪的问题,关于数组的赋值
- 加急请教一简单问题!晕了
- 急!字体问题
- 怎样在applet 中显示统计图
- java连接oracle8练习程序,调试无错误,但无法运行出正确结果,请教为什么,源代码内详,捕捉异常内详
- 【江湖救急】Eclipse里面如何配置jar包和source关联?
import java.awt.event.*;public class Computer implements ActionListener{
private Frame f = new Frame("计算器");
private Panel p = new Panel();
private Panel p1 = new Panel();
private Panel p2 = new Panel();
private Panel p3 = new Panel();
private AboutDialog d ;
private HelpDialog d1 ;
private Button b[] = new Button[21];
private Button b1[] = new Button[4];
private Button b2[] = new Button[5];
private TextField tf = new TextField();
private TextField tf1 = new TextField(1);
private String s,oper,s2;
private boolean flag = true;
private double result, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
public void run(){
s = "";
oper = "";
s2 = "";
//tf.setEditable(false);
tf1.setEditable(false);
MenuBar mb = new MenuBar();
f.setLayout(new BorderLayout(5,5));
f.setBackground(Color.gray);
f.setLocation(300,200);
p.setLayout(new BorderLayout(3,3));
p.setBackground(Color.gray);
p1.setLayout(new GridLayout(4,5));
p2.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
p3.setLayout(new GridLayout(4,1));
f.setMenuBar(mb);
Menu m1 = new Menu("编辑");
Menu m2 = new Menu("关于");
Menu m3 = new Menu("帮助");
mb.add(m1);
mb.add(m2);
mb.setHelpMenu(m3);
MenuItem mi1 = new MenuItem("清除");
MenuItem mi2 = new MenuItem("退出");
MenuItem mi3 = new MenuItem("关于计算器");
MenuItem mi4 = new MenuItem("帮助主题");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m2.add(mi3);
m3.add(mi4);
mi1.addActionListener(this);
mi2.addActionListener(this);
//mi3.addActionListener(this);
//mi4.addActionListener(this);
//--------------------------------------------------------------
mi3.addActionListener(new MyHandler());
mi4.addActionListener(new MyHandler());
//--------------------------------------------------------------
for(int i=1;i<4;i++){
b1[i] = new Button();
b1[i].setFont(new Font("仿宋",0,16));
}
b1[1].setLabel(" 退格 ");
b1[2].setLabel("二进制转换");
b1[3].setLabel(" CE ");
p2.add(tf1);
for(int i=1;i<4;i++){
p2.add(b1[i]);
b1[i].addActionListener(this);
}
for(int i=1;i<5;i++){
b2[i] = new Button();
b2[i].setFont(new Font("仿宋",0,16));
}
b2[1].setLabel("MC");
b2[2].setLabel("MR");
b2[3].setLabel("MS");
b2[4].setLabel("M+");
for(int i=1;i<5;i++){
p3.add(b2[i]);
b2[i].addActionListener(this);
}
for(int i=1;i<21;i++){
b[i] = new Button();
b[i].setFont(new Font("仿宋",0,16));
}
b[1].setLabel("7");
b[2].setLabel("8");
b[3].setLabel("9");
b[4].setLabel("/");
b[5].setLabel("squrt");
b[6].setLabel("4");
b[7].setLabel("5");
b[8].setLabel("6");
b[9].setLabel("*");
b[10].setLabel("%");
b[11].setLabel("1");
b[12].setLabel("2");
b[13].setLabel("3");
b[14].setLabel("-");
b[15].setLabel("1/x");
b[16].setLabel("0");
b[17].setLabel("+/-");
b[18].setLabel(".");
b[19].setLabel("+");
b[20].setLabel("=");
for(int i=1;i<21;i++){
p1.add(b[i]);
b[i].addActionListener(this);
b[i].setBackground(new Color(38,99,224));
b[i].setForeground(Color.yellow);
}
for(int i=4;i<15;i=i+5){
b[i].setBackground(new Color(120,180,170));
b[i].setForeground(Color.red);
}
for(int i=17;i<21;i++){
b[i].setBackground(new Color(120,180,170));
b[i].setForeground(Color.red);
}
for(int i=1;i<4;i++){
b[i*5].setBackground(new Color(20,130,180));
b[i*5].setForeground(Color.pink);
}
f.add(tf,"North");
p.add(p1,"Center");
p.add(p2,"North");
p.add(p3,"West");
f.add(p,"Center");
f.pack();
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
}
public void actionPerformed(ActionEvent e){
s = e.getActionCommand();
String s1 = tf.getText(); if(s=="0"|s=="1"|s=="2"|s=="3"|s=="4"|s=="5"|s=="6"|s=="7"|s=="8"|s=="9"){
if(flag){
tf.setText(s1 + s);
}else{
tf.setText(s);
flag = true;
}
}else if(s=="+"|s=="-"|s=="*"|s=="/"){
result = Double.parseDouble(s1);
flag = false;
oper = s;
}else if(s=="="){
tmp1 = Double.parseDouble(tf.getText());
if(oper == "+") result += tmp1;
if(oper == "-") result -= tmp1;
if(oper == "*") result *= tmp1;
if(oper == "/") result /= tmp1;
tf.setText(Double.toString(result));
oper = "";
flag = false;
}else if(s=="+/-"){
tmp2 = -1*Double.parseDouble(tf.getText());
tf.setText(Double.toString(tmp2));
}else if(s=="squrt"){
tmp3 = Double.parseDouble(tf.getText());
if(tmp3>0){
tmp3 = Math.sqrt(tmp3);
tf.setText(Double.toString(tmp3));
flag = false;
}else{}
}else if(s=="1/x"){
tmp4 = 1/Double.parseDouble(tf.getText());
tf.setText(Double.toString(tmp4));
flag = false;
}else if(s=="%"){
tmp5 = Double.parseDouble(tf.getText())/100;
if(oper == "*"){
result = result*(result*tmp5);
tf.setText(Double.toString(result));
}else if(oper == "+"){
result = result+result*tmp5;
tf.setText(Double.toString(result));
}else if(oper == "-"){
result = result-result*tmp5;
tf.setText(Double.toString(result));
}else if(oper == "/"){
result = result/(result*tmp5);
tf.setText(Double.toString(result));
}else{
tf.setText("0.0");
}
flag = false;
oper = "";
}else if(s==" 退格 "){
String after = s1.substring(0,(s1.length()-1));
tf.setText(after);
}else if(s=="."){
if(s1.indexOf('.')==-1){
tf.setText(s1+s);
}else{}
}else if(s==" CE "|s=="清除"){
tf.setText("");
flag = false;
}else if(s=="退出"){
System.exit(1);
}
/*----------------------------------------------------------------
else if(s=="关于计算器"){
d = new AboutDialog(f);
d.display();
}else if(s=="帮助主题"){
d1 = new HelpDialog(f);
d1.display();
}
-------------------------------------------------------------------*/
else if(s=="二进制转换"){
String after="";
long data;
tmp6 = Double.parseDouble(tf.getText());
data = Math.round(tmp6);
int b = 1<<31;
for(int i=0;i<32;i++){
if((data&b)!=0){
after=after+"1";
}else{
after=after+"0";
}
b=b>>>1;
}
after = after.substring(after.indexOf("1"));
tf.setText(after);
flag = false;
}else if(s=="MS"){
String before = tf.getText();
tmp7 = Double.parseDouble(before);
s2 = Double.toString(tmp7);
tf1.setText(" M");
flag = false;
}else if(s=="MR"){
tf.setText(s2);
flag = false;
}else if(s=="MC"){
s2 = "";
tf1.setText("");
}else if(s=="M+"){
tmp7 = tmp7 + Double.parseDouble(tf.getText());
s2 = Double.toString(tmp7);
flag = false;
}
}
public static void main(String args[]){
Computer c = new Computer();
c.run();
}
//-----------------------------------------------------------------
private class MyHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s=="关于计算器"){
d = new AboutDialog(f);
d.display();
}else if(s=="帮助主题"){
d1 = new HelpDialog(f);
d1.display();
}
}
}
//-----------------------------------------------------------------
}
public HelpDialog(Frame f){
super(f);
this.setLabel("Help");
this.setFilename("xxx.txt");
this.setTitle("帮助");
}
}
可惜,我就是看不懂阿!郁闷ing……
asdfsdfg
sdfg sfh fdsh d
dsfh dgh
dfgh d
dh d这个文件,你可以自己修改,只要四个文件放在一个目录下,编译运行,就可以了
当然了,要运行带有主函数的那个类的
至于看不懂,那是因为你现在不到时候,万事开头难,一步步来么,怕什么呢?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* Created by IntelliJ IDEA.
* User: Bernie Wong
* Date: Feb 18, 2003
* Time: 8:51:28 PM
* To change this template use Options | File Templates.
*/
public class Calculator {
public static void main(String[] args)
{
CaculatorFrame frame = new CaculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class CaculatorFrame extends JFrame
{
public CaculatorFrame()
{
setTitle("Calculator");
setSize(WIDTH,HEIGHT); Container contentPane = getContentPane();
CaculatorPanel panel = new CaculatorPanel();
contentPane.add(panel);
}
public static final int WIDTH = 200;
public static final int HEIGHT = 200;}
class CaculatorPanel extends JPanel
{
public CaculatorPanel()
{
setLayout(new BorderLayout()); result = 0;
lastCommand = "=";
start = true; display = new JTextField("0");
display.setEditable(false);
add(display,BorderLayout.NORTH); ActionListener insert = new InsertAction();
ActionListener command = new CommandAction(); panel = new JPanel();
panel.setLayout(new GridLayout(4,4)); addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/",command); addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*",command); addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-",command); addButton("0",insert);
addButton(".",insert);
addButton("=",command);
addButton("+",command); add(panel,BorderLayout.CENTER);
}
private void addButton(String label,ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if(start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
} private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(start)
{
if(command.equals("-"))
{
display.setText(command);
start = false;
}
else lastCommand = command;
}
else
{
calculator(Double.parseDouble(display.getText()));
start = true; }
}
} public void calculator(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);
}
private JTextField display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;}
非常简单
我有一个计算器 class 8k
可以对字符串进行科学计算器计算
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
public class Example3 extends WindowAdapter implements ActionListener{
Frame f;
Panel p;
TextArea ta;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
Button b;
int i=0;
int j=0;
int k=0;
int l=0;
int tag=0;
double aa,bb,cc,dd,xx; public static void main(String args[]){
Example3 e3=new Example3();
e3.go();
}
public void go(){
f=new Frame("Calculator");
p=new Panel(new GridLayout(4,4));
ta=new TextArea("",1,30,TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
b=new Button("C");
f.add(ta,"North");
f.add(p,"Center");
f.add(b,"South"); b1=new Button("7"); p.add(b1);
b2=new Button("8"); p.add(b2);
b3=new Button("9"); p.add(b3);
b4=new Button("+"); p.add(b4); b5=new Button("4"); p.add(b5);
b6=new Button("5"); p.add(b6);
b7=new Button("6"); p.add(b7);
b8=new Button("-"); p.add(b8); b9=new Button("1"); p.add(b9);
b10=new Button("2"); p.add(b10);
b11=new Button("3"); p.add(b11);
b12=new Button("*"); p.add(b12); b13=new Button("0"); p.add(b13);
b14=new Button("."); p.add(b14);
b15=new Button("="); p.add(b15);
b16=new Button("/"); p.add(b16); b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b.addActionListener(this);
f.addWindowListener(this);
f.setSize(200,200);
f.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
if(s=="0"||s=="1"||s=="2"||s=="3"||s=="4"||s=="5"||s=="6"||s=="7"||s=="8"||s=="9"){
switch(Integer.parseInt(s)){
case 1:case 2:case 3:case 4:case 5:
case 6:case 7:case 8:case 9:case 0:
if(ta.getText().compareTo("0")==0) break;
else ta.append(s);
}
}
else{
switch(s.charAt(0)){
case 'C':
ta.setText("");
i=0;j=0;k=0;l=0;tag=0;break;
case '.':
if(ta.getText().compareTo("")==0||tag==1)
break;
else ta.append(s);tag=1;break;
case '+':
Double a=new Double(0);
a=a.valueOf(ta.getText());
aa=a.doubleValue();
ta.setText("");
i=1;j=0;k=0;l=0;break;
case '-':
Double b=new Double(0);
b=b.valueOf(ta.getText());
bb=b.doubleValue();
ta.setText("");
j=1;i=0;k=0;l=0;break;
case '*':
Double c=new Double(0);
c=c.valueOf(ta.getText());
cc=c.doubleValue();
ta.setText("");
k=1;i=0;j=0;l=0;break;
case '/':
Double d=new Double(0);
d=d.valueOf(ta.getText());
dd=d.doubleValue();
ta.setText("");
l=1;i=0;j=0;k=0;break;
case '=':
Double x=new Double(0);
x=x.valueOf(ta.getText());
xx=x.doubleValue();
Double resultadd=new Double(aa+xx);
Double resultsub=new Double(bb-xx);
Double resultmul=new Double(cc*xx);
Double resultdiv=new Double(dd/xx);
if(i==1) ta.setText(resultadd.toString());
if(j==1) ta.setText(resultsub.toString());
if(k==1) ta.setText(resultmul.toString());
if(l==1) ta.setText(resultdiv.toString());
tag=1;
}
}
}
}