如何构建JRadioButton助手类,以便我可以轻松地重用JRadioButton

时间:2019-04-26 19:57:00

标签: java swing class helper jradiobutton

我正在使用免费的在线Stanford CS106A类自学Java。我建立了一个Java类“ ClickForStar”,其中包括一个JRadioButton,它提供了3个选项,小,中,大,用户可以在其中选择单击画布时将生成的星号。我的JRadioButton在这里可以正常使用。

我想做的是用我的JRadioButton代码构建一个辅助类“ JRadioButtonSizeSelectClass”,这样我就可以轻松地在任何我想要一个JRadioButton的程序中调用该辅助类,该程序提供3个选项,小,中,大。这是我需要帮助的地方。我不知道如何使助手类正常工作。

下面的代码摘录在完全正常运行的Java类“ ClickForStar”中显示了我正在工作的JRadioButton,然后尝试将JRadioButton移至帮助器类“ JRadioButtonSizeSelectClass”中

请参见下面的代码

//Working JRadioButton code from "ClickForStar" GraphicsProgram
    public void addJButtonsizeGroup(){
        /* Create JRadioButtons to select star size */
        smallButton = new JRadioButton("Small");    //   1. Create JRadioButton for each option stored in instance var.
        mediumButton = new JRadioButton("Medium");  //   1. ""
        largeButton = new JRadioButton("Large");    //   1. ""
        ButtonGroup sizeGroup = new ButtonGroup();  //   2. Create new ButtonGroup which is initially empty. 
        sizeGroup.add(smallButton);                 //   3. Add buttons to the ButtonGroup using add method
        sizeGroup.add(mediumButton);                //   3. ""
        sizeGroup.add(largeButton);                 //   3. ""
        mediumButton.setSelected(true);             //   4. Call setSelected to initially select desired button
        add(smallButton, SOUTH);                    //   5. Add each radio button to the interface
        add(mediumButton, SOUTH);                   //   5. ""
        add(largeButton, SOUTH);                    //   5. ""      
    }

// My attempt to move JRadioButton to a helper class that doesn't work:

public class JRadioButtonSizeSelectClass extends JRadioButton{

    /**
     * [I couldn't get this class to work.] Creates JRadioButton "small, medium, and large", with instance variables 
     * SizeGroup (type ButtonGroup that contains the three buttons), 
     * and smallButton, mediumButton, and largeButton (Type JRadioButton)
     */
    public JRadioButtonSizeSelectClass(){
        /* Create JRadioButtons to select star size */
        smallButton = new JRadioButton("Small");    //   1. Create JRadioButton for each option stored in instance var.
        mediumButton = new JRadioButton("Medium");  //   1. ""
        largeButton = new JRadioButton("Large");    //   1. ""
        ButtonGroup sizeGroup = new ButtonGroup();  //   2. Create new ButtonGroup which is initially empty. 
        sizeGroup.add(smallButton);                 //   3. Add buttons to the ButtonGroup using add method
        sizeGroup.add(mediumButton);                //   3. ""
        sizeGroup.add(largeButton);                 //   3. ""
        mediumButton.setSelected(true);             //   4. Call setSelected to initially select desired button
        add(smallButton, SOUTH);                    //   5. Add each radio button to the interface
        add(mediumButton, SOUTH);                   //   5. ""
        add(largeButton, SOUTH);                    //   5. ""      

    }

    /* Instance variables */
    private JRadioButton smallButton; //Instance variable for JRadioButton
    private JRadioButton mediumButton; //Instance variable for JRadioButton
    private JRadioButton largeButton; //Instance variable for JRadioButton

}

我试图通过add(JRadioButtonSizeSelectClass();来调用助手类“ JRadioButtonSizeSelectClass”;但是程序崩溃了。

0 个答案:

没有答案
相关问题