繁体   English   中英

无法访问扩展 JPanel 的 class 的公共成员

[英]cannot access public members of class extending JPanel

这是我的 class

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());

        }

    }

注意:其中一些变量在调用此 function 的 class 中具有全局 scope。 因此代码编译并运行,并显示对话框等。

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

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

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

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

我认为您可以直接使用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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM