简体   繁体   English

秋千游戏布局问题

[英]Swing game layout issue

I'm working on implementing a game in layered panes and i'm running into some layout issues. 我正在努力在分层窗格中实现游戏,并且遇到了一些布局问题。 For some reason, nothing lines up correctly. 由于某种原因,没有任何东西正确排列。 I end up with a white space at the top of my frame and the frame never extends out to the correct size to fit everything. 我最后在框架的顶部留有一个空白,并且框架永远不会延伸到适合所有尺寸的正确尺寸。 I've tried frame.pack() because I've done that before (without layeredpanes) but that doesn't seem to work either. 我已经尝试过frame.pack(),因为我之前已经做过了(没有分层窗格),但这似乎也不起作用。

If anybody could help me out, I'd appreciate it. 如果有人可以帮助我,我将不胜感激。 I have a feeling there is something small that I'm missing. 我觉得我缺少一些小东西。

public class gameGUI extends JFrame{

//set up all the variables that will be used in the gameGUI
static int skyWidth = 1024;
static int skyHeight = 120;
static int sidebarWidth = 120;
static int sidebarHeight = 480;
static int boardWidth = 904;
static int boardHeight = 480;
private JLayeredPane pane = new JLayeredPane();
public static YardDriver thisDriver;
public static int getScreenWidth(){return boardWidth;}
public static int getScreenHeight(){return boardHeight;}

public JLayeredPane getPane(){
    return pane;
}


//Convert the width of the screen to work with the gui
public int convertX(int rainLoc,BufferedImage img){
    int x = rainLoc % 30;
    return ((boardWidth-img.getWidth())*x) / YardDriver.getScreenWidth();
}

//Convert the height of the screen to work with the gui
public int convertY(int rainLoc, BufferedImage img){
    int y = (rainLoc - (rainLoc % 30)) / 30;    
    return ((boardHeight-img.getHeight())*y) / YardDriver.getScreenWidth();
}   

//Convert the screen to the board
public static int convertStoB(Point screenLoc){
    int y = ((screenLoc.y - skyHeight) * YardDriver.getScreenHeight())/boardHeight;
    int x = ((screenLoc.x - sidebarWidth) * YardDriver.getScreenWidth())/boardWidth;
    return x + y*YardDriver.getScreenWidth();
}

private void loadAll() {

}

public gameGUI() {
    loadAll();

    JPanel timer = new JPanel();
    //place the image in a jlabel and set bounds
    timer.add(sky);
    //Set preferredsize, border, and set opaque
    timer.setPreferredSize(new Dimension(skyWidth,skyHeight));
    timer.setBounds(0, 0, skyWidth, skyHeight);
    timer.setOpaque(true);
    timer.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));

    JPanel sidebar = new JPanel();
    sidebar.setBounds(0, skyHeight, sidebarWidth, sidebarHeight);
    JButton nativeOne = new JButton();
    JButton nativeTwo = new JButton();
    JButton nativeThree = new JButton();
    JButton exoticOne = new JButton();
    JButton exoticTwo = new JButton();
    JButton exoticThree = new JButton();
    //Set the preferredsize and set a border
    sidebar.setLayout(new GridLayout(6, 1));
    sidebar.setPreferredSize(new Dimension(sidebarWidth, sidebarHeight));
    //Add each button to the sidebar
    sidebar.add(nativeOne);
    sidebar.add(nativeTwo);
    sidebar.add(nativeThree);
    sidebar.add(exoticOne);
    sidebar.add(exoticTwo);
    sidebar.add(exoticThree);
    sidebar.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));

    JPanel board = new gamePanel(this);
    board.setBounds(sidebarWidth, skyHeight, boardWidth, boardHeight);
    board.setPreferredSize(new Dimension(904, 480));
    board.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));

    timer.setVisible(true);
    sidebar.setVisible(true);
    board.setVisible(true);

    pane.add(timer, new Integer(1));
    pane.add(sidebar, new Integer(1));
    pane.add(board, new Integer(1));
    pane.setBounds(0,0,1024, 600);

    setTitle("Storm Watch");
    setSize(1024, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Pack the frame and set it to visible
    setVisible(true);
    setContentPane(pane);

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

        //Create a new frame with the createAndShowGUI 
        JFrame frame = new gameGUI();
    }

You forgot to set the LayoutManager, do something like this: 您忘了设置LayoutManager,执行以下操作:

this.pane.setLayout(new BorderLayout());
this.pane.add(timer, BorderLayout.PAGE_START);
this.pane.add(sidebar, BorderLayout.LINE_START);
this.pane.add(board, BorderLayout.CENTER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM