日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。

日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。

1 作业中的接口与类
日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。_第1张图片
2 抽象类用于所有空间实现JPANE

package comprehensive;

import javax.swing.JPanel;
import javax.swing.JTextField;

public abstract class AbstractPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	public abstract JTextField getInputTextField();
}

2 通用接口和抽象了

// 通用抽象类
package comprehensive;
public abstract class Shape {
public abstract double area();
}

//通用接口
package comprehensive;
public interface cal {
final double PI=3.14;
double perimeter();
}


3 圆形矩形三角形圆柱的JPANE及实现类
1)接口 及实现类

//圆形jpane
package comprehensive;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.DecimalFormat;

public class CirclePanel extends AbstractPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtradius;
	private JTextField txtpre;
	private JTextField txtarea;
	private JTextField input;  //用于获取当前获得焦点的文本框
	JButton btncal,btnclear;
	/**
	 * 创建圆形计算面板.
	 */
	public CirclePanel() {
	//	圆面板设为网格布局方式,屏蔽setSize方法设置的大小,使用setPreferredSize(new Dimension(200, 220))设置大小
		setLayout(new GridLayout(4, 1, 0, 0));
	//设置半径信息面板	
		JPanel panelradius = new JPanel();
		add(panelradius);
		JLabel lblradius = new JLabel("圆的半径是");
		panelradius.add(lblradius);
		txtradius = new JTextField();
		txtradius.addFocusListener(new Handle());
		panelradius.add(txtradius);
		txtradius.setColumns(10);
		//设置放置按钮的面板
		JPanel panelbutton = new JPanel();
		add(panelbutton);	
		btncal = new JButton("计算");
		panelbutton.add(btncal);
		btnclear = new JButton("清除");
		panelbutton.add(btnclear);	
		JPanel panelper = new JPanel();
		add(panelper);
		//设置显示周长信息的面板
		JLabel lblpre = new JLabel("圆的周长是");
		panelper.add(lblpre);	
		txtpre = new JTextField();
		panelper.add(txtpre);
		txtpre.setColumns(10);
		//设置显示面积信息的面板
		JPanel panelarea = new JPanel();
		add(panelarea);
		JLabel lblarea = new JLabel("圆的面积是");
		panelarea.add(lblarea);
		txtarea = new JTextField();
		panelarea.add(txtarea);
		txtarea.setColumns(10);
		//为“计算”按钮和“清除”按钮设置监听
		btnclear.addActionListener(new ActionListener() {
			//清除两个文本框中信息
			@Override
			public void actionPerformed(ActionEvent e) {
				txtarea.setText("");
				txtpre.setText("");
				txtradius.setText("");
			}
		});
		btncal.addActionListener(new ActionListener() {
			//计算并显示在文本框中
			@Override
			public void actionPerformed(ActionEvent e) {
				double r=Double.parseDouble(txtradius.getText());
				Circle cir=new Circle(r);
				double ar=cir.area();
				String formatAr=new DecimalFormat(".##").format(ar);
				double per=cir.perimeter();
				String formatPer=new DecimalFormat(".##").format(per);
				txtpre.setText(formatPer);
			    txtarea.setText(formatAr);
			}
		});
	}
class Handle extends FocusAdapter{
	public void focusGained(FocusEvent e) {
		input=(JTextField) e.getSource();   //设置输入文本框为当前获取焦点的文本框
	}
}
	public JTextField getInputTextField(){
			return input;   //该方法在keyPanel中调用,用于设置接收输入的文本框
	}
}

//圆形计算接类
package comprehensive;
public class Circle extends Shape implements cal{
	private double radius;
	
public Circle(double radius) {
		super();
		this.radius = radius;
	}
	public double perimeter() {
		return 2*Math.PI*radius;
	}
	public double area() {
		return radius*radius*PI;
	}
}

//Cyiner.java
package comprehensive;

