在 Java NetBeans Maven 中工作时,JFrame 未加载到我在构造函数中添加的按钮或面板中

Working in Java NetBeans Maven, JFrame not loading in the buttons or panels I add in the constructor

提问人:Amadeus Grimm 提问时间:11/12/2023 更新时间:11/12/2023 访问量:35

问:

(这是我的第一个问题,如果我搞砸了格式,请道歉)

当我为这个项目创建代码时,目标是在侧边栏上制作一个包含多个 JPanels 和 JButtons 的 JFrame,作为在它们之间切换的一种方式。JFrame 的顶级布局管理器是一个 gridbaglayout,第一列由按钮组成,其右侧的列为包含 JPanel 的卡片布局腾出空间。但是,在运行代码时,不会创建组件。

构造函数是这样写的:

public HomePage(LobbyList LL, UserList UL, User USER) {
this.cardLayout = new CardLayout();
this.gridBagLayout = new GridBagLayout();
initComponents();
SetLobbyList(LL);
SetUserList(UL);
SetUser(USER);
CreatePanels();
}

声明所需各种面板的代码是这样编写的:

private JoinLobbyPanel JoinPanel; //add functionality by splitting up lobbypage
private NewLobbyPanel CreateLobby; // reference to the lobby creation page
private SearchUserPanel SearchUsers; // reference to the search user page
private JPanel cards; //contains the other functional JPanels
private final CardLayout cardLayout; //layout manager for cards
private final GridBagLayout gridBagLayout; //layout manager for grid
private JPanel grid; //contains the button components and the cards panel
private HomePanel HomePanel;

实际布局设置的代码在这里:

private void CreatePanels(){
JoinPanel = new JoinLobbyPanel(ll, ul, user);
CreateLobby = new NewLobbyPanel(ll, ul, user);
SearchUsers = new SearchUserPanel(ul);
// lobbyPage = new LobbyPage(ll, ul, user);
HomePanel = new HomePanel();
cards = new JPanel();
cards.setLayout(cardLayout);
cards.add(JoinPanel, "Join Lobby");
cards.add(CreateLobby, "Create Lobby");
cards.add(SearchUsers, "Search Users");
cards.add(HomePanel, "Home Page");
cardLayout.show(cards, "Home Page");
grid = new JPanel();
grid.setLayout(gridBagLayout);
GridBagConstraints gbc = new GridBagConstraints();
JButton homeButton = new JButton();
gbc.gridx=0;
gbc.gridy=0;
gbc.weighty=.5;
gbc.gridwidth=1;
gbc.gridheight=1;
homeButton.addActionListener((java.awt.event.ActionEvent e) -> {
cardLayout.show(cards, "Home Page");
});
grid.add(homeButton, gbc);
JButton joinButton = new JButton();
gbc.gridx=0;
gbc.gridy=1;
gbc.weighty=.5;
gbc.gridwidth=1;
gbc.gridheight=1;
joinButton.addActionListener((java.awt.event.ActionEvent e) -> {
cardLayout.show(cards, "Join Lobby");
});
grid.add(joinButton, gbc);
JButton createButton = new JButton();
gbc.gridx=0;
gbc.gridy=2;
gbc.weighty=.5;
gbc.gridwidth=1;
gbc.gridheight=1;
createButton.addActionListener((java.awt.event.ActionEvent e) -> {
cardLayout.show(cards, "Create Lobby");
});
grid.add(createButton, gbc);
JButton searchButton = new JButton();
gbc.gridx=0;
gbc.gridy=3;
gbc.weighty=.5;
gbc.gridwidth=1;
gbc.gridheight=1;
searchButton.addActionListener((java.awt.event.ActionEvent e) -> {
cardLayout.show(cards, "Search Users");
});
grid.add(searchButton, gbc);
JButton logOutButton = new JButton();
gbc.gridx=0;
gbc.gridy=4;
gbc.weighty=.5;
gbc.gridwidth=1;
gbc.gridheight=1;
logOutButton.addActionListener((java.awt.event.ActionEvent e) -> {
ul.test();
SignInPage SI = new SignInPage(ul);
SI.setVisible(true);
this.dispose();
});
grid.add(logOutButton, gbc);
gbc.fill=GridBagConstraints.BOTH;
gbc.gridx=1;
gbc.gridy=0;
gbc.weighty=1;
gbc.gridwidth=5;
gbc.gridheight=5;
grid.add(cards, gbc);
this.add(grid);
repaint();
revalidate();
this.pack();
}

我尝试了一些东西,包括检查以确保我使用了重新绘制、重新验证和打包,以防万一其中一个有问题。我还尝试在构造函数中创建一个组件(用于实验的 JTextLabel),但我注意到构造函数实际上也没有创建 JTextLabel,这向我表明构造函数不知何故不起作用。当我将测试标签放在之前以及将其放在 InitComponents() 方法之后时,我遇到了这个问题。我真的不确定从哪里开始调试它,因为我是一个相当新的程序员,并且主要只是试图重新组织我的一个队友的 GUI 设计,这依赖于一个非常低效的系统,每次你在页面之间交换时创建一个新的 JFrame。我没有收到任何运行时或编译时错误,似乎这要么是我的逻辑错误,要么是编译器的错误。你们中有人遇到过这样的问题,你们是如何解决的?

(我不确定代码的stackoverflow是什么意思需要缩进四个空格,所以我只是将所有代码都设在距离文本框侧面四个空格的地方。如果这意味着正常的缩进,但只是将所有缩进设置为四个空格,我深表歉意,我会解决这个问题)

Java Swing NetBeans

评论


答: 暂无答案