本人有一个JAVA程序难题,麻烦高手解决!快!快!!!!!!!!!!

2024-11-17 21:22:04
推荐回答(2个)
回答1:

还得继续纠错,关于WindowListener类:
用于接收窗口事件的侦听器接口。旨在处理窗口事件的类要么实现此接口(及其包含的所有方法),要么扩展抽象类 WindowAdapter(仅重写所需的方法)。然后使用窗口的 addWindowListener 方法将从该类所创建的侦听器对象向该 Window 注册。当通过打开、关闭、激活猛耐或停用、图标化或取消图标化而改变了窗口状态时,将调用该侦听器对象中的相关方法,并将 WindowEvent 传递给该方法。
楼主可以写一个自定义的监听器。我是这样写的:
package coreservlets;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
class circle extends Panel implements ActionListener
{
double r, area;
TextField radius = null, result = null;
Button b = null;

void circle()
{
radius = new TextField(10);
result = new TextField(10);
b = new Button("确定");
add(new Label("输入半径"));
add(radius);
add(new Label("面积是:"));
add(result);
add(b);
b.addActionListener(this);
result.setEnabled(false);
}

public void actionPerformed(ActionEvent e)
{
try
{
r = Double.parseDouble(radius.getText());
area = Math.PI * r * r;
result.setText("" + area);
}
catch (Exception ee)
{
radius.setText("请输入数字字符");
}
}
}

class trangle extends Panel implements ActionListener
{
double a = 0, b = 0, c = 0, area;
TextField border_a = new TextField(6);
TextField border_b = new TextField(6);
TextField border_c = new TextField(6);
TextField result = new TextField(24);
Button button = new Button("确定");

trangle()
{
add(new Label("输入三边的长度:"));
add(border_a);
add(border_b);
add(border_c);
add(new Label("面积是:"));
add(result);
add(button);
button.addActionListener(this);
result.setEnabled(false);
}

public void actionPerformed(ActionEvent e)
{
try
{
a = Double.parseDouble(border_a.getText());
b = Double.parseDouble(border_b.getText());
c = Double.parseDouble(border_c.getText());
if (a + b > c && a + c >陆迹 b && c + b > a)
{
double p = (a + b + c) / 2;
area = Math.sqrt(p * (p - a) * (p - b)
* (p - c));
result.setText("" + area);
}
else
{
result.setText("您输入的数字不能形成三角形"枝悉春);
}

}
catch (Exception ee)
{
result.setText("请输入数字字符");
}
}
}

class Win extends JFrame implements ActionListener
{
MenuBar bar = null;
Menu menu = null;
MenuItem item1, item2;
circle circle;
trangle trangle;

Win()
{
bar = new MenuBar();
menu = new Menu("选择");
setSize(300, 200);
item1 = new MenuItem("圆面积计算");
item2 = new MenuItem("三角形面积计算");
menu.add(item1);
menu.add(item2);
bar.add(menu);
setMenuBar(bar);
circle = new circle();
trangle = new trangle();
item1.addActionListener(this);
item2.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == item1)
{
removeAll();
add(circle, "Center");
validate();
}
else if (e.getSource() == item2)
{
removeAll();
add(trangle, "Center");
validate();
}
}
}

public class BaiDu
{
public static void main(String args[])
{
Win win = new Win();
win.setTitle("exam_1");
win.setBounds(100, 100, 700, 300);
win.setVisible(true);
win.addWindowListener(new MyListener());

}
}

class MyListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

回答2:

double.parseDouble

D应该大写
Double.parseDouble