public class Cyiner extends Shape implements cal {
	private double radius;
	private double height;
	public Cyiner(double radius,double height) {
		super();
		this.radius = radius;
		this.height = height;
	}
	public double perimeter() {
		return PI * radius * radius * height;
	}
	public double area() {
		double s1 = 2 * PI * radius * height;
		double s2 = PI * radius * radius;
		return s1 + s2;
	}
}

//CyPanel.java
package comprehensive;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CyPanel extends AbstractPanel {
	private static final long serialVersionUID = 1L;
	JTextField input;
	private JTextField txt_radius;
	private JTextField txt_height;
	private JTextField txt_per;
	private JTextField txt_area;
	JButton button_calc,button_clear;
	public CyPanel() {
		setLayout(new GridLayout(5, 1));
		
		Handle handle = new Handle();
		
		JPanel p1 = new JPanel();
		JLabel l1 = new JLabel("圆柱体的半径是");
		txt_radius = new JTextField(8);
		txt_radius.addFocusListener(handle);
		p1.add(l1);
		p1.add(txt_radius);
		add(p1);
		
		JPanel p2 = new JPanel();
		JLabel l2 = new JLabel("  圆柱体的高是  ");
		txt_height = new JTextField(8);
		txt_height.addFocusListener(handle);
		p2.add(l2);
		p2.add(txt_height);
		add(p2);
		
		JPanel p3 = new JPanel();
		button_calc = new JButton("计算");
		button_calc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Cyiner cy = new Cyiner(Double.parseDouble(txt_radius.getText()), 
						Double.parseDouble(txt_height.getText()));
				txt_area.setText("" + cy.area());
				txt_per.setText("" + cy.perimeter());
			}
		});
		button_clear = new JButton("清除");
		button_clear.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				txt_area.setText(null);
				txt_height.setText(null);
				txt_per.setText(null);
				txt_radius.setText(null);
			}
		});
		p3.add(button_calc);
		p3.add(button_clear);
		add(p3);
		
		JPanel p4 = new JPanel();
		JLabel l3 = new JLabel("圆柱体的体积是");
		txt_per = new JTextField(10);
		p4.add(l3);
		p4.add(txt_per);
		add(p4);
		
		JPanel p5 = new JPanel();
		JLabel l4 = new JLabel("圆柱体的面积是");
		txt_area = new JTextField(10);
		p5.add(l4);
		p5.add(txt_area);
		add(p5);
	}
	public JTextField getInputTextField() {
		return input;
	}
	class Handle extends FocusAdapter{
		public void focusGained(FocusEvent e) {
			input=(JTextField) e.getSource();   //设置输入文本框为当前获取焦点的文本框
		}
	}

}

//Triangle.java
package comprehensive;

public class Triangle extends Shape implements cal{
	private double a,b,c;
	public Triangle(double x,double y,double z) {
		super();
		a = x;
		b = y;
		c = z;
	}
	public double perimeter() {
		return a + b + c;
	}
	public double area() {
		double p = perimeter() / 2;
		double s = Math.sqrt(p * (p-a) * (p-b) * (p-c));
		return s;
	}
}

