1

オブジェクトを作成して、作成したパラメーターGUIオブジェクトとして作成した配列に追加しようとしています。何らかの理由で、TheDatesを変数に解決できません

構築中のオブジェクト:

public static void main(String[] args)
{
    DateDriver myDateFrame = new DateDriver();
}

//Constructor
public DateDriver()
{
    outputFrame = new JFrame();
    outputFrame.setSize(600, 500);
    outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String command;
    Date [] theDates = new Date[100];    //this is the array I am having issues with
    int month, day, year;

    ...
}

これは、theDatesに関する私の問題がどこにあるかです:

public void actionPerformed(ActionEvent e) 
{    //The meat and Potatoes
     if ( e.getSource() == arg3Ctor) 
     {
        JOptionPane.showMessageDialog(null, "3 arg Constructor got it");
        int month = Integer.parseInt(monthField.getText());
        int day = Integer.parseInt(dayField.getText());
        int year = Integer.parseInt(yearField.getText());
        theDates[getIndex()] = new Date(month, day, year);//here is the actual issue
     }
}

考えすぎているのか、何を考えているのかわかりません。配列を静的、パブリックなどにしようとしました。また、として実装しようとしましたmyDayeFrame.theDates

どんなガイダンスでも大歓迎です

4

3 に答える 3

2

スコープに問題がある可能性があります。theDatesはコンストラクターで宣言されており、コンストラクターでのみ表示されます。考えられる解決策:クラスフィールドとして宣言します。コンストラクターで初期化してください。ただし、クラスで宣言されている場合は、クラスで表示されます。

于 2012-07-19T04:05:25.723 に答える
2

コンストラクターでローカル変数として定義theDatesしているため、そのスコープはコンストラクター内で制限されます。代わりに、クラスのフィールドとして宣言します。

private Data[] theDates;

// ...

   public DateDriver()
   {
       theDates = new Date[100];
       // ...
   }
于 2012-07-19T04:05:39.790 に答える
1

1.コンストラクター内の配列オブジェクト参照変数であるtheDatesを定義したので、コンストラクター自体の内部にスコープがあります。

2.クラススコープでtheDatesを宣言して、そのクラス内全体に表示されるようにする必要があります。

3.そして、Arrayの代わりにCollectionを使用する方が良いでしょう、ArrayListに行きます

例えば:

public class DateDriver {

    private ArrayList<Date> theDates;

    public DateDriver() {
        theDates = new ArrayList<Date>();
    }
}
于 2012-07-19T04:26:42.660 に答える