1

これがクラスとスーパークラスです。質問は次のとおりです。

テストドロー:

package project3;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TestDraw extends MyShape
{
    public static void main(String[] args) 
    {
        DrawPanel panel = new DrawPanel();
        JFrame application = new JFrame();



        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
        application.setSize(300,300);
        application.setVisible(true);   
        JLabel southLabel = new JLabel(toString());
        application.add(southLabel, BorderLayout.SOUTH);
    }
}

マイシェイプ:

package project3;

import java.awt.Color;

public class MyShape 
{
    private int x1, y1, x2, y2;
    private Color myColor;
    public MyShape()
    {
        setX1(1);
        setY1(1);
        setX2(1);
        setY2(1);
        setMyColor(Color.BLACK);
    }
    public MyShape(int x1, int y1, int x2, int y2, Color myColor)
    {
        setX1(x1);
        setY1(y1);
        setX2(x2);
        setY2(y2);
        setMyColor(myColor);
    }
    public void setX1(int x1)
    {
        if(x1 >= 0 && x1 <= 300)
        {
            this.x1 = x1;
        }
        else
        {
            this.x1 = 0;
        }
    }
    public int getX1()
    {
        return x1;
    }
    public void setY1(int y1)
    {
        if(y1 >= 0 && y1 <= 300)
        {
            this.y1 = y1;
        }
        else
        {
            this.y1 = 0;
        }
    }
    public int getY1()
    {
        return y1;
    }
    public void setX2(int x2)
    {
        if(x2 >= 0 && x2 <= 300)
        {
            this.x2 = x2;
        }
        else
        {
            this.x2 = 0;
        }
    }
    public int getX2()
    {
        return x2;
    }
    public void setY2(int y2)
    {
        if(y2 >= 0 && y2 <= 300)
        {
            this.y2 = y2;
        }
        else
        {
            this.y2 = 0;
        }
    }
    public int getY2()
    {
        return y2;
    }
    public void setMyColor(Color myColor)
    {
        this.myColor = myColor;
    }
    public Color getMyColor()
    {
        return myColor;
    }
    public String toString()
    {
        return String.format("X1: %d, X2: %d, Y1: %d, Y2: %d, Color: %s", getX1(), getX2(),
                getY1(), getY2(), getMyColor());
    }
}

クラス TestDraw で、MyShape からの toString をウィンドウのテキスト ボックスに入れようとしましたが、「JLabel southLabel = new JLabel(toString());」を実行すると、それは私の toString() が静的である必要があることを伝えました。toString を静的にする場合を除いて、それはすべて問題なくダンディです。その文字列の取得を静的にしたいのですが、これは悪いことです...何かアイデアはありますか?

toString() をスーパークラスに入れてみましたが、同じ問題が発生し、先生に尋ねてみましたが、彼は「本を見てください」と言います...章を2時間読んで、まだ見つけていません3回目を読んだ後の例。

前もって感謝します!

PS: 回答はいいですが、説明が優先されます!

4

2 に答える 2

3

クラスのインスタンスを作成します。
TestDraw testDraw = new TestDraw();
その上で toString() メソッドを呼び出します。メイン メソッドを使用している間は、静的コンテキストにいます。つまり、TestDraw 型のオブジェクトがありません。これは、そのフィールドやメソッドがないことも意味します。

于 2009-11-12T19:42:34.633 に答える
1

これは、静的メソッド (メイン) 内で非静的メソッドを呼び出すためです。それはうまくいきません。代わりに、次のように TestDraw オブジェクトをインスタンス化する必要があります。

TestDraw testDraw = new TestDraw();
JLabel southLabel = new JLabel(testDraw.toString());
于 2009-11-12T19:43:19.577 に答える