//TrianglePanel.java
package comprehensive;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TrianglePanel extends AbstractPanel {
	private static final long serialVersionUID = 1L;
	private JTextField txt_a;
	private JTextField txt_b;
	private JTextField txt_c;
	private JTextField txtpre;
	private JTextField txtarea;
	private JTextField input;  //用于获取当前获得焦点的文本框
	JButton btncal,btnclear;
	
	public TrianglePanel() {
		setLayout(new GridLayout(6, 1));
		
		JPanel pan = new JPanel();
		JLabel l1 = new JLabel("第一条边长是");
		txt_a = new JTextField(8);
		pan.add(l1);
		pan.add(txt_a);
		add(pan);
		
		JPanel p2 = new JPanel();
		JLabel l2 = new JLabel("第二条边长是");
		txt_b = new JTextField(8);
		p2.add(l2);
		p2.add(txt_b);
		add(p2);
		
		JPanel p3 = new JPanel();
		JLabel l3 = new JLabel("第三条边长是");
		txt_c = new JTextField(8);
		p3.add(l3);
		p3.add(txt_c);
		add(p3);
		
		Handle handle = new Handle();
		Handles handles = new Handles();
		txt_a.addFocusListener(handle);
		txt_b.addFocusListener(handle);
		txt_c.addFocusListener(handle);
		
		
		JPanel p4 = new JPanel();
		btncal = new JButton("计算");
		btnclear = new JButton("清除");
		p4.add(btncal);
		p4.add(btnclear);
		add(p4);
		btncal.addActionListener(handles);
		btnclear.addActionListener(handles);
		
		JPanel p5 = new JPanel();
		JLabel l4 = new JLabel("三角形的周长是");
		txtpre = new JTextField(10);
		p5.add(l4);
		p5.add(txtpre);
		add(p5);
		
		JPanel p6 = new JPanel();
		JLabel l5 = new JLabel("三角形的面积是");
		txtarea = new JTextField(10);
		p6.add(l5);
		p6.add(txtarea);
		add(p6);
	}
	class Handle extends FocusAdapter{
		public void focusGained(FocusEvent e) {
			input=(JTextField) e.getSource();   //设置输入文本框为当前获取焦点的文本框
		}
	}
		public JTextField getInputTextField(){
				return input;   //该方法在keyPanel中调用,用于设置接收输入的文本框
		}
	class Handles implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getSource() == btncal) {
				Triangle t = new Triangle(Double.parseDouble(txt_a.getText()),
						Double.parseDouble(txt_b.getText()), Double.parseDouble(txt_c.getText()));
				txtarea.setText("" + t.area());
				txtpre.setText("" + t.perimeter());
			}
			if(arg0.getSource() == btnclear) {
				txt_a.setText(null);
				txt_b.setText(null);
				txt_c.setText(null);
				txtarea.setText(null);
				txtpre.setText(null);
			}
		}
		
	}
}

//Rect.java
package comprehensive;
public class Rect extends Shape implements cal{
	private double len;
	private double wedth;
	public Rect(double len,double wedth) {
		super();
		this.len = len;
		this.wedth = wedth;
	}
	public double perimeter() {
		return len + wedth;
	}
	public double area() {
		return len * wedth;
	}
}

