GridLayout,JPane組件
/**
?* @(#)TestCalculator.java
?*
?* TestCalculator application
?*
?* @author
?* @version 1.00 2007/1/22
?*/
?import java.*;
?import java.awt.*;
?import java.awt.event.*;
?import javax.swing.*;
import java.util.EventListener;
public class TestCalculator extends JFrame implements ActionListener
{
?private JTextField jfd=new JTextField();
?String str=new String("123+456-789*0.=\\");
?String strch=null;
?Character ch=null;
?
??? public TestCalculator()
??? {
??? ?
??? ?this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
??? ?Container c=this.getContentPane();
??? ?jfd.setHorizontalAlignment(JTextField.RIGHT);
??? ?JPanel jpl=new JPanel();
??? ?jpl.setLayout(new GridLayout(4,4));
??? ?c.add(jfd,BorderLayout.NORTH);
??? ?c.add(jpl,BorderLayout.CENTER);
??? ?//System.out.println(str.substring(1));
??? ?JButton b=null;
??? ?for(int i=0;i<16;i++)
??? ?{
??? ??ch=str.charAt(i);
??? ??b=new JButton(ch.toString());
??? ??//b=new JButton(new Integer(i+1).toString());
??? ??b.addActionListener(this);
??? ??jpl.add(b);
??? ?}
??? ?/*b=new JButton("1");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("2");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("3");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("+");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("4");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("5");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("6");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("-");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("7");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("8");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("9");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("*");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("0");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton(".");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("=");
??? ?b.addActionListener(this);
??? ?jpl.add(b);
??? ?
??? ?b=new JButton("\\");
??? ?b.addActionListener(this);
??? ?jpl.add(b);*/
??? ?
??? ?
??? ?
???
??? ??
??? }
??? public static void main(String[] args)
??? {
??? ?
??? ?// TODO, add your application code
??? ?System.out.println("Hello World!");
??? ?TestCalculator mainFrame=new TestCalculator();
??? ?mainFrame.setTitle("TestCalculator");
??? ?mainFrame.setBounds(300,200,300,400);
??? ?mainFrame.setVisible(true);
??? }
?public void actionPerformed(ActionEvent e)
?{
??// TODO: Add your code here
???? //||strnum.indexOf("-")!=-1||strnum.indexOf("*")!=-1||strnum.indexOf("\\")!=-1
??
??String strnum=jfd.getText();
??//System.out.println(strnum);
??Character c1='+';
??//Character c2='-';
??int n1=0;
??float num1=0;
??float num2=0;
??float sum=0;
??Integer len=new Integer(0);
??
??if(e.getActionCommand().equals("+")||e.getActionCommand().equals("-")||e.getActionCommand().equals("*")||e.getActionCommand().equals("\\"))
??{
???
???num1=new Float(strnum.substring(0,len));
???
???System.out.println(num1);
???if(strnum.indexOf(c1)!=-1||strnum.indexOf("-")!=-1||strnum.indexOf("*")!=-1||strnum.indexOf("\\")!=-1)
???{
????//System.out.println(strnum.length());
????jfd.setText(jfd.getText());
????//System.out.println(e.getActionCommand());
???}
???else
???{
????jfd.setText(jfd.getText()+e.getActionCommand());
?????
????//System.out.println("no find.");
???}
???
??}
??else if(e.getActionCommand().equals("."))
??{
???
???if(strnum.indexOf(".")!=-1)
???{
????
????n1=strnum.indexOf(".");
????
????//System.out.println(strnum.length());
????if(strnum.substring(n1+1,n1+1).equals(".")==false)
????{
?????//System.out.println(strnum.substring(n1+1,n1+1).equals("."));
?????jfd.setText(jfd.getText());
?????
????}
????else
????{
?????jfd.setText(jfd.getText()+e.getActionCommand());
?????
????}
???}
???else
???{
????jfd.setText(jfd.getText()+e.getActionCommand());
???}
???
??}
??else
??{
???if(e.getActionCommand().equals("="))
???{
????if(strnum.indexOf("=")!=-1)
????{
?????jfd.setText(jfd.getText());
????}
????else
????{
?????
?????jfd.setText(jfd.getText()+e.getActionCommand());
????
????
?????//System.out.println(strnum.substring(len+1,strnum.length()));
????}
???}
???else
???{
????jfd.setText(jfd.getText()+e.getActionCommand());
???}
???
??}
??
?}
}
posted on 2007-01-26 15:14 大頭劍客 閱讀(219) 評論(0) 編輯 收藏 所屬分類: 學習筆記