简体   繁体   English

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

[英]cannot access public members of class extending JPanel

Here is my class这是我的 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();
    }
}

I'm using the following code to create it and I am trying to access the user input from the panel components.我正在使用以下代码来创建它,并且我正在尝试从面板组件访问用户输入。

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

        }

    }

note: some of these variables have global scope in the class calling this function.注意:其中一些变量在调用此 function 的 class 中具有全局 scope。 So the code compiles and runs, and shows the dialog etc.因此代码编译并运行,并显示对话框等。

I'm trying to make userChoice reflect the radio button selection.我试图让userChoice反映单选按钮的选择。 However, I cannot access the radio buttons directly via dialogPanel.newCloset or dialogPanel.loadCloset as it tells me it does not exist.但是,我无法通过dialogPanel.newClosetdialogPanel.loadCloset直接访问单选按钮,因为它告诉我它不存在。

So I tried making the button group a public attribute, too.所以我也尝试将按钮组设为公共属性。 And tried retrieving the String using dialogPanel.newOrLoad.getSelection().getActionCommand() but that also was acting like it didn't exist.并尝试使用dialogPanel.newOrLoad.getSelection().getActionCommand()检索字符串,但这也表现得好像它不存在。

So I tried making that getter for the getActionCommand() as above and it can't find that public function either.所以我试着为上面的getActionCommand()制作那个吸气剂,它也找不到公共 function 。

I have other classes that extend JPanel that have accessible public attributes.我还有其他扩展 JPanel 的类,这些类具有可访问的公共属性。 I don't get what's wrong here.我不明白这里有什么问题。 All I can think is that the dialogPanel is getting destroyed when the dialog closes, except in my debugger it's still showing all the components existing.我能想到的只是对话框关闭时 dialogPanel 被破坏了,除了在我的调试器中它仍然显示所有存在的组件。 And well, if it is the case that those things aren't persisting.. how do I fix this?好吧,如果这些事情没有持续存在的话..我该如何解决这个问题? I'd prefer not to build the panel inside my function as I want to keep things clean.我不想在我的 function 中构建面板,因为我想保持清洁。

I think you can use NewOrLoadPanel type directly instead of JPanel :我认为您可以直接使用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