1

我目前正在尝试为我正在制作的游戏创建一个 gui,但我开始遇到一些麻烦。由于某种原因,我无法将计时器的背景更改为蓝色,并将 gameGUI 的背景更改为主板的绿色。这是我第一次在摇摆中编程,所以我对这一切都很陌生。

我也想知道我是否以正确的方式使用 layeredpane。该板需要能够成为分层窗格,以便我可以一直将图像放在它上面。计时器将是一个顶部有太阳的天空,并且云也会在某个时间点从其上方掠过。

如果你能给我一些建议并一路帮助我,即使你只是在这里和那里纠正一些小问题,那也很棒。

谢谢你。

public class gameGUI extends JPanel{

    private ArrayList<BufferedImage> nativeOne = new ArrayList<BufferedImage>();
    private ArrayList<BufferedImage> nativeTwo = new ArrayList<BufferedImage>();
    private ArrayList<BufferedImage> nativeThree = new ArrayList<BufferedImage>();
    private ArrayList<BufferedImage> exoticOne = new ArrayList<BufferedImage>();
    private ArrayList<BufferedImage> exoticTwo = new ArrayList<BufferedImage>();
    private ArrayList<BufferedImage> exoticThree = new ArrayList<BufferedImage>();

    private static int screenWidth = 680;
    private static int screenHeight = 680;

    private JLayeredPane layeredPane;

    public gameGUI(){        
        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(680, 680));
        layeredPane.setBackground(Color.GREEN);
        layeredPane.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));        
    }

    private static JLayeredPane timer(){
        JLayeredPane timer = new JLayeredPane();
        timer.setPreferredSize(new Dimension(800,120));
        timer.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        timer.setBackground(Color.BLUE);
        return timer;
    }

    private static JPanel sideBar(){
        //Create the side bar and add buttons
        JPanel returnMofo = new JPanel();
        JButton nativeOne = new JButton(native1);
        JButton nativeTwo = new JButton(native2);
        JButton nativeThree = new JButton(native3);
        JButton exoticOne = new JButton(exotic1);
        JButton exoticTwo = new JButton(exotic2);
        JButton exoticThree = new JButton(exotic3);
        returnMofo.setLayout(new GridLayout(6,1));
        returnMofo.setPreferredSize(new Dimension(120, 680));
        returnMofo.setBorder(BorderFactory.createLineBorder (Color.BLACK, 2));
        returnMofo.add(nativeOne);
        returnMofo.add(nativeTwo);
        returnMofo.add(nativeThree);
        returnMofo.add(exoticOne);
        returnMofo.add(exoticTwo);
        returnMofo.add(exoticThree);
        return returnMofo;
    }


    private static void createAndShowGUI(){
        JFrame frame = new JFrame();
        frame.setTitle("Storm Watch");
        frame.setSize(800, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameGUI gui = new gameGUI();
        frame.add(timer(), BorderLayout.NORTH);
        frame.add(sideBar(), BorderLayout.WEST);
        frame.add(gui, BorderLayout.EAST);                        

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args){    
        createAndShowGUI();    
    }                
}
4

2 回答 2

3

确保您将layeredPane和设置timeropaque

例如...

layeredPane.setOpaque(true);

您可能还想添加layeredPane到主板...

public TestGameBoard() {
    layeredPane = new JLayeredPane();
    layeredPane.setPreferredSize(new Dimension(680, 680));
    layeredPane.setBackground(Color.GREEN);
    layeredPane.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
    layeredPane.setOpaque(true);

    setLayout(new BorderLayout()); // Easy to use layout manager
    add(layeredPane); // Now I'm visible ;)
}

当我引起您的注意时,您应该始终在事件调度线程的上下文中创建/操作 UI...

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    });

}
于 2012-11-08T21:15:59.347 回答
1

两个问题。首先,在使用 JLayeredPane 时需要使用 setOpaque(true) 使背景色显示。

layeredPane.setBackground(Color.GREEN);
layeredPane.setOpaque(true);

...

timer.setBackground(Color.BLUE);
timer.setOpaque(true);

其次,您将 gameGUI 而不是 gameGUI 的 layeredPane 添加到框架中。应该:

frame.add(gui.layeredPane, BorderLayout.EAST);
于 2012-11-08T21:20:41.303 回答