gpt4 book ai didi

java - Swing gui 未正确隐藏/显示

转载 作者:行者123 更新时间:2023-12-01 11:56:05 25 4
gpt4 key购买 nike

我正在用 java 编写这个 gui,它运行得很好,除了我最近发现了一个错误。目前,我通过文件菜单访问每个屏幕,并使用以下方法在我正在查看的面板之间来回切换。我还包含了文件菜单操作监听器之一执行的方法。为了简洁起见,我没有包括其他命令,只需知道它们以非常相似的顺序使用相同类型的命令即可。

问题是,有时在屏幕之间单击时,前一个面板的元素在新面板上仍然可见。这个新面板通常也会丢失大部分或全部元素。我遇到的另一个(可能相关)问题是,当我运行代码的一半或更多时间时,我会看到初始屏幕,但之后当我单击新屏幕时,根本不会显示任何内容。这非常令人困惑,因为我没有对代码进行任何更改,甚至没有在运行之间重新编译,而且它的行为有所不同。自从我为了简单起见将面板的添加方法移至 setCurrentPanel 后,第二个问题才出现。

private void setCurrentPanel(JPanel current) {

System.out.println(oldCurrent.getName() + " " + current.getName());
if (oldCurrent.getName().equals(current.getName())) {

} else {
buildingPanel.setVisible(false);
securityPanel.setVisible(false);
adminUsersPanel.setVisible(false);
adminBuildingPanel.setVisible(false);
adminServerPanel.setVisible(false);
changePasswordPanel.setVisible(false);
serverSettingsPanel.setVisible(false);
addBuildingPanel.setVisible(false);
addUser.setVisible(false);
for (BuildingItem item : buildingMenuItem) {
item.panel.setVisible(false);
}
add(current);
current.setVisible(true);
revalidate();
repaint();
oldCurrent = current;
refreshCount = refreshCount + 1;
System.out.println("Refresh " + refreshCount);
}
}

private void setupAdminServerPanel() {

getServerSettingsFromSQL();
serverSettingsPanel = new JPanel(new GridBagLayout());
serverSettingsPanel.setName("Server Settings Panel");
GridBagConstraints gbr = new GridBagConstraints();
SpinnerModel minPasswordModel = new SpinnerNumberModel(Integer.parseInt(settingMinPassword), 5, 20, 1);
SpinnerModel minUsernameModel = new SpinnerNumberModel(Integer.parseInt(settingMinUsername), 5, 20, 1);
final JSpinner minPasswordSpinner = new JSpinner(minPasswordModel);
final JSpinner minUsernameSpinner = new JSpinner(minUsernameModel);
JButton lockdownButton = new JButton("Lockdown");
lockdownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
lockdownMode();
}
});
JButton ApplyButton = new JButton("Apply");
ApplyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (minPasswordSpinner.getValue() != Integer.parseInt(settingMinPassword) ||
(minUsernameSpinner.getValue() != Integer.parseInt(settingMinUsername))) {
writeSettingsToSQL((Integer) minPasswordSpinner.getValue(),
(Integer) minUsernameSpinner.getValue());
getServerSettingsFromSQL();
}
}
});
lockdownButton.setPreferredSize(new Dimension(50, 50));
minUsernameSpinner.setPreferredSize(new Dimension(150, 30));
minPasswordSpinner.setPreferredSize(new Dimension(150, 30));
gbr.gridy = 0;
gbr.gridx = 1;
gbr.gridwidth = 1;
gbr.ipady = 0;
gbr.insets = new Insets(10, 10, 10, 10);
serverSettingsPanel.add(lockdownButton, gbr);
gbr.gridy = 1;
gbr.gridx = 0;
serverSettingsPanel.add(new JLabel("Minimum Username Length"), gbr);
gbr.gridy = 2;
gbr.gridx = 0;
serverSettingsPanel.add(new JLabel("Minimum Password Length"), gbr);
gbr.gridy = 1;
gbr.gridx = 1;
gbr.gridwidth = 2;
serverSettingsPanel.add(minUsernameSpinner, gbr);
gbr.gridy = 2;
serverSettingsPanel.add(minPasswordSpinner, gbr);
gbr.gridy = 3;
gbr.gridx = 1;
gbr.gridwidth = 1;
serverSettingsPanel.add(ApplyButton, gbr);

setCurrentPanel(serverSettingsPanel);
}

如果您需要查看更多代码,请告诉我。我真的很感激我能得到的任何帮助!谢谢

最佳答案

我无权发表评论,所以也许这不是完整的答案:

您重新验证并重新绘制面板,是对显示它的框架或面板进行操作吗?

正如已经建议的,卡片布局是更好的方法。

关于java - Swing gui 未正确隐藏/显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com