📄 lab16_2.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
class TriangleDialog extends Dialog implements ActionListener
{
TextField edge_a, edge_b, edge_c, answer;
double a = 0, b = 0, c = 0, area;
Button but;
Panel p, p1;
TriangleDialog(Frame f, String s, boolean b)
{
super(f, s, b);
p = new Panel();
p1 = new Panel();
edge_a = new TextField(6);
edge_b = new TextField(6);
edge_c = new TextField(6);
answer = new TextField(6);
setLayout(new FlowLayout());
but = new Button("确定");
add(new Label("输入三角形三条边的长度"));
p.add(new Label("边_a = "));
p.add(edge_a);
p.add(new Label("边_b = "));
p.add(edge_b);
p.add(new Label("边_c = "));
p.add(edge_c);
add(p);
p1.add(new Label("面积 = "));
p1.add(answer);
p1.add(but);
add(p1);
edge_a.addActionListener(this);
edge_b.addActionListener(this);
edge_c.addActionListener(this);
but.addActionListener(this);
setBounds(100, 100, 450, 200);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == but || e.getSource() == edge_a || e.getSource() == edge_b || e.getSource() == edge_c)
{
try
{
a = Double.parseDouble(edge_a.getText());
b = Double.parseDouble(edge_b.getText());
c = Double.parseDouble(edge_c.getText());
if(a + b > c && Math.abs(a - b) < c)
{
double p = (a + b + c) / 2;
area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
NumberFormat f = NumberFormat.getInstance();
f.setMaximumFractionDigits(5);
String s = f.format(area);
answer.setText(s);
}
else answer.setText("不能构成三角形");
}
catch(NumberFormatException event)
{
answer.setText("含有错误字符");
}
}
}
}
class CircleDialog extends Dialog implements ActionListener
{
TextField r, answer;
Button but;
double l, area;
Panel p1, p2;
CircleDialog(Frame f, String s, boolean b)
{
super(f, s, b);
p1 = new Panel();
p2 = new Panel();
r = new TextField(6);
answer = new TextField(6);
but = new Button("确定");
p1.add(new Label("输入半径"));
p1.add(new Label("r = "));
setLayout(new FlowLayout());
p1.add(r);
p2.add(new Label("面积 = "));
p2.add(answer);
p2.add(but);
add(p1);
add(p2);
r.addActionListener(this);
but.addActionListener(this);
setBounds(120, 120, 300, 200);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == but || e.getSource() == r)
{
try
{
l = Double.parseDouble(r.getText());
area = Math.PI * l * l;
NumberFormat f = NumberFormat.getInstance();
f.setMaximumFractionDigits(5);
String s = f.format(area);
answer.setText(s);
}
catch(NumberFormatException event)
{
answer.setText("含有错误字符");
}
}
}
}
class MyFrame extends Frame implements ActionListener
{
MenuBar bar;
Menu menu;
MenuItem triangle, circle;
TriangleDialog tdialog;
CircleDialog cdialog;
MyFrame(String s)
{
super(s);
bar = new MenuBar();
menu = new Menu("选择");
triangle = new MenuItem("三角形");
circle = new MenuItem("圆形");
tdialog = new TriangleDialog(this, "三角形面积计算", false);
cdialog = new CircleDialog(this, "圆形面积计算", false);
bar.add(menu); menu.add(triangle); menu.add(circle);
setLayout(new GridLayout(1,1));
add(new Label("选择图形", Label.CENTER));
setMenuBar(bar);
setVisible(true);
setBounds(80, 80, 300, 100);
validate();
triangle.addActionListener(this);
circle.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == triangle)
{
tdialog.setVisible(true);
}
if(e.getSource() == circle)
{
cdialog.setVisible(true);
}
}
}
public class Lab16_2
{
public static void main(String[] args)
{
MyFrame f = new MyFrame("面积计算");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -