2017-02-23 31 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

我不明白你的问题。但是,这里是将您的背景更改为RED的代码;

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好像是说你类是JFrameJFrame是刚性容器,而是创建基于JPanel是你的GUI。有关更多信息,请参阅Java Swing extends JFrame vs calling it inside of class

  2. 您通过删除paint(...)方法中的super.paint(g)调用来破坏涂料链。当更改您的GUI以延长JPanel而不是JFrame时,应该使用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

相关问题