我有一项任务要求我创建您在图像中看到的布局,作为游戏开发的一部分。我以前从未在桌面应用程序中使用过 Java,因此在使用 Swing 和 AWT 库方面我完全是个菜鸟。该图像建议我们使用 JLayeredPane 作为根容器,然后将其余部分添加到其顶部。我的问题是,我尝试从背景图像和 gridLayout 开始,但我似乎无法显示除背景之外的任何内容。网格根本不显示(没有边框线,没有单元格背景),并且我添加到其中的任何其他组件都失败了。有人能在这里指出正确的方向吗?我已经阅读了文档并看到了各种布局、容器和组件的一些示例,但我似乎无法让它们一起工作。
到目前为止,这是我的代码:
public class BoardView extends JFrame{
// Constructor
public BoardView() {
JFrame window = new JFrame("Sorry Game"); // create a new Jwindow instance
ImageIcon appIcon = new ImageIcon(getClass().getClassLoader().getResource("res/icon.png")); // create the icon for the app
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when the 'X' button is clicked, the app stops
window.setSize(new Dimension(1000, 700)); // setting the size of the window
window.setResizable(false); // Window won't be resizable
window.setIconImage(appIcon.getImage()); // set the icon for the app
window.setLayout(new BorderLayout());
JLayeredPane layeredPane = new JLayeredPane();
JLabel background = new JLabel();
background.setSize(1000,700);
background.setIcon(new ImageIcon(getClass().getClassLoader().getResource("res/background.png"))); for the JLabel
layeredPane.add(background,0);
JPanel gridPanel = new JPanel(new GridLayout(16,16));
gridPanel.setSize(650,700);
layeredPane.add(gridPanel);
for(int i = 0; i < 256; i++) {
JLabel tile = new JLabel();
tile.setBackground(Color.red);
tile.setBorder(new LineBorder(Color.black));
gridPanel.add(tile);
}
JLabel logo = new JLabel();
logo.setIcon(new ImageIcon(getClass().getClassLoader().getResource("res/sorryImage.png")));
layeredPane.add(logo);
window.add(layeredPane);
window.setLocationRelativeTo(null); // centers the window to the screen
window.setVisible(true); // make the window visible
}
}
我的思考过程是,我可以将 JFrame 的布局设置为 BorderLayout,以便我可以将最终布局分为两部分,西部分和东部分。西边的一个将包含网格以及各种 JLabel 和按钮,东边的一个将包含其余组件。我尝试过使用BorderLayout.WEST
& EAST
向 JFrame 添加组件时的参数,但没有一个起作用或改变任何事情。当按照文档将组件添加到 JLayeredPane 时,我还尝试使用深度索引,但同样没有任何变化。
附:请注意,我并不是在找人为我创建布局。我希望有人帮助我了解我做错了什么以及创建此类布局的最佳方法是什么。
为了初始化我想要在其中包含图像的网格单元格,我不需要将它们手动添加到这些位置吗?
如果您使用GridLayout
每个单元都必须有一个组件,并且组件必须按顺序添加。也就是说,当添加组件时,它们将根据需要自动换行到下一行。
因此,即使您不想在单元格中添加图像,您也需要添加一个虚拟组件,例如没有文本/图标的 JLabel。
更简单的方法可能是使用GridBagLayout
. The GridBagLayout
可以配置为没有组件的单元“保留”空间。因此您可以将组件添加到特定单元格。
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
public class GridBagLayoutSparse extends JPanel
{
public GridBagLayoutSparse()
{
setBorder( new LineBorder(Color.RED) );
GridBagLayout gbl = new GridBagLayout();
setLayout( gbl );
/*
// Set up a grid with 5 rows and columns.
// The minimimum width of a column is 50 pixels
// and the minimum height of a row is 20 pixels.
int[] columns = new int[5];
Arrays.fill(columns, 50);
gbl.columnWidths = columns;
int[] rows = new int[5];
Arrays.fill(rows, 20);
gbl.rowHeights = rows;
*/
// Add components to the grid at top/left and bottom/right
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
addLabel("Cell 0:0", gbc);
gbc.gridx = 3;
gbc.gridy = 4;
addLabel("Cell 3:4", gbc);
}
private void addLabel(String text, GridBagConstraints gbc)
{
JLabel label = new JLabel(text);
label.setBorder( new LineBorder(Color.BLUE) );
add(label, gbc);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("GridBagLayoutSparse");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout( new GridBagLayout() );
frame.add(new GridBagLayoutSparse());
frame.setSize(300, 300);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
}
}
- 按原样运行代码,组件将在中心分组在一起。
- 取消注释块注释并再次运行。这些组件将被放置在适当的单元格中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)