Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java单选按钮更改另一个面板中的按钮文本_Java_Swing_Actionlistener_Jradiobutton - Fatal编程技术网

Java单选按钮更改另一个面板中的按钮文本

Java单选按钮更改另一个面板中的按钮文本,java,swing,actionlistener,jradiobutton,Java,Swing,Actionlistener,Jradiobutton,我正在用java为我的编程II课程制作日历,我需要一些帮助。我制作了我的日历,使其成为6 x 7的按钮网格,并创建了一个方法,可以更改按钮文本以匹配所选月份(通过单选按钮选择)。它适用于默认选择的月份,但我无法让它在选择新单选按钮时更新ui。我的formatCalendar方法和日历按钮位于扩展JPanel的类CalendarPanel中,但我的单选按钮位于MonthRadioButtonPanel中,它也扩展了JPanel。我知道我单选按钮上的动作监听器工作正常,因为我用一个简单的系统测试过它

我正在用java为我的编程II课程制作日历,我需要一些帮助。我制作了我的日历,使其成为6 x 7的按钮网格,并创建了一个方法,可以更改按钮文本以匹配所选月份(通过单选按钮选择)。它适用于默认选择的月份,但我无法让它在选择新单选按钮时更新ui。我的formatCalendar方法和日历按钮位于扩展JPanel的类CalendarPanel中,但我的单选按钮位于MonthRadioButtonPanel中,它也扩展了JPanel。我知道我单选按钮上的动作监听器工作正常,因为我用一个简单的系统测试过它。out。println。。。谢谢,非常感谢您的帮助

我的单选按钮是在MonthRadioButtonPanel()构造函数中创建的

JRadioButton[] monthRadioButton;
RadioButtonListener listener = new RadioButtonListener();

public MonthRadioButtonPanel()
{
    monthRadioButton = new JRadioButton[12];
    ButtonGroup monthSelect = new ButtonGroup();

    this.setLayout(new GridLayout(2,6));

    //creates radio buttons with labels and default selected value
    monthRadioButton[0] = new JRadioButton("January",true);
    monthRadioButton[1] = new JRadioButton("February",false);
    monthRadioButton[2] = new JRadioButton("March",false);
    monthRadioButton[3] = new JRadioButton("April",false);
    monthRadioButton[4] = new JRadioButton("May",false);
    monthRadioButton[5] = new JRadioButton("June",false);
    monthRadioButton[6] = new JRadioButton("July",false);
    monthRadioButton[7] = new JRadioButton("August",false);
    monthRadioButton[8] = new JRadioButton("September",false);
    monthRadioButton[9] = new JRadioButton("October",false);
    monthRadioButton[10] = new JRadioButton("November",false);
    monthRadioButton[11] = new JRadioButton("December",false);

    for (int i = 0; i < monthRadioButton.length; i++)
    {
        //adds radio buttons and an action listener to each 
        monthSelect.add(monthRadioButton[i]);
        monthRadioButton[i].addActionListener(listener);      
        monthRadioButton[i].setActionCommand(monthRadioButton[i].getText());
        this.add(monthRadioButton[i]);                        
    }

}
编辑

所以我改变了我的RadioButtonListener,就像@Hovercraft Full Of鳗鱼所建议的那样,它看起来像:

class RadioButtonListener implements ActionListener
{
    private CalendarPanel cal;

    public RadioButtonListener()
    {

    }

    public RadioButtonListener(CalendarPanel cal) {
        this.cal = cal;
    }

    public void actionPerformed(ActionEvent e)
    {
        cal.formatCalendar(getDaysInMonth(), getFirstDayOfWeek());
        cal.revalidate();
        cal.repaint();
    }    
}
}

我的框架类构造函数编译得很好,但当我单击单选按钮时,它会抛出空指针异常

public class Frame extends JFrame
{

//creates frame with added panels
public Frame()
{
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Calendar");
    this.setSize(900,400);
    this.setLayout(new BorderLayout());

    MonthRadioButtonPanel mon = new MonthRadioButtonPanel();
    this.add(mon, BorderLayout.SOUTH);

    CalendarPanel cal = new CalendarPanel();

    MonthRadioButtonPanel.RadioButtonListener list = mon.new RadioButtonListener(cal);

    this.add(cal, BorderLayout.WEST);
    this.add(new TitlePanel(), BorderLayout.NORTH);

    this.setVisible(true);

}

}

您对错误的CalendarPanel对象进行了更改,该对象是专门在侦听器中创建的。同时,显示的CalendarPanel对象对这些更改一无所知

class RadioButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        // the line below creates a *new* CalendarPanel object
        // one completely unrelated to the displayed CalendarPanel 
        CalendarPanel cal = new CalendarPanel();
        cal.formatCalendar(getDaysInMonth(), getFirstDayOfWeek());
        cal.revalidate();
        cal.repaint();
    }    
}
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 解决方案是将对显示的CalendarPanel对象的引用传递给侦听器,并对其进行更改

