1

paint(Graphics g) コードを使用しようとすると、エラーが発生します。3D 長方形のウィンドウが表示されるようにコードを解決してください。ありがとう!

private static void paint(Graphics g){
    g.draw3DRect(10, 10, 50, 50, true);

そして下に向かって:

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
            paint();

        }
    });
}


}
4

1 に答える 1

5

Java では、オーバーライド時にメソッドの可視性を下げることはできません。同様に、インスタンス メソッドは作成できませんstatic。である必要があります

@Override
public void paint(Graphics g){
    super.paint(g);
    g.draw3DRect(10, 10, 50, 50, true);
}

Swing では、 などのトップレベル ウィンドウでカスタム ペイントを実行しないでくださいJFrameJComponent代わりに、オーバーライドのサブクラスを作成し、paintComponent必ず を呼び出してsuper.paintComponent(g)ください。


class MyComponent extends JComponent {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.draw3DRect(10, 10, 50, 50, true);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }
}

新しいコンポーネントのインスタンスを に追加することを忘れないでくださいJFrame:

frame.add(new MyComponent());
于 2013-05-28T15:17:22.327 に答える