我目前正在尝试为自己制作的游戏创建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();
}
}
最佳答案
确保将layeredPane
和timer
设置为opaque
。
例如...
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();
}
});
}