//RectPanel.java
package comprehensive;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class RectPanel extends AbstractPanel {
	private static final long serialVersionUID = 1L;
	private JTextField txtlength,txtwidth;
	private JTextField txtpre;
	private JTextField txtarea;
	private JTextField input;  //用于获取当前获得焦点的文本框
	JButton btncal,btnclear;
	/**
	 * 创建圆形计算面板.
	 */
	public RectPanel() {
	//	圆面板设为网格布局方式,屏蔽setSize方法设置的大小,使用setPreferredSize(new Dimension(200, 220))设置大小
		setLayout(new GridLayout(5, 1, 0, 0));
	//设置长度信息面板	
		JPanel panellength = new JPanel();
		add(panellength);
		JLabel lbllength = new JLabel("矩形的长是");
		panellength.add(lbllength);
		txtlength = new JTextField();
		txtlength.addFocusListener(new Handle());
		panellength.add(txtlength);
		txtlength.setColumns(10);
		//设置宽度信息面板	
				JPanel panelwidth= new JPanel();
				add(panelwidth);
				JLabel lblwidth = new JLabel("矩形的宽是");
				panelwidth.add(lblwidth);
				txtwidth = new JTextField();
				panelwidth.add(txtwidth);
				txtwidth.addFocusListener(new Handle());
				txtwidth.setColumns(10);
		//设置放置按钮的面板
		JPanel panelbutton = new JPanel();
		add(panelbutton);
		btncal = new JButton("计算");
		panelbutton.add(btncal);
		btncal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double a = Double.parseDouble(txtlength.getText());
				double b = Double.parseDouble(txtwidth.getText());
				Rect rect = new Rect(a, b);
				txtpre.setText(""+rect.perimeter());
				txtarea.setText(""+rect.area());
			}
		});
		btnclear = new JButton("清除");
		panelbutton.add(btnclear);	
		btnclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtarea.setText(null);
				txtlength.setText(null);
				txtwidth.setText(null);
				txtpre.setText(null);
			}
		});
		JPanel panelper = new JPanel();
		add(panelper);
		//设置显示周长信息的面板
		JLabel lblpre = new JLabel("矩形的周长是");
		panelper.add(lblpre);	
		txtpre = new JTextField();
		panelper.add(txtpre);
		txtpre.setColumns(10);
		//设置显示面积信息的面板
		JPanel panelarea = new JPanel();
		add(panelarea);
		JLabel lblarea = new JLabel("矩形的面积是");
		panelarea.add(lblarea);
		txtarea = new JTextField();
		panelarea.add(txtarea);
		txtarea.setColumns(10);
	}
	class Handle extends FocusAdapter{
		public void focusGained(FocusEvent e) {
			input=(JTextField) e.getSource();   //设置输入文本框为当前获取焦点的文本框
		}
	}
		public JTextField getInputTextField(){
				return input;   //该方法在keyPanel中调用,用于设置接收输入的文本框
		}
	}


//输入键盘
package comprehensive;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class KeyPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	/**
	 * 创建输入键盘面板
	 */
	JButton[] keyButton=new JButton[12];
	String[] num={"1","2","3","4","5","6","7","8","9","0",".","BackSpace"};
	AbstractPanel selectedPanel;  //表示当前选择的图形面板,键入数据要传递到当前选择的面板中
	JTextField inputTextField;
	
	public KeyPanel() {
		setPreferredSize(new Dimension(350, 230));
		setLayout(new GridLayout(4,3));
		Border lb=BorderFactory.createLineBorder(Color.gray, 2);
		setBorder(lb);
		Handle handle=new Handle();
		for(int i=0;i<12;i++){
			keyButton[i]=new JButton(num[i]);
		    keyButton[i].setFont(new Font("Arial",Font.BOLD,15));
			keyButton[i].setForeground(Color.BLACK);
			keyButton[i].addActionListener(handle);
			add(keyButton[i]);
		}
	}
class Handle implements ActionListener{
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button=(JButton)e.getSource();  //获取事件源,看是哪个按钮被按下
		inputTextField=selectedPanel.getInputTextField();   //从传递来的形状面板中获取当前得到焦点的文本框
		inputNumber(inputTextField, button);  //调用inputNumber方法,在该文本框中输入数据	
	}
}
public void setSelectedPanel(AbstractPanel selected){
	selectedPanel = selected;
}

public void  inputNumber(JTextField tf,JButton button){  //根据不同事件源在文本框中显示不同信息
	String oldString=tf.getText();  //获取文本框中已有信息
	if(oldString==null){
		tf.setText(" ");
	}
	String subStr=oldString.substring(0, oldString.length());  //根据不同事件源的不同情况,设置不同的值
	String newString=button.getText();   
	if(newString.equals("BackSpace")){
		tf.setText(subStr);
	}
	else if(newString.equals(".")){
		tf.setText(oldString+".");
	}
	else{
		tf.setText(oldString+newString);
	}
}

}

//主运行程序 采用网格布局管理方式
package comprehensive;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;

