Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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 与getSource()同时使用JRadioButtons和JButtons_Java_Arrays_Swing_Jbutton_Jradiobutton - Fatal编程技术网

Java 与getSource()同时使用JRadioButtons和JButtons

Java 与getSource()同时使用JRadioButtons和JButtons,java,arrays,swing,jbutton,jradiobutton,Java,Arrays,Swing,Jbutton,Jradiobutton,我对JavaGUI相当陌生。我有一个需要我使用它的项目。我基本上创建了一个4x4网格,用户只需单击方形按钮,就可以在网格上的任何位置输入数字1-4。我有4个单选按钮,标记为1-4。我想知道是否有一种方法,这样当我填充其中一个单选按钮时,我就可以单击网格上的任意位置,文本将设置为该单选按钮的文本。我的构造器工作得很好。我的代码中的actionPerformed方法有问题 public class Sym4GUI extends JFrame { private JButton [][] b

我对JavaGUI相当陌生。我有一个需要我使用它的项目。我基本上创建了一个4x4网格,用户只需单击方形按钮,就可以在网格上的任何位置输入数字1-4。我有4个单选按钮,标记为1-4。我想知道是否有一种方法,这样当我填充其中一个单选按钮时,我就可以单击网格上的任意位置,文本将设置为该单选按钮的文本。我的构造器工作得很好。我的代码中的actionPerformed方法有问题

public class Sym4GUI extends JFrame
{
    private JButton [][] buttons = new JButton [4][4];
private JRadioButton [] RButtons = new JRadioButton [4];
private JPanel pane1;
private JPanel pane2;
private ButtonHandler bh;
private Sym4 sym;
public Sym4GUI()
{
    super ("Welcome to Sym4.");
    Container c = getContentPane();

    sym = new Sym4();

    pane1 = new JPanel();
    pane2 = new JPanel();

    //c.setLayout(new FlowLayout());
    pane1.setLayout(new FlowLayout());
    pane2.setLayout(new GridLayout(4,4));

    bh = new ButtonHandler();

    for (int i = 0; i < buttons.length; i++)
        for (int j = 0; j < buttons[i].length; j++)
        {
            buttons[i][j] = new JButton( "" );
            pane2.add(buttons[i][j]);
            buttons[i][j].addActionListener(bh);
        }

    ButtonGroup btngrp = new ButtonGroup();
    for (int i = 0; i < RButtons.length; i++)
    {
        RButtons[i] = new JRadioButton(i+1 + "");
        btngrp.add(RButtons[i]);
        pane1.add(RButtons[i]);
        RButtons[i].addActionListener(bh);
    }

    c.add(pane1, BorderLayout.NORTH);
    c.add(pane2, BorderLayout.CENTER);

    setSize(500, 500);
    setVisible(true);

    //JOptionPane.showMessageDialog(null, "Click an open space to place chosen number there...");
}

private class ButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent ae)
    {
这就是我想知道如何填充单选按钮,然后单击网格上的任意位置,将网格方形文本设置为填充单选按钮文本

        for (int i = 0; i < RButtons.length; i++)
            if (ae.getSource() == RButtons[i])
            {
                for (int k = 0; k < buttons.length; k++)
                    for (int j = 0; j < buttons[k].length; j++) 
                        if (ae.getSource() == buttons[k][j])
                            buttons[k][j].setText(RButtons[i].getText());
            }
    }
}

public static void main (String [] args)
{
    Sym4GUI s1 = new Sym4GUI();
    s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


如果您需要更多说明,请告诉我。

您应该使用ae.getSource并将其与单选按钮进行比较,因为这是JButton事件的来源。相反,您应该在JRadioButton的数组中循环检查选择了哪一个,设置触发事件的JButton,该事件可以使用ae.GetSource发现您是一个救生员先生或女士!谢谢