class RadioButtonListener implements ActionListener
{
    private CalendarPanel cal;

    public RadioButtonListener(CalendarPanel cal) {
       this.cal = cal;
    }
    public void actionPerformed(ActionEvent e)
    {
        cal.formatCalendar(getDaysInMonth(), getFirstDayOfWeek());
        cal.revalidate();
        cal.repaint();
    }    
}
创建上述侦听器时,需要传递对有效显示的CalendarPanel对象的引用,以使其工作


编辑
您在评论中声明:


好的,我这样做了,并在我的框架构造函数中实例化了它(它也创建并添加了我的其他面板),但它创建了一个空指针异常

public class Frame extends JFrame
{

//creates frame with added panels
public Frame()
{
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Calendar");
    this.setSize(900,400);
    this.setLayout(new BorderLayout());

    MonthRadioButtonPanel mon = new MonthRadioButtonPanel();
    this.add(mon, BorderLayout.SOUTH);

    CalendarPanel cal = new CalendarPanel();

    MonthRadioButtonPanel.RadioButtonListener list = mon.new RadioButtonListener(cal);

    this.add(cal, BorderLayout.WEST);
    this.add(new TitlePanel(), BorderLayout.NORTH);

    this.setVisible(true);

}

}
如果您的问题有任何重大更改,请编辑您的问题(正如我现在所做的),在当前问题的底部发布新代码和新描述。如果可以避免更改原始问题或代码,以免使您的答案无效,请不要更改原始问题或代码

现在谈谈您的问题:我只能猜测,因为我们没有看到您更改的代码,但是您是否在将有效的CalendarPlayer分配给其变量之前,将CalendarPlayer变量传入RadioButtonListener构造函数。如果是这样,这将解释这一点,因为您将把
null
传递到RadioButtonListener构造函数中。解决方案是在调用RadioButtonListener构造函数之前,首先将CalendarPlayer分配给它的变量

JRadioButton[] monthRadioButton;
RadioButtonListener listener = new RadioButtonListener();

public MonthRadioButtonPanel()
{
    monthRadioButton = new JRadioButton[12];
    ButtonGroup monthSelect = new ButtonGroup();

    this.setLayout(new GridLayout(2,6));

    //creates radio buttons with labels and default selected value
    monthRadioButton[0] = new JRadioButton("January",true);
    monthRadioButton[1] = new JRadioButton("February",false);
    monthRadioButton[2] = new JRadioButton("March",false);
    monthRadioButton[3] = new JRadioButton("April",false);
    monthRadioButton[4] = new JRadioButton("May",false);
    monthRadioButton[5] = new JRadioButton("June",false);
    monthRadioButton[6] = new JRadioButton("July",false);
    monthRadioButton[7] = new JRadioButton("August",false);
    monthRadioButton[8] = new JRadioButton("September",false);
    monthRadioButton[9] = new JRadioButton("October",false);
    monthRadioButton[10] = new JRadioButton("November",false);
    monthRadioButton[11] = new JRadioButton("December",false);

    for (int i = 0; i < monthRadioButton.length; i++)
    {
        //adds radio buttons and an action listener to each 
        monthSelect.add(monthRadioButton[i]);
        monthRadioButton[i].addActionListener(listener);      
        monthRadioButton[i].setActionCommand(monthRadioButton[i].getText());
        this.add(monthRadioButton[i]);                        
    }

}

编辑2


您可能会创建两个RadioButtonListener,一个使用默认构造函数,一个不带参数,另一个使用参数。摆脱默认构造函数,同样是没有参数的构造函数,不要创建其中的两个对象(这毫无意义,让我摸不着头脑为什么要这样做),然后努力找出一种方法,将正确的引用引入侦听器。如果仔细考虑,您应该能够弄清楚这一点,而不是从这里复制和粘贴代码。

好的,我这样做了,并在我的框架构造函数中实例化了它(它还创建和添加了我的其他面板),但它会创建空指针异常。对不起,我不熟悉堆栈溢出。我用我的新代码编辑了我的问题。谢谢。:)@玛莎:见编辑2。您不能只是从这里复制和粘贴代码,然后期望它工作。你必须使用这里介绍的想法来帮助你思考和修改你自己的代码。你是对的。当我发布这个问题的时候,我很累,已经有一段时间我一直在用脑袋来思考这个问题,所以我希望它会这么简单。然而,多亏了你的建议和一个良好的睡眠,我才解决了这个问题。谢谢大家!@玛莎:晚上睡个好觉真是太棒了——祝贺你,还有精彩的表演!