English 中文(简体)
JPanel setBackground(Color.BLACK)没有
原标题:JPanel setBackground(Color.BLACK) does nothing

我有焦土习俗JPanel,我用Netbeans GUI公司的建筑商将其 ad到我的身边,但背景却获得了变化! 我可以看一看这个圈子,照相。 什么错误?

public class Board extends JPanel{

    private Player player;

    public Board(){
        setOpaque(false);
        setBackground(Color.BLACK);  
    }

    public void paintComponent(Graphics g){  
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(player.getxCenter(), player.getyCenter(), player.getRadius(), player.getRadius());
    }

    public void updatePlayer(Player player){
        this.player=player;
    }
}
最佳回答

如果你的小组不透明,你就会看到你的背景。

问题回答

您必须打上<代码>super.paintComponent(>);,以便Java API能够得出原始背景。 超级公司是指原JPanel代码。

public void paintComponent(Graphics g){
    super.paintComponent(g);

    g.setColor(Color.red);
    g.fillOval(player.getxCenter(), player.getyCenter(), player.getRadius(), player.getRadius());
}

You need to create a new Jpanel object in the Board constructor. for example

public Board(){
    JPanel pane = new JPanel();
    pane.setBackground(Color.ORANGE);// sets the background to orange
} 
setOpaque(false); 

http://www.ohchr.org。

setOpaque(true);

我刚刚尝试了一种光辉的实施工作,它只是努力:

public class Test {

    public static void main(String[] args) {
            JFrame frame = new JFrame("Hello");
            frame.setPreferredSize(new Dimension(200, 200));
            frame.add(new Board());
            frame.pack();
            frame.setVisible(true);
    }
}

public class Board extends JPanel {

    private Player player = new Player();

    public Board(){
        setBackground(Color.BLACK);
    }

    public void paintComponent(Graphics g){  
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(player.getCenter().x, player.getCenter().y,
             player.getRadius(), player.getRadius());
    } 
}

public class Player {

    private Point center = new Point(50, 50);

    public Point getCenter() {
        return center;
    }

    private int radius = 10;

    public int getRadius() {
        return radius;
    }
}

为了给某个肤色确定背景:

(1) 首先确定背景

(2) 称作“Clear(0,0,this.getWidth()、this.getHala()”的方法(成分油漆区的宽度和高度)

I think it is the basic procedure to set the background... I ve had the same problem.

另一种有用的 h指:如果你想将BUT NOT引向一个特定区域(有些像面罩或“洞”),则用“洞”形状(任何形状)的制图法标语,然后称作“清晰度”方法(后方应先放在“洞”颜色上)。

你们可以通过采用“lip(你想要的任何时候)”的“AFTER”方法,使“剪辑”的交叉点更加复杂。

我没有发现任何工会或排泄区改道的方法,只有交叉点,太坏......

希望会有助于





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签