java-常见几何图形计算器

PS:感谢木木润泽的大力支持
广东某高校 java实训,同学们点个赞啊

1.思路

(1)、先分为5个类,一个主界面类,4个几何图形的类。本人能力有限,无法做到把计算器打包弄成一个类,故在每个几何图形类里面都会有计算器的产生。
(2)、先把每个类的界面弄出来,然后再连接起来,最后再一一做事件触发器

2.技术难点

由于本人经验不足,不知道焦点函数如何获得文本框对象,故用的方法是首先全局变量int flag,然后再用焦点监听器,把相对应的文本框给相对应的
flag值,然后再在事件触发器中检测是哪个flag值,赋值给文本框(可能说得不是很明白,在代码里面会有体现的)

3.图片

(知道没有图片很多人都不愿意往下看了( ̄ ‘i  ̄;))

在这里插入图片描述
主菜单在这里插入图片描述
下拉
在这里插入图片描述

矩形
在这里插入图片描述
在这里插入图片描述
三角形
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
圆形
在这里插入图片描述
在这里插入图片描述
圆柱
在这里插入图片描述

4.代码

(1)、CalculatorWindow
package Calculator;

/*
	该程序以RectanglePanel.java为第一个完成品,其他三个都是通过复制粘贴加修改方式完成的,
	其他三个类注释可能会有错误,正确的注释都在RectanglePanel.java里面
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class CalculatorWindow extends JFrame implements ItemListener,ActionListener {

    JMenuBar menuBar;
    JMenu menu;
    JMenuItem zhu,exit;
    JPanel MainPanel;//主界面
    JPanel j=new JPanel();
    CardLayout card=new CardLayout();
    JComboBox cmb;

    public CalculatorWindow() {
        super("常见几何图形计算器");
        this.setLayout(new CardLayout());
        this.setSize(550, 300);
        this.setLocation(200, 200);

        menu=new JMenu("操作");
        j.setLayout(card);
        zhu=new JMenuItem("主界面");
        exit=new JMenuItem("退出");
        exit.addActionListener(this);
        zhu.addActionListener(this);
        menu.add(zhu);
        menu.add(exit);
        menuBar=new JMenuBar();
        menuBar.add(menu);
        //菜单结束
        MainPanel=new JPanel();
        MainPanel.setLayout(new BorderLayout());



        cmb=new JComboBox();    //创建JComboBox

        cmb.addItem("请点击下拉列表选择");
        cmb.addItem("矩形的基本计算");
        cmb.addItem("圆的基本计算");
        cmb.addItem("三角形的基本计算");
        cmb.addItem("圆柱的基本计算");
        cmb.addItemListener(this);
        cmb.setSize(50,50);

        MainPanel.add(cmb,BorderLayout.NORTH);
        getContentPane().add(MainPanel);

        MainPanel.add(j,BorderLayout.CENTER);

        add(MainPanel);
        this.setJMenuBar(menuBar);
        this.setVisible(true);


    }
    public void itemStateChanged(ItemEvent e)
    {
        if(e.getStateChange() == ItemEvent.SELECTED)
        {
            if(cmb.getSelectedItem()=="请点击下拉列表选择") {
                j.add(new JLabel(""));
                card.next(j);
            }
            if(cmb.getSelectedItem()=="矩形的基本计算") {
                j.add(new RectanglePanel());
                card.next(j);
            }
            if(cmb.getSelectedItem()=="圆的基本计算") {
                j.add(new yuanPanel());
                card.next(j);
            }
            if(cmb.getSelectedItem()=="三角形的基本计算") {
                j.add(new sanjiaoPanel());
                card.next(j);
            }
            if(cmb.getSelectedItem()=="圆柱的基本计算") {
                j.add(new yuanzhuPanel());
                card.next(j);
            }

        }
    }
    
    @Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
    	
		if(e.getSource()==exit) {//退出
			//System.out.println("exit");
			System.exit(0);
		}
		else if (e.getSource()==zhu) {//返回主菜单
			cmb.setSelectedIndex(0);
		}
	}
    
    public static void main(String [] args) {
        new CalculatorWindow();
    }

	
}
(2)、RectanglePanel
package Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;

public class RectanglePanel extends JPanel implements ActionListener,FocusListener{
	
	JPanel leftPanel,rightPanel,buttonPanel;//左边的容器,右边的容器,按钮容器
	JButton resultButton,clearButton;//结果按钮,清除按钮
	JTextField widhtTextField,hightTextField,lengthTextField,areaTextField;//宽和高的输入文本框,周长和面积输出文本框
	JButton []jButton = new JButton[12];//右边的计算器
	double height,widht;//全局变量,高和宽
	
	int flag;//判断标志,1为输入宽的文本框,2为输入高的文本框
	
	public RectanglePanel() {
		//super("矩形计算");
		this.setLayout(new BorderLayout());//东南西北的布局
		//this.setSize(550, 250);//大小
		//setLocationRelativeTo(null);//设置窗口居于屏幕中央
		
		//leftPanel开始
		leftPanel=new JPanel();
		leftPanel.setLayout(new GridLayout(5,1));//5行1列
			//第一行开始
		JPanel p1=new JPanel();
		JLabel l1_1=new JLabel("请输入矩形的宽:");
		widhtTextField=new JTextField(10);
		p1.add(l1_1);
		p1.add(widhtTextField);
		leftPanel.add(p1);
			//第一行结束
			//第二行开始
		JPanel p2=new JPanel();
		JLabel l2=new JLabel("请输入矩形的高:");
		hightTextField=new JTextField(10);
		p2.add(l2);
		p2.add(hightTextField);
		leftPanel.add(p2);
			//第二行结束
			//第三行开始
		buttonPanel=new JPanel();
		resultButton=new JButton("计算结果:");
		clearButton=new JButton("清空");
		buttonPanel.add(resultButton);
		buttonPanel.add(clearButton);
		leftPanel.add(buttonPanel);
			//第三行结束
			//第四行开始
		JPanel p3=new JPanel();
		JLabel l3=new JLabel("矩形的周长:");
		lengthTextField=new JTextField(15);
		p3.add(l3);
		p3.add(lengthTextField);
		leftPanel.add(p3);
			//第四行结束
			//第五行开始
		JPanel p4=new JPanel();
		JLabel l4=new JLabel("矩形的面积:");
		areaTextField=new JTextField(15);
		p4.add(l4);
		p4.add(areaTextField);		
		leftPanel.add(p4);
			//第五行结束
		//leftPanel结束
		
		//rightPanel开始
		rightPanel=new JPanel();
		rightPanel.setLayout(new GridLayout(4,3));//网格,4行3列
		String [] name= {"1","2","3","4","5","6","7","8","9","0",".","BackSpace"};
		for(int i=0;i<name.length;i++) {
			jButton[i]=new JButton(name[i]);
			jButton[i].addActionListener(this);//每个按钮添加事件触发器
			rightPanel.add(jButton[i]);//添加按钮
		}
		//rightPanel结束
		
		//结果按钮监听者
		resultButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(!widhtTextField.getText().trim().equals("")&&//trim()作用是去除两端空白字符的字符串
						!widhtTextField.getText().trim().isEmpty()&&
						!hightTextField.getText().trim().equals("")&&
						!hightTextField.getText().trim().isEmpty()) {//判断条件是,先去除两边的空白字符串(如果有的话),然后判断两个输入框是否为空
					widht=Double.parseDouble(widhtTextField.getText());//先取得文本款的内容,然后转换成double类型
					height=Double.parseDouble(hightTextField.getText());
					lengthTextField.setText(String.valueOf(RectangleLenght()));//先计算周长,然后再转换成String,放在矩形周长的文本框内
					areaTextField.setText(String.valueOf(RectangleArea()));//先计算面积,然后再转换成String,放在矩形面积的文本框内
				}
			}
		});
		
		//清除按钮监听者
		clearButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				widhtTextField.setText("");//文本框全都清空
				hightTextField.setText("");
				lengthTextField.setText("");
				areaTextField.setText("");
			}
		});
		
		//注册焦点与事件
		widhtTextField.addFocusListener(this);
		hightTextField.addFocusListener(this);
		widhtTextField.addActionListener(this);
		hightTextField.addActionListener(this);

		add(rightPanel,BorderLayout.EAST);
		add(leftPanel,BorderLayout.WEST);
		this.setVisible(true);
	}
	
	//计算周长
	public double RectangleLenght() {
		return 2*widht+2*height;
	}
	
	//计算面积
	public double RectangleArea() {
		return widht*height;
	}
	
	
	/*public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new RectanglePanel();
	}*/



	
	//当光标在宽的输入文本框内,flag=1;当光标在高的输入文本框内,flag=2
	public void focusGained(FocusEvent e) {
		// TODO 自动生成的方法存根
		JComponent component = (JComponent)e.getSource();//getSource()获取事件所作用的对象,并转换成JComponent类型
		if(component==widhtTextField) {//当光标在宽的输入文本框内
			flag=1;
		}
		else if(component==hightTextField) {//当光标在高的输入文本框内
			flag=2;
		}
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		JButton button=(JButton)e.getSource();//获取事件所作用的对象,并传值给button
		String s=e.getActionCommand();//获得当前事件所对应按钮里面的字符串
		if(flag==1) {//当光标在宽的输入文本框
			//inputTextField=widhtTextField;
			inputNum(s,widhtTextField);
		}
		else if(flag==2) {//当光标在高的输入文本框内
			//inputTextField=hightTextField;
			inputNum(s,hightTextField);
		}
	}
	
	public void inputNum(String s,JTextField tf) {
		if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals(".")||
				s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")||s.equals("0")) {//字符串等于1、2、3……时
			if(tf.getText().equals("0")) tf.setText(s);//如果当前文本框没有东西,添加该字符串
			else tf.setText(tf.getText()+s);//否则在基础上添加字符串
		}
		else if(s.equals("BackSpace")) {//按钮为删除键时
			if(!tf.getText().equals("")) {//非空时
				tf.setText(tf.getText().substring(0, tf.getText().length()-1));//substrin方法返回字符串的子字符串。
			}
		}
	}
	
}
(3)、sanjiaoPanel
package Calculator;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class sanjiaoPanel extends JPanel implements ActionListener,FocusListener{
	
	JPanel leftPanel,rightPanel,buttonPanel;
	JButton resultButton,clearButton;
	JTextField ATextField,BTextField,CTextField,lengthTextField,areaTextField;
	JButton []jButton = new JButton[12];//右边的计算器
	double A,B,C;//三个边
	
	//JTextField inputTextField;//临时存放的文本框
	int flag;//判断标志
	
	public sanjiaoPanel() {
		//super("三角形计算");
		this.setLayout(new BorderLayout());
		//this.setSize(550, 250);
		//setLocationRelativeTo(null);//设置窗口居于屏幕中央
		
		//leftPanel开始
		leftPanel=new JPanel();
		leftPanel.setLayout(new GridLayout(6,1));//6行1列
			//第一行开始
		JPanel p1=new JPanel();
		JLabel l1_1=new JLabel("请输入三角形的边 A:");
		ATextField=new JTextField(7);
		p1.add(l1_1);
		p1.add(ATextField);
		leftPanel.add(p1);
			//第一行结束
			//第二行开始
		JPanel p2=new JPanel();
		JLabel l2=new JLabel("请输入三角形的边 B:");
		BTextField=new JTextField(7);
		p2.add(l2);
		p2.add(BTextField);
		leftPanel.add(p2);
			//第二行结束
			//第三行开始
		JPanel p3=new JPanel();
		JLabel l3=new JLabel("请输入三角形的边 C:");
		CTextField=new JTextField(7);
		p3.add(l3);
		p3.add(CTextField);
		leftPanel.add(p3);
			//第三行结束
			//第四行开始
		buttonPanel=new JPanel();
		resultButton=new JButton("计算结果:");
		clearButton=new JButton("清空");
		buttonPanel.add(resultButton);
		buttonPanel.add(clearButton);
		leftPanel.add(buttonPanel);
			//第四行结束
			//第五行开始
		JPanel p4=new JPanel();
		JLabel l4=new JLabel("三角形的周长:");
		lengthTextField=new JTextField(13);
		p4.add(l4);
		p4.add(lengthTextField);
		leftPanel.add(p4);
			//第四行结束
			//第五行开始
		JPanel p5=new JPanel();
		JLabel l5=new JLabel("三角形的面积:");
		areaTextField=new JTextField(13);
		p5.add(l5);
		p5.add(areaTextField);		
		leftPanel.add(p5);
			//第五行结束
		//leftPanel结束
		
		//rightPanel开始
		rightPanel=new JPanel();
		rightPanel.setLayout(new GridLayout(4,3));
		String [] name= {"1","2","3","4","5","6","7","8","9","0",".","BackSpace"};
		for(int i=0;i<name.length;i++) {
			jButton[i]=new JButton(name[i]);
			jButton[i].addActionListener(this);//每个按钮添加事件触发器
			rightPanel.add(jButton[i]);//添加按钮
		}
		//rightPanel结束
		
		//结果按钮监听者
		resultButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(!ATextField.getText().trim().equals("")&&//trim()作用是去除两端空白字符的字符串
						!ATextField.getText().trim().isEmpty()&&
						!BTextField.getText().trim().equals("")&&
						!BTextField.getText().trim().isEmpty()&&
						!CTextField.getText().trim().equals("")&&
						!CTextField.getText().trim().isEmpty()) {//判断条件是,先去除两边的空白字符串(如果有的话),然后判断两个输入框是否为空
					A=Double.parseDouble(ATextField.getText());//先取得文本款的内容,然后转换成double类型
					B=Double.parseDouble(BTextField.getText());
					C=Double.parseDouble(CTextField.getText());
					if(A+B>C&&A+C>B&&B+C>A) {
						lengthTextField.setText(String.valueOf(sanjiaoxingLenght()));//先计算周长,然后再转换成String,放在周长的文本框内
						areaTextField.setText(String.valueOf(sanjiaoxingArea()));//先计算面积,然后再转换成String,放在面积的文本框内
					}
					else {
						JOptionPane.showMessageDialog(null, "非三角形!", "标题【出错啦】", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		
		//清除按钮监听者
		clearButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ATextField.setText("");//文本框全都清空
				BTextField.setText("");
				CTextField.setText("");
				lengthTextField.setText("");
				areaTextField.setText("");
			}
		});
		
		//注册焦点与事件
		ATextField.addFocusListener(this);
		BTextField.addFocusListener(this);
		CTextField.addFocusListener(this);
		ATextField.addActionListener(this);
		BTextField.addActionListener(this);
		CTextField.addActionListener(this);
		
		add(rightPanel,BorderLayout.EAST);
		add(leftPanel,BorderLayout.WEST);
		this.setVisible(true);
	}
	
	//计算周长
	public double sanjiaoxingLenght() {
		return A+B+C;
	}
		
	//计算面积
	public double sanjiaoxingArea() {
		double s = (A+B+C)/2.0;
		double v = s*(s-A)*(s-B)*(s-C);
		return Math.sqrt(v);
	}
		

	//当光标在A的输入文本框内,flag=1;当光标在B的输入文本框内,flag=2;当光标在C的输入文本框内,flag=3,
		public void focusGained(FocusEvent e) {
			// TODO 自动生成的方法存根
			JComponent component = (JComponent)e.getSource();//getSource()获取事件所作用的对象,并转换成JComponent类型
			if(component==ATextField) {
				flag=1;
			}
			else if(component==BTextField) {
				flag=2;
			}
			else if(component==CTextField) {
				flag=3;
			}
			
		}

		@Override
		public void focusLost(FocusEvent e) {
			// TODO 自动生成的方法存根
			
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			JButton button=(JButton)e.getSource();//获取事件所作用的对象,并传值给button
			String s=e.getActionCommand();//获得当前事件所对应按钮里面的字符串
			if(flag==1) {
				//inputTextField=ATextField;
				inputNum(s,ATextField);
			}
			else if(flag==2) {
			//	inputTextField=BTextField;
				inputNum(s,BTextField);
			}
			else if(flag==3) {
				//inputTextField=CTextField;
				inputNum(s,CTextField);
			}
		}
		
		//按下右边的计算器,左边会有相对应的数字出来
		public void inputNum(String s,JTextField tf) {
			if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals(".")||
					s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")||s.equals("0")) {//字符串等于1、2、3……时
				if(tf.getText().equals("0")) tf.setText(s);//如果当前文本框没有东西,添加该字符串
				else tf.setText(tf.getText()+s);//否则在基础上添加字符串
			}
			else if(s.equals("BackSpace")) {//按钮为删除键时
				if(!tf.getText().equals("")) {//非空时
					tf.setText(tf.getText().substring(0, tf.getText().length()-1));//substrin方法返回字符串的子字符串。
				}
			}
		}
		
		/*public static void main(String []args) {
			new sanjiaoPanel();
		}
	*/
}

