2017-02-23 8 views
-1

私はこのクラスのラボで作業していました。バックグラウンドカラーを変更しようとしたときにデフォルトのままになってしまった人は、プログラミングのどこが間違っているのか説明してください。なぜ私の背景色が変わらないのですか?

import javax.swing.*; 
import java.awt.*; 
public class DemoPoly extends JFrame { 

// constructor 
public DemoPoly() { 
    // defines Frame characteristics 

    int size = 300; 
    setSize(size,size); 
    setTitle("a random window"); 

    getContentPane().setBackground(Color.red); 

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    setVisible(true); 
} 

public static void main (String[] args){ 
    // instantiates a JFrame 
    // exits on close (opional) 
    JFrame object = new DemoPoly(); 
} 

public void paint (Graphics g){ 
    // This provides the Graphics object g, where 
    // you are going to use you graphics primitive 
    // to paint on the content pane of the frame. 
    int[] arr = {0,100,100,0}; 
    int[] yarr = {0,0,100,100}; 
    Square object = new Square(arr,yarr,Color.red); 
    AbstractPolygon randSquare = new Square(arr, yarr, Color.red); 
} 
+1

のJavaの説明グラフィックスまたはスクリーンオブジェクトがこのサイトでは広すぎます。 – ProgrammersBlock

答えて

0

私はあなたの質問を理解していません。しかし、赤を背景に変えるためのコードはここにあります。

public class DemoPoly extends JFrame { 

    public DemoPoly() { 
     // defines Frame characteristics 

     int size = 300; 
     setSize(size, size); 
     setTitle("a random window"); 

     //change background here 
     getContentPane().setBackground(Color.red); 

     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     // instantiates a JFrame 
     // exits on close (opional) 
     JFrame object = new DemoPoly(); 
    } 
} 

コードは正常です。おそらくあなたのペイント方法で@overrideを使用します。

1

私はあなたのコード内の問題のカップルを参照してください。

  1. JFrameの拡張はJFrameではなくJPanel Sに基づいてGUIを作成し、剛性容器で、JFrameあるクラス言うようなものです。詳細については、Java Swing extends JFrame vs calling it inside of classを参照してください。

  2. paint(...)メソッドのsuper.paint(g)コールを削除してペイントチェーンを破棄しています。 GUIを変更しての代わりにJPanelを拡張する場合は、代わりにpaintComponent(...)メソッドを使用する必要があります。 Lesson: Performing Custom Painting in Swingにアクセスしてください。

  3. paint(...)メソッドに@Overrideという表記を追加するのを忘れました。

  4. スレッドの問題の原因となるEvent Dispatch Thread (EDT)にプログラムを配置していません。

    あなたはこのようなあなたのmain()方法を変更することでこの問題を解決することができます

    public static void main(String[] args) { 
        SwingUtilities.invokeLater(new Runnable() { 
         @Override 
         public void run() { 
          //Your constructor here 
         } 
        }); 
    } 
    
  5. 代わりJFrameサイズを設定するので、getPreferredSize()メソッドをオーバーライドしてpack()を呼び出します。 Should I setPreferred|Maximum|MiniumSize in Java Swing?を参照してください。一般的な合意は「はい」と言います。


あなたの問題はpaint(...)方法に

super.paint(g); 

を追加することで解決します:あなたのコードは、考慮に入れ、上記のすべての勧告に

@Override 
public void paint(Graphics g) { 
    super.paint(g); //Never remove this 
    //Your code goes here 
} 

必要があります今のように見える:

この出力を生成し(そして、それはあなたのコンポーネントをより細かく制御できますので、あなたがあなたの現在のコードが、より良いと買ってあげる同じ出力である)
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.WindowConstants; 

public class DemoPoly { 

    private JFrame frame; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new DemoPoly().createAndShowGui(); 
      } 
     }); 
    } 

    public void createAndShowGui() { 
     frame = new JFrame(getClass().getSimpleName()); 

     CustomPanel cp = new CustomPanel(); 
     cp.setBackground(Color.RED); 
     frame.add(cp); 
     frame.pack(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    } 
} 

class CustomPanel extends JPanel { 
    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
    } 

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

enter image description here

関連する問題