0

我正在开发一个软件,用户必须在使用之前根据用户的需要设置软件。

当用户单击菜单项时,软件会抛出一个 JDialog 并要求用户输入,并且软件会存储输入。这工作正常。下一段我有问题。我想要一个面板内的切换按钮(用户输入的文本作为其标签)。我尝试使用categoryPanel.add(C.getCategoryButton)但没有用。请帮忙!提前致谢。

这是我所做的......我创建了一个扩展 JToggleButton 的 Category 类

public class Category extends JToggleButton implements ActionListener
{
    private JToggleButton categoryButton;


    public JToggleButton getCategoryButton()
    {
        buildCategoryButton();
        return categoryButton;
    }

    private void buildCategoryButton()
    {
        categoryButton = new JToggleButton();
        categoryButton.setText(MainFrame.getUserInput());
        categoryButton.setVisible(true);
    }

这是调用 getCategoryButton() 方法的地方

private void catCapBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        userInput = catCapTextField.getText(); //works fine
        Category C = new Category();
        categoryPanel.add(C.getCategoryButton()); //doesn't work
        validate();

        catCapture.setVisible(false);//this closes the JDialog, and it works fine.
    } 
4

1 回答 1

0

当您扩展 JToggleButton 时,您创建的 Category 类将成为 JToggleButton 的一个实例。因此,您不需要 JToggleButton 的私有实例。我建议如下:

public class Category extends JToggleButton implements ActionListener {

    public Category() {
        super(MainFrame.getUserInput());
    }

另外,如果我没记错的话,按钮不需要设置为可见。

于 2017-02-14T00:07:13.520 回答