java高手写代码 怎么写java代码( 二 )


求一个50行左右的JAVA代码 , 最好每行带注释,谢谢啦/*这个相当详细java高手写代码了.
程序也不算太难.而且给老师看java高手写代码的时候效果比较好.因为有图形化界面,又实现一个比较实用java高手写代码的功能.老师会比较高兴java高手写代码的.
建立一个文件名为Change.java就可以编译了*/
/*
* 这个程序实现输入身高算出标准体重,输入体重,算出身高的功能
*/
import java.awt.*; //导入相关类包,这才样使用相应awt图形界面的类
import java.awt.event.*;//同上
public class Change extends Frame { //定义一个类Change, 父类是Frame(图形界面的)
Button b = new Button("互查"); //创建一个按钮的对象b,显示为"互查"
Label l1 = new Label("身高(cm)");//创建一个lable.显示身高
Label l2 = new Label("体重(kg)");//创建一个lable 显示体重
double heigth, weigth; //定义变量
double x, y; //定义变量
TextField tf1 = new TextField(null, 10);//添加Text框
TextField tf2 = new TextField(null, 10);//添加Text框
public Change() {//类的构造函数,完成初始化
super("互查表");//创建窗口,标题为互查表
setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局
add(l1);//把lable 身高放到window里
add(tf1);//把Text 框 放到窗口上
add(l2); //把lable 体重放到window里
add(tf2);//Test放到窗口里
add(b);//把button放到窗口上
pack();//自动放到窗口里排列上边的组件
setVisible(true);//可以让用户看到窗口
addWindowListener(new WindowAdapter() {//如果按 X, 关闭窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());//添加button监听函数
}
class ButtonListener implements ActionListener {//实现click button时功能操作
public void actionPerformed(ActionEvent e) {//当click调用
if (tf1.getText()!=null) {//检查tf1 test 是否为空
try {//取异常
x = Double.parseDouble(tf1.getText());//字符转为double型
weigth = (x - 100) * 0.9;//算重量
tf2.setText("" + weigth);//显示重量
} catch (NumberFormatException ex) {
tf1.setText("");//如果输入不是数字,设为空
}
}
if (tf1.getText().equals("")==true){//tf1是否为空
y = Double.parseDouble(tf2.getText());//把tf2里的文本转为double 型 的
heigth = y / 0.9 + 100;//算身高根据重量
tf1.setText("" + heigth);}//显示身高
}
}
public static void main(String[] args) {//主函数,程序入口
new Change(); //建立类Change的对象,并调用java高手写代码他的构造函数Change().显示窗口
}
}
java编程题,自己觉得又点难(求高手写代码)//Color类
public class Color {
private String colorName;
final public void setColor(String color){
this.colorName = color;
}
public String getColor(){
return this.colorName;
}
}
//White类
public class White extends Color{
public White(){
this.setColor("white");
}
public String getColor() {
return super.getColor();
}
}
//Red类
public class Red extends White{
public Red(){
this.setColor("Red");
}
}
//Prism类
public class Prism {
static public void activePrism(Color c){
if(c.getColor().equals("white")){
Red r = new Red();
Blue b = new Blue();
System.out.println(r.getColor());
System.out.println(b.getColor());
}
else{
return;
}
}
}
//测试类
public class ColorTest {
public static void main(String[] args) {
White w = new White();
Prism.activePrism(w);

推荐阅读