我目前正在尝试为我正在制作的游戏创建一个 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();
}
}