我在java中编写这个gui并且效果很好,除了我最近发现了一个bug。目前我通过文件菜单访问每个屏幕,并使用以下方法在我正在查看的面板之间来回切换。我还包括了一个文件菜单actionlisteners执行的方法。为了简洁起见,我没有包括其他人,只知道他们以非常相似的顺序使用相同类型的命令。
问题在于,有时在屏幕之间点击时,新面板上仍会显示上一个面板的元素。这个新面板通常会缺少大部分或全部元素。我遇到的另一个(也可能是相关的)问题是当我运行代码的一半或更多时间我看到我的初始屏幕但在此之后当我点击进入新屏幕时根本没有显示任何内容。这非常令人困惑,因为我不会使用代码更改任何内容,甚至不会在运行之间重新编译,它的行为也不同。第二个问题只发生在我为了简单起见而将面板的add方法移动到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);
}
如果您需要查看更多代码,请告诉我们。我真的很感激我能得到的任何帮助!感谢
答案 0 :(得分:1)
我不被评论,所以也许这不是一个完整的答案:
您重新验证并重新绘制面板,是否对显示它的框架或面板执行此操作?
如前所述,卡片布局是一种更好的方式。