(4)、yuanPanel
package Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;


public class yuanPanel extends JPanel implements ActionListener,FocusListener{
	JPanel leftPanel,rightPanel,buttonPanel;
	JButton resultButton,clearButton;
	JTextField radiusTextField,lengthTextField,areaTextField,inputTextField;
	JButton []jButton = new JButton[12];//右边的计算器
	double r;//半径
	final static double PI=3.14159;
	int flag;//判断标志
	
	public yuanPanel() {
		//super("圆计算");
		this.setLayout(new BorderLayout());
		//this.setSize(450, 250);
		
		//leftPanel开始
		leftPanel=new JPanel();
		leftPanel.setLayout(new GridLayout(4,1));//4行1列
			//第一行开始
		JPanel p1=new JPanel();
		JLabel l1_1=new JLabel("请输入圆的半径:");
		radiusTextField=new JTextField(7);
		p1.add(l1_1);
		p1.add(radiusTextField);
		leftPanel.add(p1);
			//第一行结束
		
			//第二行开始
		buttonPanel=new JPanel();
		resultButton=new JButton("计算结果:");
		clearButton=new JButton("清空");
		buttonPanel.add(resultButton);
		buttonPanel.add(clearButton);
		leftPanel.add(buttonPanel);
			//第二行结束
			
			//第三行开始
		JPanel p2=new JPanel();
		JLabel l2=new JLabel("圆的周长:");
		lengthTextField=new JTextField(15);
		p2.add(l2);
		p2.add(lengthTextField);
		leftPanel.add(p2);
			//第三行结束
		
			//第四行开始
		JPanel p3=new JPanel();
		JLabel l3=new JLabel("圆的面积:");
		areaTextField=new JTextField(15);
		p3.add(l3);
		p3.add(areaTextField);		
		leftPanel.add(p3);
			//第四行结束
		//leftPanel结束
		
		//rightPanel开始
		rightPanel=new JPanel();
		rightPanel.setLayout(new GridLayout(4,3));
		String [] name= {"1","2","3","4","5","6","7","8","9","0",".","BackSpace"};
		for(int i=0;i<name.length;i++) {
			jButton[i]=new JButton(name[i]);
			jButton[i].addActionListener(this);//每个按钮添加事件触发器
			rightPanel.add(jButton[i]);//添加按钮
		}
		//rightPanel结束
		
		//结果按钮监听者
		resultButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(!radiusTextField.getText().trim().equals("")&&//trim()作用是去除两端空白字符的字符串
						!radiusTextField.getText().trim().isEmpty()) {//判断条件是,先去除两边的空白字符串(如果有的话),然后判断两个输入框是否为空
					r=Double.parseDouble(radiusTextField.getText());//先取得文本款的内容,然后转换成double类型
					
						lengthTextField.setText(String.valueOf(yuanLenght()));//先计算周长,然后再转换成String,放在周长的文本框内
						areaTextField.setText(String.valueOf(yuanArea()));//先计算面积,然后再转换成String,放在面积的文本框内
				}
			}
		});
		
		//清除按钮监听者
		clearButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				radiusTextField.setText("");//文本框全都清空
				lengthTextField.setText("");
				areaTextField.setText("");
			}
		});
		
		//注册焦点与事件
		radiusTextField.addFocusListener(this);
		radiusTextField.addActionListener(this);
		
		
		add(rightPanel,BorderLayout.EAST);
		add(leftPanel,BorderLayout.WEST);
		this.setVisible(true);
	}
	
		//计算周长
		public double yuanLenght() {
			return 2*PI*r;
		}
			
		//计算面积
		public double yuanArea() {
			return PI*r*r;
		}
	
		//当光标在半径的输入文本框内,flag=1
		public void focusGained(FocusEvent e) {
			// TODO 自动生成的方法存根
			JComponent component = (JComponent)e.getSource();//getSource()获取事件所作用的对象,并转换成JComponent类型
			if(component==radiusTextField) {
				flag=1;
			}			
		}
		
		@Override
		public void focusLost(FocusEvent e) {
			// TODO 自动生成的方法存根
			
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			JButton button=(JButton)e.getSource();//获取事件所作用的对象,并传值给button
			String s=e.getActionCommand();//获得当前事件所对应按钮里面的字符串
			if(flag==1) {
				//inputTextField=ATextField;
				inputNum(s,radiusTextField);
			}
		}
		
		//按下右边的计算器,左边会有相对应的数字出来
		public void inputNum(String s,JTextField tf) {
			if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals(".")||
					s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")||s.equals("0")) {//字符串等于1、2、3……时
				if(tf.getText().equals("0")) tf.setText(s);//如果当前文本框没有东西,添加该字符串
				else tf.setText(tf.getText()+s);//否则在基础上添加字符串
			}
			else if(s.equals("BackSpace")) {//按钮为删除键时
				if(!tf.getText().equals("")) {//非空时
					tf.setText(tf.getText().substring(0, tf.getText().length()-1));//substrin方法返回字符串的子字符串。
				}
			}
		}
		
		
	/*public static void main(String [] args) {
		new yuanPanel();
	}*/
	
}