public class CalculatorWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private KeyPanel keypanel;
	private AbstractPanel cirPanel,rectPanel,trianPanel,cyPanel;
	private CardLayout card;
	//菜单选项需要进行事件处理
	JMenuItem menuExit;
	JRadioButtonMenuItem menuCircle, menuRect,menuTangle,menuCyinder;
	JPanel panelWest;  //根据选项显示面板
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CalculatorWindow frame = new CalculatorWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CalculatorWindow() {
		setTitle("图形计算器");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 590, 306);
		Container con=getContentPane();
		//con.setLayout(new GridLayout(1,2));
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		Handle handle=new Handle();
		//添加“文件”菜单、菜单项及为菜单项添加监听
		JMenu menuFile = new JMenu("文件");
		menuBar.add(menuFile);
		menuExit = new JMenuItem("退出");
		menuExit.addActionListener(handle);
		menuFile.add(menuExit);
		//添加“计算”菜单、菜单项及为菜单项添加监听
		JMenu menuCalcu = new JMenu("计算");
		menuBar.add(menuCalcu);
		ButtonGroup group = new ButtonGroup();
		menuCircle = new JRadioButtonMenuItem("圆形");
		menuCircle.addActionListener(handle);
		menuCalcu.add(menuCircle);
		
		menuRect = new JRadioButtonMenuItem("矩形");
		menuRect.addActionListener(handle);
		menuCalcu.add(menuRect);
		
		menuTangle = new JRadioButtonMenuItem("三角形");
		menuTangle.addActionListener(handle);
		menuCalcu.add(menuTangle);
		
		menuCyinder = new JRadioButtonMenuItem("圆柱体");
		menuCyinder.addActionListener(handle);
		menuCalcu.add(menuCyinder);
		
		group.add(menuCyinder);
		group.add(menuTangle);
		group.add(menuRect);
		group.add(menuCircle);
		
		//主窗口左方面板为卡片布局方式,按需求显示形状面板
		panelWest = new JPanel();
		panelWest.setPreferredSize(new Dimension(220, 0));
		panelWest.setSize(220, 280);
		card = new CardLayout();
		panelWest.setLayout(card);
		//添加卡片面板,并为其取卡片名
		cirPanel=new CirclePanel();
		rectPanel=new RectPanel();
		trianPanel = new TrianglePanel();
		cyPanel = new CyPanel();
		panelWest.add("0",cirPanel);
		panelWest.add("1",rectPanel);
		panelWest.add("2",trianPanel);
		panelWest.add("3",cyPanel);
		con.add(panelWest,BorderLayout.WEST);
		
		//主窗口右侧面板中显示keyPanel,用于输入数字
		JPanel panelEast = new JPanel();
		panelEast.setPreferredSize(new Dimension(360, 0));
		keypanel=new KeyPanel();
		keypanel.setSelectedPanel(cirPanel);  //设置启动时,显示的图形面板为cirPanel,通过参数传递给keypanel,用于接收输入
		panelEast.add(keypanel);
		con.add(panelEast,BorderLayout.EAST);	
	}
	
//ActionEvent事件处理,根据菜单选项,显示对应卡片
	class Handle implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == menuExit){
                System.exit(0);
			}else if(e.getSource() == menuCircle){
				card.show(panelWest, "0");
				keypanel.setSelectedPanel(cirPanel);
			}else if(e.getSource() == menuRect){
				card.show(panelWest, "1");
				keypanel.setSelectedPanel(rectPanel);
			}else if(e.getSource() == menuTangle) {
				card.show(panelWest, "2");
				keypanel.setSelectedPanel(trianPanel);
			}else if(e.getSource() == menuCyinder) {
				card.show(panelWest, "3");
				keypanel.setSelectedPanel(cyPanel);
			}
		}
		
	}

}

运行结果

日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。_第2张图片
日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。_第3张图片
日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。_第4张图片
日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。_第5张图片
日常生活中,人们需要对某些常见图形和几何体做计算面积,体积,周长等相关计算。设计一个形状计算器,完成此功能。通过对菜单的选择,可以对不同的形状进行计算。_第6张图片

你可能感兴趣的:(JAVA,swing,JPANE)