【问题标题】:JFrame background color flashes but then revertsJFrame 背景颜色闪烁但随后恢复
【发布时间】:2013-11-08 17:29:18
【问题描述】:

最近我一直在尝试在 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");
        }
    }
}

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

【问题讨论】:

    标签: java swing jframe jpanel paintcomponent


    【解决方案1】:

    尝试下一个:

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

    而不是frame.setBackground(Color.BLACK);,因为Invaders m 填满了所有帧。

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

    【讨论】:

      【解决方案2】:
      frame.getContentPane().add(m);
      //frame.setBackground(Color.BLACK);
      frame.getContentPane().setBackground( Color.BLACK );
      

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

      【讨论】:

      • 好消息是,当我将frame.setBackground() 更改为frame.getContentPane().setBackground 时,颜色不再闪烁。坏消息是现在根本不显示背景颜色。展示位置可能有问题吗?
      • @user2946438,你的代码有一个add(m) 语句。什么是“米”。它是覆盖内容窗格整个区域的另一个面板吗?如果是这样,您将看不到内容窗格的背景,您需要设置 Invaders 类的背景颜色。
      • @user2946438,也刚刚注意到你的 cpu_move() 方法有一个 Thread.sleep() 方法。您永远不应该在绘画()方法或在事件调度线程上执行的任何代码中执行 Thread.sleep。相反,您应该使用 Swing Timer 来制作动画。阅读 Swing 教程。在ConcurrencyTimers 上有区域部分。首先学习 Swing 的基础知识。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多