Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.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 单选按钮不执行此操作';s动作_Java_Swing_Jradiobutton - Fatal编程技术网

Java 单选按钮不执行此操作';s动作

Java 单选按钮不执行此操作';s动作,java,swing,jradiobutton,Java,Swing,Jradiobutton,每次我点击in2cm的单选按钮并尝试转换时,当我点击转换按钮时没有显示任何内容。如果可能的话,还可以告诉我如何使用列表框,所以我将把它放在一个列表框中,使GUI更易于使用 ButtonGroup convertgroup=new ButtonGroup(); jr=new JRadioButton[10]; jr[0]=new JRadioButton("Inch to Cm"); l1 = new JTextField(25); l2 = new JTextField(25); leng

每次我点击in2cm的单选按钮并尝试转换时,当我点击转换按钮时没有显示任何内容。如果可能的话,还可以告诉我如何使用列表框,所以我将把它放在一个列表框中,使GUI更易于使用

ButtonGroup convertgroup=new ButtonGroup();
jr=new JRadioButton[10];

jr[0]=new JRadioButton("Inch to Cm");

l1 = new JTextField(25);
l2 = new JTextField(25);

length1 = new JLabel("ft", SwingConstants.LEFT);
length2 = new JLabel("cm", SwingConstants.LEFT);

convertgroup.add(jr[0]);

jr[1]=new JRadioButton("Meter to Feet");
length = new JLabel("m    = ", SwingConstants.LEFT);
l1 = new JTextField(25);
l2 = new JTextField(25);

length1 = new JLabel("ft", SwingConstants.LEFT);
length2 = new JLabel("cm", SwingConstants.LEFT);

convertgroup.add(jr[1]);

jr[2]=new JRadioButton("Mile to Km");
Mi2Km = new JTextField(25);
Km2Mi = new JTextField(25);
length3 = new JLabel("Mile", SwingConstants.LEFT);
length4 = new JLabel("Km", SwingConstants.LEFT);

convertgroup.add(jr[2]);

jr[3]=new JRadioButton("Nautical Mile to Km");
Nmi2Km= new JTextField(25);
Km2Nmi= new JTextField(25);

jr[4]=new JRadioButton("League to Km");
Le2Km = new JTextField(25);
Km2Le = new JTextField(25);

submit = new JButton("Convert");
clear = new JButton("Clear");
quit = new JButton("Quit");

submit.addActionListener(this);
clear.addActionListener(this);
quit.addActionListener(this);

converterPanel.add(jr[0]);
converterPanel.add(jr[2]);
converterPanel.add(jr[1]);
converterPanel.add(l1);
converterPanel.add(length);
converterPanel.add(l2);
converterPanel.add(length1);
converterPanel.add(jr[3]);
converterPanel.add(jr[4]);

converterPanel.add(submit);
converterPanel.add(clear);
converterPanel.add(quit);

length.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
以下是执行的操作:

public void actionPerformed(ActionEvent event) {

    if (event.getSource() == quit) {
        System.exit(0);
    }

    if (event.getSource() == clear) {
        l1.setText("");
        l2.setText("");
    }

    if (event.getSource()== submit) {

        if(!jr[0].isSelected() && !jr[1].isSelected() && !jr[2].isSelected())
            JOptionPane.showMessageDialog(converterFrame,
                    "No RadioButton has been Selected ", "Warning",
                    JOptionPane.WARNING_MESSAGE);

        if(jr[0].isSelected())
            in2cm();

        if(jr[1].isSelected()){
            lengthConvert();

            return;
        }
    }
}

我已经发现并修复了该问题,但如何在每次选择jr[0]时更改jlabel?因为jr[1]中的jlabel是米和英尺,而jr[0]中我需要以米和厘米为单位。

请向我们展示
actionPerformed
方法。我添加了actionPerformed方法,我看不到任何地方您将
ActionListener
注册到任何收音机按钮上公共类转换器实现ActionListener{这一个?另外,您可以添加似乎不起作用的方法吗?
in2cm()
lengthConvert()