JFrame背景颜色闪烁但随后恢复

时间:2013-11-08 17:29:18

标签: java swing jframe jpanel paintcomponent

最近我一直试图在java中复制Space Invaders,以帮助学习使用java和一般编程语言开发应用程序。但是我遇到了JFrame的一个小问题:我为窗口声明的背景颜色不会停留,它只是闪烁然后恢复为默认值。这是我的代码:

import javax.imageio.ImageIO;`
import java.io.*;`
import javax.swing.*;`
import java.awt.*;`
import java.awt.image.*;`
import java.awt.image.ImageObserver;`
import java.awt.event.*;`

public class Invaders extends JPanel{

    public static int x = 40;
    public static int y = 345;
    public static int h = 20;
    public static int k = 180;
    public static int move = 1;
    static final Invaders m = new Invaders();

    public static void main(String[] args){

        final JFrame frame = new JFrame("Movement of 2d Shapes");
        frame.setSize(404,390);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(m);
        frame.setBackground(Color.BLACK);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                if(x <= 350){
                    x += 10;
                    m.repaint();
                };
            }
        };

        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                if(x >= 10){
                    x -= 10;
                    m.repaint();
                };
            }
        };

        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");

        InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        m.getActionMap().put("RIGHT", actionRight);
        m.getActionMap().put("LEFT", actionLeft);

    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        draw(g);
        cpu_move(m);
    }

    public void cpu_move(Invaders m){ 
        if(h == 0){
            move = 0;
        }else if(h == 375){
            move = 1;
        }
        if(move == 0){
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            h += 5;
            m.repaint();
        }else if(move == 1){
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            h -= 5;
            m.repaint();
        };
    }

    public void draw(Graphics g){
        try{
            g.drawImage(ImageIO.read(getClass().getResource(
                 "images/Ship.jpg")), x, y, 35, 23, Color.BLACK, null);
            g.drawImage(ImageIO.read(getClass().getResource(
                 "images/Alien.jpg")), h, k, 28, 20, Color.BLACK, null);
        }catch(IOException k){
            Component temporaryLostComponent = null;
            JOptionPane.showMessageDialog(temporaryLostComponent, 
                  "one or more image files missing or corrupt");
        }
    }
}

背景颜色的声明有什么问题?编译时没有错误,但它仍然会这样做。我做错了什么?

2 个答案:

答案 0 :(得分:1)

frame.getContentPane().add(m);
//frame.setBackground(Color.BLACK);
frame.getContentPane().setBackground( Color.BLACK );

阅读Using Top Level Containters上的Swing教程中的部分,以了解框架的结构。内容窗格绘制在框架的顶部。

答案 1 :(得分:1)

尝试下一步:

frame.getContentPane().add(m);
m.setBackground(Color.BLACK);

代替frame.setBackground(Color.BLACK);,因为Invaders m填充所有框架。

你的背景将是黑色的图像。