简体   繁体   English

无法在Java中将颜色设置为JPanel

[英]Can't set color to JPanel in Java

I have just started with Graphics in java, and I'm already stuck. 我刚刚开始使用Java中的Graphics,但已经陷入困境。 I have tried to set the color of the JPanel to red but nothing seems to work! 我试图将JPanel的颜色设置为红色,但是似乎没有任何效果! Any help is highly appreciated. 非常感谢您的帮助。

JFrame class: JFrame类:

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;


public class redBoxFrame{

    public static void main(String[]args){
        JFrame f = new JFrame();
        f.setSize(400, 200);
        f.setTitle("A red box");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new redBoxPanel();
        p.setBackground(Color.RED);
        f.add(p);
        f.setVisible(true);
  }

} 

JPanel class: JPanel类:

   import java.awt.Graphics;
   import javax.swing.JPanel;
   import java.awt.Color;

  public class redBoxPanel extends JPanel {

     public void paintComponent(Graphics g){
     g.fillRect(0, 0, 100, 100);
     g.setColor(Color.RED);

     }
  }

As you can see I have both tried to declare the color in the JFrame class and in the JPanel class but none of them seem to work. 如您所见,我都尝试在JFrame类和JPanel类中声明颜色,但是它们似乎都不起作用。 Thank you! 谢谢!

I think you you are missing super.paintComponent(g); 我认为您缺少了super.paintComponent(g); in your painComponent method. 在您的painComponent方法中。

I Believe that the solution is working however you are, like you said in your question, setting the background in the JFrame class and the JPanel class. 我相信该解决方案正在工作,但是就像您在问题中所说的那样,您可以在JFrame类和JPanel类中设置背景。 if you remove the setBackground from the JFrame class you should see only the rectangle that you are drawing. 如果从JFrame类中删除setBackground,则应该只看到正在绘制的矩形。 Please try the below solution and let us know if it works. 请尝试以下解决方案,让我们知道它是否有效。

JFrame class: JFrame类:

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;


public class redBoxFrame{

    public static void main(String[]args){
        JFrame f = new JFrame();
        f.setSize(400, 200);
        f.setTitle("A red box");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new redBoxPanel();
        f.add(p);
        f.setVisible(true);
  }

} 

JPanel class: JPanel类:

  import java.awt.Graphics;
  import javax.swing.JPanel;
  import java.awt.Color;

  public class redBoxPanel extends JPanel {

     public void paintComponent(Graphics g){
     super.paintComponent(g);
     g.fillRect(0, 0, 100, 100);
     g.setColor(Color.RED);

     }
  }

Everyone here seems to miss the fact that the colour should be set before drawing. 这里的每个人似乎都错过了绘制之前就应该设置颜色的事实。

I'll set the main background to BLUE for demo purposes. 为了演示目的,我将主要背景设置为BLUE。

public static void main(String[] args) {
    //...
    JPanel p = new redBoxPanel();
    // BLUE bg. This covers the whole panel.
    p.setBackground(Color.BLUE);
    //...
}

And now for the red box! 现在是红色框!

public static class redBoxPanel extends JPanel {
    @Override public void paintComponent(Graphics g) {
        // You need to call the superclass' paintComponent() so that the 
        // setBackground() you called in main() is painted.
        super.paintComponent(g);

        // You need to set the colour BEFORE drawing your rect
        g.setColor(Color.RED);

        // Now that we have a colour, perform the drawing
        g.fillRect(0, 0, 100, 100);

        // More, for fun
        g.setColor(Color.GREEN);
        g.drawLine(0, 0, 100, 100);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM