【问题标题】:Swing game layout issueSwing 游戏布局问题
【发布时间】:2012-11-15 02:21:18
【问题描述】:

我正在致力于在分层窗格中实现游戏,但遇到了一些布局问题。出于某种原因,没有正确排列。最后我的框架顶部有一个空白区域,框架永远不会延伸到正确的尺寸以适合所有东西。我试过 frame.pack() 因为我以前做过(没有分层窗格),但这似乎也不起作用。

如果有人可以帮助我,我将不胜感激。我有一种感觉,我缺少一些小东西。

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();
    }

【问题讨论】:

  • 啊!代码太多!你能提供一个SSCCE吗? sscce.org
  • @PicklishDoorknob 图片也是制作SSCCE 的问题。 OP 将需要执行类似 a) 小(以字节为单位)图像的热链接。 b) 在代码中生成它们。 c) 用 J2SE 组件代替,显示相同的布局问题。 -- 话虽如此,我怀疑问题是由于使用了JLayeredPane,它从内存中忽略了所有内容大小提示(或者至少默认没有布局管理器)。
  • @AndrewThompson 哈哈,每个人总是告诉我使用一个,但是当我试图告诉其他人时它不起作用:P
  • ImageIO.read(new File("images/Raindrop.png")); 与眼前的问题无关,但在部署时,这些可能是只能通过以下方式访问的embedded-resource。网址。请参阅embedded-resource tag Wiki 了解如何形成URL。
  • @Requiem 然后使用CardLayout 在组之间切换。 LayeredPane 结束了 - 恕我直言 - 这是天空和棋盘的不错选择,但对于游戏的其余部分则不是......

标签: java swing layout-manager jlayeredpane


【解决方案1】:

您忘记设置 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 2020-11-22
    相关资源
    最近更新 更多