JAVA学习第二天

    技术2022-05-20  39

    忙着发以前写的一个小东西,有点耽误了,要再加把劲才行啊!

     

    今天主要学习了一下JAVA的数据类型、运算符和基本语句,好在不太难理解。只是要记的东西太多,特别是一些关键字的首字母要大写,有点不习惯,只能慢慢去适应了。为了不让学习枯燥而丧失兴趣,抄书写了一段applet小应用程序,源码如下:

     

    import java.awt.*;import java.applet.*;

    public class boy extends Applet{   public void paint(Graphics g)   {      g.setColor(Color.red);      g.drawString("我一边喝着咖啡,一边学习JAVA.",5,10);      g.setColor(Color.blue);      g.drawString("尽管我对面坐着一个漂亮的女孩,但是我学得很认真!",5,30);   }}

     

     

    1、java applet 不再需要main方法,但必须有一个类扩展了Applet类,该类叫做这个Java applet的主类,它必须是public的,其源文件保存时必须用主类的名字。

     

    2、编译时:javac boy.java

     

    3、编写一个html来运行该程序:<applet code=boy.class height=100 width=300></applet>,该thml和class文件保存在同目录下,命名为boy.html(html文件可以任意命名)。如果不在同一目录下,需要用codebase来指出class文件的路径。用浏览器打开html文件即可,也可以用appletviewer boy.html 命令来运行。

     

     


     

     

    时间还早,又抄了一个难一点的,从严要求自己,起点高一点嘛,呵呵

     

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

    public class myp1   {public static void main(String args[])      {Cwindow p=new Cwindow();      }   }

    class Cwindow extends Frame implements ActionListener   {   Panel myp1=new Panel(),myp2=new Panel(),myp3=new Panel();   TextField subject=new TextField(16);   TextField stu_number=new TextField(3);   TextField number[]=new TextField[50],name[]=new TextField[50],result[]=new TextField[50];   Button button=new Button("确认");   Button b1=new Button("确定"),b2=new Button("取消");   Statics other_window;   int people_number=0;      Cwindow()   {super("学生成绩统计表");   other_window=new Statics();   setBounds(100,120,550,200);   setVisible(true);   myp2.setVisible(false);   myp3.setVisible(false);   Label 科目=new Label("考试科目:"),人数=new Label("考试人数(不超过50个):"),学号=new Label("学号:"),姓名=new Label("姓名:"),成绩=new Label("成绩:");   myp1.add(科目);myp1.add(subject);myp1.add(人数);myp1.add(stu_number);myp1.add(button);   button.addActionListener(this);   myp2.add(new Label("每行都必须输入成绩"));   myp2.add(b1);myp2.add(b2);   b1.addActionListener(this);   b2.addActionListener(this);   myp3.setLayout(new GridLayout(81,3));   myp3.add(学号);myp3.add(姓名);myp3.add(成绩);

       for(int i=0;i<=49;i++)   {number[i]=new TextField(" "+(i+1));name[i]=new TextField("姓名");result[i]=new TextField("0");   myp3.add(number[i]);myp3.add(name[i]);myp3.add(result[i]);   }

       ScrollPane p=new ScrollPane();   p.add(myp3);   add("North",myp1);add("Center",myp3);add("South",myp2);   addWindowListener(new WindowAdapter()   {   public void windowClosing(WindowEvent e)   {setVisible(false);System.exit(0);}   });

       pack();

       }   public void actionPerformed(ActionEvent e)

       {if (e.getSource()==button)      {people_number=Integer.parseInt(stu_number.getText());         if(people_number<=50)         {myp3.setVisible(true);myp2.setVisible(true);            for(int i=0;i<people_number;i++)               {number[i].setBackground(Color.pink);                name[i].setBackground(Color.cyan);                result[i].setBackground(Color.green);                number[i].setEnabled(true);                name[i].setEnabled(true);                result[i].setEnabled(true);               }

                for(int i=people_number;i<=50;i++)               {number[i].setEnabled(false);                name[i].setEnabled(false);                result[i].setEnabled(false);                result[i].setBackground(Color.white);                name[i].setBackground(Color.white);                number[i].setBackground(Color.white);               }         }         else {stu_number.setText("超过额定人数");}      }

       else if(e.getSource()==b1)     {other_window.setVisible(true);      int geshu_优=0,geshu_良=0,geshu_及格=0,geshu_不及格=0;      double a[]=new double[50],sum=0;

          for (int i=0;i<people_number;i++)        {a[i]=Double.valueOf(result[i].getText().trim()).doubleValue();

             if (a[i]>=90) {geshu_优++;}         if (a[i]>90&&a[i]>=80) {geshu_良++;}         if (a[i]>=60&&a[i]<80) {geshu_及格++;}         if (a[i]<60) {geshu_不及格++;}         sum=sum=a[i];        }

          sum=sum/people_number;      other_window.text[0].setText("  "+people_number);      other_window.text[1].setText("  "+geshu_优);      other_window.text[2].setText("  "+geshu_良);      other_window.text[3].setText("  "+geshu_及格);      other_window.text[4].setText("  "+geshu_不及格);      other_window.text[5].setText("  "+sum);     }

       else if(e.getSource()==b2)

         {for(int i=0;i<=49;i++)        {number[i].setText("  "+(i+1));name[i].setText(null);         result[i].setText("0");        }     }   }}class Statics extends Frame

    {TextField text[]=new TextField[6];Label 考试人数=new Label("考试人数:"),优秀=new Label("优秀人数:"),良好=new Label("良好人数:"),及格=new Label("及格人数:"),不及格=new Label("不及格人数:"),平均=new Label("平均分数:");

    Statics(){super("成绩统计结果表");setLayout(new GridLayout(2,6));add(考试人数);add(优秀);add(良好);add(及格);add(不及格);add(平均);

    setVisible(false);

    setBounds(140,140,200,100);   for(int i=0;i<=5;i++)   {text[i]=new TextField();add(text[i]);   }addWindowListener(new WindowAdapter()

    {public void windowClosing(WindowEvent e)   {setVisible(false);}});

    }}

     

    但是,运行后并没有出现预想的效果,怪哉!

     


    最新回复(0)