问题描述
有没有办法在 Java 中使用 1 个以上的布局管理器.现在我正在使用 gridLayout 来实现一个棋盘,但在它下面我想放一些其他的东西,但不在 gridLayout 中.也许是 FlowLayout 或其他一些布局.我该怎么做呢?谢谢!
Is there way to use more than 1 layout manager in Java. Right now I'm using a gridLayout to implement a chess board but beneath it I would like to put some other stuff but not in a gridLayout. Maybe a FlowLayout or some other layout. How would I go about doing this?Thanks!
推荐答案
是的,您只需要规划整个 UI 布局(即窗口、主面板等)
Yes, all you need is to plan your over all UI Layout (i.e; Window, master panel etc)
例如,您需要在棋盘下放置一些东西,我通常会在基本级别使用 BorderLayout.
For example, you need to put something under your chessboard, I would normally go with a BorderLayout at the basic level.
假设我有一个名为 masterPanel 的 JPanel,它包含我的国际象棋应用程序的所有组件.所以,代码看起来像:
So assume I have a JPanel called masterPanel, that holds all the components for my chess app.So, code would look like:
JPanel masterPanel = new JPanel(new BorderLayout());
JPanel chessBoardPanel = createChessboardPanel(); //assuming this method will return a
//JPanel with chess board using GridLayout
JPanel infoPanel = new JPanel(); //this is the panel that would contain info elements, that //may go below my chess board.
//Now add everything to master panel.
masterPanel.add(chessBoardPanel, BorderLayout.CENTER);
masterPanel.add(infoPanel, BorderLayout.PAGE_END);
//add masterPanel to your window (if required)
this.getContentPane().add(masterPanel);
这篇关于Java 中的多个布局管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!