(5)、yuanzhuPanel
package Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;

public class yuanzhuPanel extends JPanel implements ActionListener,FocusListener{

	JPanel leftPanel,rightPanel,buttonPanel;
	JButton resultButton,clearButton;
	JTextField rTextField,hightTextField,lengthTextField,areaTextField;
	JButton []jButton = new JButton[12];//右边的计算器
	double r,h;
	final static double PI=3.14159;
	int flag;//判断标志
	
	public yuanzhuPanel() {
		//super("圆柱计算");
		this.setLayout(new BorderLayout());
		//this.setSize(500, 250);
		
		//leftPanel开始
		leftPanel=new JPanel();
		leftPanel.setLayout(new GridLayout(5,1));//5行1列
			//第一行开始
		JPanel p1=new JPanel();
		JLabel l1_1=new JLabel("请输入圆柱的底面半径:");
		rTextField=new JTextField(8);
		p1.add(l1_1);
		p1.add(rTextField);
		leftPanel.add(p1);
			//第一行结束
			//第二行开始
		JPanel p2=new JPanel();
		JLabel l2=new JLabel("请输入圆柱的高:            ");
		hightTextField=new JTextField(8);
		p2.add(l2);
		p2.add(hightTextField);
		leftPanel.add(p2);
			//第二行结束
			//第三行开始
		buttonPanel=new JPanel();
		resultButton=new JButton("计算结果:");
		clearButton=new JButton("清空");
		buttonPanel.add(resultButton);
		buttonPanel.add(clearButton);
		leftPanel.add(buttonPanel);
			//第三行结束
			//第四行开始
		JPanel p3=new JPanel();
		JLabel l3=new JLabel("圆柱表面积:");
		lengthTextField=new JTextField(15);
		p3.add(l3);
		p3.add(lengthTextField);
		leftPanel.add(p3);
			//第四行结束
			//第五行开始
		JPanel p4=new JPanel();
		JLabel l4=new JLabel("圆柱体积:    ");
		areaTextField=new JTextField(15);
		p4.add(l4);
		p4.add(areaTextField);		
		leftPanel.add(p4);
			//第五行结束
		//leftPanel结束
		
		//rightPanel开始
		rightPanel=new JPanel();
		rightPanel.setLayout(new GridLayout(4,3));
		String [] name= {"1","2","3","4","5","6","7","8","9","0",".","BackSpace"};
		for(int i=0;i<name.length;i++) {
			jButton[i]=new JButton(name[i]);
			jButton[i].addActionListener(this);//每个按钮添加事件触发器
			rightPanel.add(jButton[i]);//添加按钮
		}
		//rightPanel结束
		
		//结果按钮监听者
		resultButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(!rTextField.getText().trim().equals("")&&//trim()作用是去除两端空白字符的字符串
						!rTextField.getText().trim().isEmpty()&&
						!hightTextField.getText().trim().equals("")&&
						!hightTextField.getText().trim().isEmpty()) {//判断条件是,先去除两边的空白字符串(如果有的话),然后判断两个输入框是否为空
					r=Double.parseDouble(rTextField.getText());//先取得文本款的内容,然后转换成double类型
					h=Double.parseDouble(hightTextField.getText());
					
						lengthTextField.setText(String.valueOf(yuanzhuLenght()));//先计算周长,然后再转换成String,放在周长的文本框内
						areaTextField.setText(String.valueOf(yuanzhuArea()));//先计算面积,然后再转换成String,放在面积的文本框内
				}
			}
		});
		
		//清除按钮监听者
		clearButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				rTextField.setText("");//文本框全都清空
				hightTextField.setText("");
				lengthTextField.setText("");
				areaTextField.setText("");
			}
		});
		
		//注册焦点与事件
		rTextField.addFocusListener(this);
		hightTextField.addFocusListener(this);
		rTextField.addActionListener(this);
		hightTextField.addActionListener(this);
		
		
		add(rightPanel,BorderLayout.EAST);
		add(leftPanel,BorderLayout.WEST);
		this.setVisible(true);
	}
	
		//计算周长
		public double yuanzhuLenght() {
			return 2*PI*r*r+2*PI*r*h;
		}
			
		//计算面积
		public double yuanzhuArea() {
			
			return PI*r*r*h;
		}
	
		//当光标在A的输入文本框内,flag=1;当光标在B的输入文本框内,flag=2;当光标在C的输入文本框内,flag=3,
		public void focusGained(FocusEvent e) {
			// TODO 自动生成的方法存根
			JComponent component = (JComponent)e.getSource();//getSource()获取事件所作用的对象,并转换成JComponent类型
			if(component==rTextField) {
				flag=1;
			}
			else if(component==hightTextField) {
				flag=2;
			}
			
		}
		
		@Override
		public void focusLost(FocusEvent e) {
			// TODO 自动生成的方法存根
			
		}
	
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			JButton button=(JButton)e.getSource();//获取事件所作用的对象,并传值给button
			String s=e.getActionCommand();//获得当前事件所对应按钮里面的字符串
			if(flag==1) {
				//inputTextField=ATextField;
				inputNum(s,rTextField);
			}
			else if(flag==2) {
			//	inputTextField=BTextField;
				inputNum(s,hightTextField);
			}
		}
		
		//按下右边的计算器,左边会有相对应的数字出来
		public void inputNum(String s,JTextField tf) {
			if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals(".")||
					s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")||s.equals("0")) {//字符串等于1、2、3……时
				if(tf.getText().equals("0")) tf.setText(s);//如果当前文本框没有东西,添加该字符串
				else tf.setText(tf.getText()+s);//否则在基础上添加字符串
			}
			else if(s.equals("BackSpace")) {//按钮为删除键时
				if(!tf.getText().equals("")) {//非空时
					tf.setText(tf.getText().substring(0, tf.getText().length()-1));//substrin方法返回字符串的子字符串。
				}
			}
		}
		
		
		
	/*public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new yuanzhuPanel();
	}*/

}

  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值