2

JFrame を拡張するメイン クラスがあり、それが jframe に jpanel を追加します。次に、jpanelの背景色を設定しようとしましたが、役に立ちませんでした。Googleで見つけたものによると、問題がどこにあるのかよくわかりsetBackground(Color)ません.JPanelを設定するだけでこれを修正できますが、うまくいかないようです. setOpaque(true)また、これに対する他の修正setVisible(true)は、、、、または使用して JFrame を形成することgetContentPane().setBackground(Color)でしたが、これらのどれも機能していないようです。より多くの情報が必要な場合、または他のアドバイスが必要な場合は、お気軽に教えてください。:) 主なクラスは次のとおりです。

public class main extends JFrame{

    private Content content;

    public main(){

        content = new Content(400, 600);

        this.setTitle("Shooter2.0");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.getContentPane().add(content);
        this.getContentPane().setBackground(Color.BLACK);
        this.pack();
        this.setVisible(true);
        try{
            Thread.sleep(10000);
        }catch(Exception e){}
    }


    public static void main(String[] args){
        main game = new main();
    }

}

Content クラスは次のとおりです。

public class Content extends JPanel{

    private viewItem ship;

    public Content(int w, int h){
        this.setPreferredSize(new Dimension(w, h));
        this.setLayout(new BorderLayout());     
        this.createBattlefield();
        this.setOpaque(true);
        this.setBackground(Color.BLACK);
        this.repaint();
        this.setVisible(true);
    }

    public void createBattlefield(){
        ship = new viewItem("bubble-field.png", 180, 550, 40, 42);      
    }

    public void paint(Graphics g){
        g.setColor(Color.BLACK);
        this.setBackground(Color.BLACK);
        ship.draw(g);       
    }

}
4

2 に答える 2

0

コードブロックを置き換える

public void paint(Graphics g){
    g.setColor(Color.BLACK);
    this.setBackground(Color.BLACK);
    ship.draw(g);       
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);        
    ship.draw(g);       
}

コンストラクターで JPanel の背景色を設定しているため、paintComponent(){} メソッドでは必要ありません...

上記のコードを試してみてください....

于 2014-09-09T18:23:44.327 に答える