×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / 初级java问题请教...
    一applet, 从a:打开,可以运行自如。可是,同样文件,如从c:打开,
    虽然也可以,但那些button只反应一次(awt button,是作类似计数用),
    也即只能加一次,就再没有反应。

    也想过是classpath设置问题。可是应该没有问题,因为两个文件都
    可以运行,只是如果从c:打开文件,actionPerformed method似乎只
    能执行一次。

    一模一样的文件,真是百思不得其解...

    请教各位高手为什么?? 谢先了。
    • Why not post your source code?
      • source code here......
        本文发表在 rolia.net 枫下论坛this is Date.java:

        public class Date
        {
        private int year, month, day;
        int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        public Date(int y, int m, int d)
        {
        setYear(y);
        setMonth(m);
        setDay(d);
        }

        public void setYear(int pYear)
        {
        year = pYear;
        }

        public void setMonth(int pMonth)
        {
        month = (pMonth > 0 && pMonth < 13) ? pMonth : 1;
        }

        public void setDay(int pDay)
        {
        if (checkDay(pDay))
        day = pDay;
        else
        day = 1;
        }

        private boolean checkDay(int pDay)
        {
        if (pDay > 0 && pDay <= daysPerMonth[month])
        return true;
        else
        if (month == 2 && pDay == 29 && year % 4 == 0)
        return true;
        else
        return false;
        }

        public int getYear()
        {
        return year;
        }

        public int getMonth()
        {
        return month;
        }

        public int getDay()
        {
        return day;
        }

        public void tickYear()
        {
        year++;
        }

        public void tickMonth()
        {
        month++;
        if (month > 12)
        {
        month = 1;
        year++;
        }
        if (day > daysPerMonth[month])
        day = daysPerMonth[month];
        }

        public void tickDay()
        {
        day++;
        setDay(day);
        if (day == 1)
        month++;
        }

        public String toString()
        {
        String monthNames[] = {"", "January",
        "February",
        "March",
        "April",
        "May", "June",
        "July", "August", "September",
        "October", "November", "December"};
        return monthNames[month] + " " + day + ", " + year;
        }
        }


        this is DateTest.java:

        import java.applet.Applet;
        import java.awt.*;
        import java.awt.event.*;

        public class DateTest extends Applet implements ActionListener
        {
        private Date date;

        private int year,
        month,
        day;

        private Label lSetYear,
        lSetMonth,
        lSetDay;

        private TextField tYear,
        tMonth,
        tDay,
        tSetDate;

        private Button setButton;
        private Button[] addButton = new Button[3];

        public void init()
        {
        lSetYear = new Label("Set Year");
        lSetMonth = new Label("Set Month");
        lSetDay = new Label("Set Day");

        tYear = new TextField(5);
        tMonth = new TextField(5);
        tDay = new TextField(5);
        tSetDate = new TextField(25);

        setButton = new Button("Set Date");
        addButton[0] = new Button("Add a Year");
        addButton[1] = new Button("Add a Month");
        addButton[2] = new Button("Add a Day");

        add(lSetMonth);
        add(tMonth);
        add(lSetDay);
        add(tDay);
        add(lSetYear);
        add(tYear);
        add(tSetDate);
        add(setButton);
        add(addButton[1]);
        add(addButton[2]);
        add(addButton[0]);

        for (int i = 0; i < addButton.length; i++)
        addButton[i].addActionListener(this);

        setButton.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e)
        {
        year = Integer.parseInt(tYear.getText());
        month = Integer.parseInt(tMonth.getText());
        day = Integer.parseInt(tDay.getText());

        if (e.getSource() == setButton)
        date = new Date(year, month, day);

        if (e.getSource() == addButton[0])
        date.tickYear();

        if (e.getSource() == addButton[1])
        date.tickMonth();

        if (e.getSource() == addButton[2])
        date.tickDay();

        tSetDate.setText(date.toString());
        }
        }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 可能是临时文件在做怪吧。浏览器发现曾经有过这个文件,就可能不会加载新版本的文件,而使用缓存的旧版本
      • good point, thanks......however,
        i use textpad to compile and run the applet, so it's the appletviewer that actually runs the applet, not the browser. actually i ftped my applet class and corresponding html file to my homepage. it works as fine as i run it from a: driver.

        so back to my question: why those buttons only react ONCE if i open
        the file from c: driver? the applet does show up. when the buttons are clicked, they do add a number...but that's it.

        exactly the same file, so wired...
        • good news...u know what, just now i tried to add "." to the classpath and it works...but still dont know why...
          b4 i set classpath to: c:\dir;.;c:\jdk..\rt.jar and ran the applet from a: and c:.
          a: worked and c: funtioned weird (buttons only functioned once). later i
          set classpath only to c:\..rt.jar and got the same result.

          now i set it to .;c:\...rt.jar and both (a & c) work.

          so my guessing is that it's sth related to classpath. but why? i've got
          more than one Date class under c:\dir. but i dont think it really matters
          coz everytime b4 i run the applet i always compile the related Date file.
          i think that if the compiler finds multiple files with the same name, it will look at each file's compiling time to decide which one to use. but now
          it looks like i have to explicitly specify the classpath. even so, it still cant
          explain the weird behaviour of those buttons...
          • 原因很简单啊,因为你必须那么做。 因为你的程序编译后有多个文件组成, 如果不把当前路径加进去, 它不会自己到这个目录下去找的。 这是Java的一个特点, 有点像unix