成果截图

![](https://jrboy.oss-cn-chengdu.aliyuncs.com/wp-content/uploads/2019/06/2019060715114862-300x200.png)![](https://jrboy.oss-cn-chengdu.aliyuncs.com/wp-content/uploads/2019/06/2019060715120368-300x200.png)

代码展示

###### 1,程序主窗体代码(CalculatorWindow.java)

```
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class CalculatorWindow extends JFrame implements ActionListener,ItemListener {

CardLayout mycard;
JPanel controPanel,pCenter;
JComboBox chooseList;
JMenuBar menuBar;
JMenu menu;
JMenuItem mainWindow,exit;

public CalculatorWindow(){
setTitle("常见几何图形计算器");

menuBar = new JMenuBar();
menu = new JMenu("操作");
mainWindow = new JMenuItem("主界面");
mainWindow.addActionListener(this);
exit = new JMenuItem("退出");
exit.addActionListener(this);
menu.add(mainWindow);
menu.add(exit);
menuBar.add(menu);
setJMenuBar(menuBar);

mycard = new CardLayout();
pCenter = new JPanel();
pCenter.setLayout(mycard);
add(pCenter,"Center");

controPanel = new JPanel();
controPanel.setLayout(new BorderLayout());

chooseList = new JComboBox();
chooseList.addItem("请点击下拉列表选择");
chooseList.addItem("矩形的基本计算");
chooseList.addItem("圆的基本计算");
chooseList.addItem("三角形的基本计算");
chooseList.addItem("圆柱的基本计算");
chooseList.addItemListener(this);

ImageIcon icon = new ImageIcon(图片地址);
JButton imageButton = new JButton(icon);

controPanel.add(imageButton,"Center");
controPanel.add(chooseList,"North");

pCenter.add("0",controPanel);
pCenter.add("1",new RectanglePanel());
pCenter.add("2",new CirclePanel());
pCenter.add("3",new TrianglePanel());
pCenter.add("4",new CylinderPanel());

setBounds(100, 100, 700, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

}

public void itemStateChanged(ItemEvent e) {
int index = chooseList.getSelectedIndex();
String choice = String.valueOf(index);
mycard.show(pCenter, choice);

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == mainWindow) {
mycard.first(pCenter);
chooseList.setSelectedIndex(0);
}
else if (e.getSource() == exit) {
System.exit(0);
}
}

public static void main(String[] args) {
new CalculatorWindow();
}

}
```

###### 2,BoxPanel.java和AbstractPanel.java的实现

```
//代码(BoxPanel.java)

import java.awt.Font;

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

public class BoxPanel extends JPanel{
JTextField text;

public BoxPanel(String s,int n) {

Box box = Box.createHorizontalBox();
box.add(new JLabel(s));
text = new JTextField(" ",n);

text.setHorizontalAlignment(JTextField.RIGHT);

text.setFont(new Font("Arial",Font.BOLD,15));
box.add(text);
add(box);

}
public JTextField getJTextField() {
return text;
}
}

//代码(AbstractPanel.java)

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

public abstract class AbstractPanel extends JPanel {
public abstract JTextField getInputTextField();
}
```

###### 3,矩形计算器的实现(RectanglePanel.java)

```
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RectanglePanel extends AbstractPanel implements ActionListener,FocusListener{

JButton resultButton,clearButton;
JPanel leftPanel,rightPanel,buttonPanel;
JTextField widthTextField,heightTextField;
JTextField lengthTextField,areaTextField,inputTextField;
BoxPanel bpWidth,bpHeight,bpLength,bpArea;

public RectanglePanel() {
setLayout(new GridLayout(1,2));
rightPanel = new KeyJPanel(this);
leftPanel = new JPanel();

Box box = Box.createVerticalBox();
bpWidth = new BoxPanel("请输入矩形的宽", 10);
widthTextField = bpWidth.getJTextField();

widthTextField.addFocusListener(this);
bpHeight = new BoxPanel("请输入矩形的高",10);
heightTextField = bpHeight.getJTextField();
heightTextField.addFocusListener(this);
buttonPanel = new JPanel();
resultButton = new JButton("计算结果");

resultButton.addActionListener(this);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
buttonPanel.add(resultButton);
buttonPanel.add(clearButton);
bpLength = new BoxPanel("矩形的周长:", 20);
lengthTextField = bpLength.getJTextField();
bpArea = new BoxPanel("矩形的面积:",20);
this.areaTextField = this.bpArea.getJTextField();

box.add(bpWidth);
box.add(bpHeight);
box.add(buttonPanel);
box.add(bpLength);
box.add(bpArea);
leftPanel.add(box);

add(leftPanel);
add(rightPanel);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == resultButton) {
try {
double width = Double.parseDouble(widthTextField.getText());
double height = Double.parseDouble(heightTextField.getText());
lengthTextField.setText(""+2*(width + height));
areaTextField.setText(""+width*height);
} catch (NumberFormatException e1) {

JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE);
}
}

else if (e.getSource() == clearButton) {
widthTextField.setText(" ");
heightTextField.setText(" ");
}
}

public void focusGained(FocusEvent e) {
inputTextField = (JTextField)e.getSource();
}

public void focusLost(FocusEvent e) {}

public JTextField getInputTextField() {
return inputTextField;
}

}
```

###### 4,圆计算器的实现(CirclePanel.java)

```
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CirclePanel extends AbstractPanel implements ActionListener,FocusListener{

JButton resultButton,clearButton;
JPanel leftPanel,rightPanel,buttonPanel;
JTextField radiusTextField;
JTextField lengthTextField,areaTextField,inputTextField;
BoxPanel bpRadius,bpLength,bpArea;

public CirclePanel() {
setLayout(new GridLayout(1,2));
rightPanel = new KeyJPanel(this);
leftPanel =new JPanel();

Box box = Box.createVerticalBox();
bpRadius = new BoxPanel("请输入圆的半径:", 10);
radiusTextField = bpRadius.getJTextField();

radiusTextField.addFocusListener(this);
buttonPanel = new JPanel();
resultButton = new JButton("计算结果:");

resultButton.addActionListener(this);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
buttonPanel.add(resultButton);
buttonPanel.add(clearButton);
bpLength = new BoxPanel("圆的周长:", 20);
lengthTextField = bpLength.getJTextField();
bpArea = new BoxpPanel("圆的面积:", 20);
areaTextField = bpArea.getJTextField();

box.add(bpRadius);
box.add(buttonPanel);
box.add(bpLength);
box.add(bpArea);
leftPanel.add(box);

add(leftPanel);
add(rightPanel);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == resultButton) {
try {
double radius = Double.parseDouble(radiusTextField.getText());
lengthTextField.setText(""+2*Math.PI*radius);
areaTextField.setText(""+Math.PI*radius*radius);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE);
}
}
else if (e.getSource() == clearButton) {
radiusTextField.setText(" ");
}
}

public void focusGained(FocusEvent e) {
inputTextField = (JTextField)e.getSource();
}

public void focusLost(FocusEvent e) {}

public JTextField getInputTextField() {
return inputTextField;
}

}
```

###### 5,三角形计算器的实现(TrianglePanel.java)

```
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TrianglePanel extends AbstractPanel implements ActionListener,FocusListener{

JButton resultButton,clearButton;
JPanel leftPanel,rightPanel,buttonPanel;
JTextField sideATextField,sideBTextField,sideCTextField;
JTextField lengthTextField,areaTextField,inputTextField;
BoxPanel bpSideA,bpSideB,bpSideC,bpLength,bpArea;

public TrianglePanel() {
setLayout(new GridLayout(1,2));
rightPanel = new KeyJPanel(this);
leftPanel =new JPanel();

Box box = Box.createVerticalBox();
bpSideA = new BoxPanel("请输入三角形的边A:",10);
sideATextField = bpSideA.getJTextField();

sideATextField.addFocusListener(this);
bpSideB = new BoxPanel("请输入三角形的边B:",10);
sideBTextField = bpSideB.getJTextField();
sideBTextField.addFocusListener(this);
bpSideC = new BoxPanel("请输入三角形的边C:",10);
sideCTextField = bpSideC.getJTextField();
sideCTextField.addFocusListener(this);
buttonPanel = new JPanel();
resultButton = new JButton("计算结果:");

resultButton.addActionListener(this);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
buttonPanel.add(resultButton);
buttonPanel.add(clearButton);

bpLength = new BoxPanel("三角形的周长:", 20);
lengthTextField = bpLength.getJTextField();
bpArea = new BoxPanel("三角形的面积:", 20);
areaTextField = bpArea.getJTextField();

box.add(bpSideA);
box.add(bpSideB);
box.add(bpSideC);
box.add(buttonPanel);
box.add(bpLength);
box.add(bpArea);
leftPanel.add(box);

add(leftPanel);
add(rightPanel);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == resultButton) {
try {
double sideA = Double.parseDouble(sideATextField.getText());
double sideB = Double.parseDouble(sideBTextField.getText());
double sideC = Double.parseDouble(sideCTextField.getText());

if ((sideA + sideB>sideC)&&(sideA + sideC>sideB)&&(sideB + sideC>sideA)) {
double p = (sideA + sideB +sideC)/2.0;
lengthTextField.setText("" + 2 * p);
double area = Math.sqrt(p * (p - sideA) * (p - sideB) *(p - sideC));
areaTextField.setText("" + area);
}
else {
JOptionPane.showMessageDialog(this, "这不构成一个三角形,请重新输入三边","警告对话框",JOptionPane.WARNING_MESSAGE);
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE);
}
}
else if (e.getSource() == clearButton) {
sideATextField.setText(" ");
sideBTextField.setText(" ");
sideCTextField.setText(" ");
}
}

public void focusGained(FocusEvent e) {
inputTextField = (JTextField)e.getSource();
}

public void focusLost(FocusEvent e) {}

public JTextField getInputTextField() {
return inputTextField;
}

}
```

###### 6,圆柱计算器的实现(CylinderPanel.java)

```
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CylinderPanel extends AbstractPanel implements ActionListener,FocusListener{

JButton resultButton,clearButton;
JPanel leftPanel,rightPanel,buttonPanel;
JTextField radiusTextField,highTextField;
JTextField surfaceAreaTextField,volumeTextField,inputTextField;
BoxPanel bpRadius,bpHeight,bpSurfaceArea,bpVolume;

public CylinderPanel() {
setLayout(new GridLayout(1,2));
rightPanel = new KeyJPanel(this);
leftPanel =new JPanel();

Box box = Box.createVerticalBox();
bpRadius = new BoxPanel("请输入圆柱的底面半径:", 10);
radiusTextField = bpRadius.getJTextField();
radiusTextField.addFocusListener(this);
bpHeight = new BoxPanel("请输入圆柱的高:", 10);
highTextField = bpHeight.getJTextField();
highTextField.addFocusListener(this);
buttonPanel = new JPanel();
resultButton = new JButton("计算结果:");
resultButton.addActionListener(this);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
buttonPanel.add(resultButton);
buttonPanel.add(clearButton);
bpSurfaceArea = new BoxPanel("圆柱的表面积:", 20);
this.surfaceAreaTextField = this.bpSurfaceArea.getJTextField();
bpVolume = new BoxPanel("圆柱的体积:", 20);
this.volumeTextField = this.bpVolume.getJTextField();
box.add(bpRadius);
box.add(bpHeight);
box.add(buttonPanel);
box.add(bpSurfaceArea);
box.add(bpVolume);
leftPanel.add(box);

add(leftPanel);
add(rightPanel);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == resultButton) {
try {
double radius = Double.parseDouble(radiusTextField.getText());
double height = Double.parseDouble(highTextField.getText());
double area = Math.PI * radius *radius;
surfaceAreaTextField.setText(""+(Math.PI * 2 * radius * height + 2 * area));
volumeTextField.setText(""+area * height);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(this, "请输入数字:","警告对话框",JOptionPane.WARNING_MESSAGE);
}
}
else if (e.getSource() == clearButton) {
radiusTextField.setText(" ");
highTextField.setText(" ");
}
}

public void focusLost(FocusEvent e) {
inputTextField = (JTextField)e.getSource();
}

public void focusGained(FocusEvent e) {}

public JTextField getInputTextField() {
return inputTextField;
}

}
```

###### 7,数字软键盘的实现(KeyJPanel.java)

```
import java.awt.Color;
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 KeyJPanel extends JPanel implements ActionListener{

JButton[] keyButton = new JButton[12];
String[] num = {"1","2","3","4","5","6","7","8","9","0",".","BackSpace"};
AbstractPanel selectedPanel;
JTextField inputTextField;

public KeyJPanel(AbstractPanel selectedPanel) {
this.selectedPanel = selectedPanel;

Border lb = BorderFactory.createLineBorder(Color.gray, 2);
setBorder(lb);

setLayout(new GridLayout(4,3));

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(this);
add(keyButton[i]);
}

}

public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();

inputTextField = selectedPanel.getInputTextField();

inputNumber(inputTextField,button);
}

private void inputNumber(JTextField tf, JButton button) {
String oldString = tf.getText();
if (oldString == null) {
tf.setText(" ");
}
String subStr = oldString.substring(0,oldString.length() -1);

String newString = button.getText();

if (newString.equals("BackSpace")) {
tf.setText(subStr);
}

else if (newString.equals(".")) {
tf.setText(oldString+".");
}

else {
tf.setText(oldString+newString);
}
}

}
```

最后修改:2021 年 08 月 12 日
如果觉得我的文章对你有用,请随意赞赏