2016-09-13 3 views
-1
import javax.swing.*; 
import java.awt.*; 

public class CGLinesGrid extends JFrame { 
    public CGLinesGrid() { 
     super ("Exercise 4"); 
     setSize (500, 500); 
     setVisible(true); 
    } 

    public void paint (Graphics g) { 

     for (int i = 1; i<=9 ; i++) { 
      g.drawLine(70, 30+i*40, 390, 30+i*40);    
      g.drawLine(30+i*40, 70, 30+i*40, 390); 
     } 
     for (int i = 1; i<=9 ; i++) { 
      g.drawOval(70, 20+i*40, 40, 10+i*30); 
     } 
    } 

    public static void main (String[] args) { 
     CGLinesGrid draw = new CGLinesGrid(); 
     draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 
+2

問題は何ですか? – ChiefTwoPencils

+0

私は8x8グリッドを作成しました。私はそれらの中に楕円形を入れたい。あなたは私を助けることができますか? : –

+1

'paint'をオーバーライドしないでください。あなたは' paintComponent'メソッドをオーバーライドすることになっています。 – RealSkeptic

答えて

0

解決策に至るまでのカップル。

@RealSkepticが言ったように、決してあなたのコンポーネントの中からpaint()メソッドを呼び出してください。必ずpaintComponent(Graphics g)メソッドを使用してください。

第二に、常に

super.paintComponent(g); 

サードを使用して、その親クラスのpaintComponentメソッドを呼び出しpaintComponent()メソッドを呼び出すときに、paintComponent()メソッドはJPanelを使用し、JFrame上で直接呼び出すことはできません。

今すぐ解決してください。

8x8グリッドの四角形が必要です。四角形の内側にはそれぞれ円があります。

これを行う最も簡単な方法は、GridLayoutを持つJPanelを使用し、64 JLabelを追加することです。それぞれの `JLabelはその正方形の中に正方形と円を印刷します。

我々は、正方形と円をプリントアウトのJLabelを作成する必要があります下最低料金:

:8×8グリッド内のすべてのこれらの JLabel Sが含まれている私たちは JPanelが必要

class SquareWithCircleLabel extends JLabel { 
    @Override 
    public void paintComponent(Graphics g) { 
     //notice the call to its parent method 
     super.paintComponent(g); 

     //draw the square and circle 
     g.drawRect(0, 0, this.getWidth(), this.getHeight()); 
     g.drawOval(1, 1, this.getWidth() - 1, this.getHeight() - 1); 
    } 
} 

class PaintPanel extends JPanel { 
    PaintPanel() { 
     //give it a 8 x 8 GridLayout 
     setLayout(new GridLayout(8, 8)); 

     //add 81 labels to the JPanel and they will automatically be formatted into a 9 x 9 grid because of our GridLayout 
     for (int i = 0; i < 64; i++) { 
      add(new SquareWithCircleLabel()); 
     } 
    } 
} 

は、今、私たちは、単にコンストラクタで私たちのJFrameこのJPanelを追加する必要が

add(new PaintPanel()); 

完全なコードは次のようになります。

import javax.swing.*; 
import java.awt.*; 

public class CGLinesGrid extends JFrame { 
    public CGLinesGrid() { 
     super ("Exercise 4"); 
     setSize (500, 500); 
     add(new PaintPanel()); 
     setVisible(true); 
    } 

    public static void main (String[] args) { 
     CGLinesGrid draw = new CGLinesGrid(); 
     draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    class PaintPanel extends JPanel { 
     PaintPanel() { 
      setLayout(new GridLayout(9, 9)); 
      for (int i = 0; i < 81; i++) { 
       add(new SquareWithCircleLabel()); 
      } 
     } 
    } 

    class SquareWithCircleLabel extends JLabel { 
     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      g.drawRect(0, 0, this.getWidth(), this.getHeight()); 
      g.drawOval(1, 1, this.getWidth() - 1, this.getHeight() - 1); 
     } 
    } 

} 
+0

Sir Yitzih、 –

+0

@DarrelDelaCruzどのようなエラーが出ていますか? – yitzih

+0

2つのクラスの階層が矛盾していると言われています:( –

関連する問題