【问题标题】:cannot access public members of class extending JPanel无法访问扩展 JPanel 的类的公共成员
【发布时间】:2020-07-03 00:04:43
【问题描述】:

这是我的课

import javax.swing.*;
import java.util.Map;

public class NewOrLoadPanel extends JPanel {
    public DefaultComboBoxModel<String> usernameChoiceList;
    public JRadioButton newCloset = new JRadioButton();
    public JRadioButton loadCloset = new JRadioButton();
    public JButton next;
    public JComboBox usernameChoice;
    ButtonGroup newOrLoad = new ButtonGroup();

    public NewOrLoadPanel(XMLParsed choices) {
        initComponents(choices);
    }

    private void initComponents(XMLParsed choices) {

        newCloset.setText("New Closet File");
        newCloset.setActionCommand("new");
        loadCloset.setText("Load Closet from File...");
        loadCloset.setActionCommand("load");
        newOrLoad.add(newCloset);
        newOrLoad.add(loadCloset);
        next = new JButton("Next");
        add(loadCloset);
        add(newCloset);
        JLabel inputLabel = new JLabel("Choose a username from the list below: ");
        add(inputLabel);

        usernameChoiceList = new DefaultComboBoxModel<>();
        for (Map.Entry<String, String> mapEntry : choices.hexUsernameMap.entrySet()) {
            usernameChoiceList.addElement(mapEntry.getKey());
        }
        usernameChoice = new JComboBox(usernameChoiceList);
        add(usernameChoice);

    }
    public String getSelectedAction() {
        return newOrLoad.getSelection().getActionCommand();
    }
}

我正在使用以下代码来创建它,并且我正在尝试从面板组件访问用户输入。

private void newOrLoadChooser() {
        hexUserObject = new XMLParsed();
        JPanel dialogPanel = new NewOrLoadPanel(hexUserObject);
        int result = JOptionPane.showConfirmDialog(null, dialogPanel, "", JOptionPane.OK_CANCEL_OPTION);
        boolean userChoice = dialogPanel.getComponent(1).isFocusOwner();
        if (userChoice == true) { // new file
            newClosetChooser();
            myInv = new ParsedInventory(hexValue, username);
            validateFolders(myInv.getUsername());
        }
        else {
            myInv = loadInventoryFile();
            validateFolders(myInv.getUsername());

        }

    }

注意:其中一些变量在调用此函数的类中具有全局范围。所以代码编译运行,显示对话框等。

我正在尝试使 userChoice 反映单选按钮的选择。但是,我无法通过dialogPanel.newClosetdialogPanel.loadCloset 直接访问单选按钮,因为它告诉我它不存在。

所以我也尝试将按钮组设为公共属性。并尝试使用dialogPanel.newOrLoad.getSelection().getActionCommand() 检索字符串,但这也表现得好像它不存在。

所以我尝试为上面的getActionCommand() 制作那个吸气剂,但它也找不到那个公共函数。

我还有其他扩展 JPanel 的类,这些类具有可访问的公共属性。我不明白这里有什么问题。我能想到的只是对话框关闭时 dialogPanel 被破坏了,除了在我的调试器中它仍然显示所有存在的组件。好吧,如果这些事情没有持续存在的话..我该如何解决这个问题?我不想在我的函数中构建面板,因为我想保持干净。

【问题讨论】:

  • 由于没有理由在上面的代码中明显扩展JPanel,我的建议是不要在这里扩展,只需使用普通@987654329 @.
  • @AndrewThompson 我扩展它的原因是将所有设置代码从该功能中取出,并保持代码更清晰/更分区。这不是一般的最佳做法吗?

标签: java swing public


【解决方案1】:

我觉得你可以直接用NewOrLoadPanel类型代替JPanel

NewOrLoadPanel dialogPanel = new NewOrLoadPanel(hexUserObject);

int result = JOptionPane.showConfirmDialog(null, dialogPanel, "", JOptionPane.OK_CANCEL_OPTION);
boolean isLoadCloset = dialogPanel.loadCloset.isSelected();
boolean isNewCloset = dialogPanel.newCloset.isSelected();

【讨论】:

  • 这很好。不幸的是,它不能解决我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
  • 2022-10-19
  • 1970-01-01
  • 2015-09-07
  • 2015-07-11
  • 2014-01-26
  • 1970-01-01
相关资源
最近更新 更多