java - 我的单选按钮代码出了什么问题?

标签 java swing button radio-button

第 36 行的“public RadioButton () {”附近继续出现错误。另外,我发现我已经尝试了所有我能想到的方法来解决该问题,现在正在转向这里的精彩社区。如果有人可以解释我出错的地方以及我的代码是否存在任何其他明显的错误。

感谢您的所有建议(抱歉代码有点长)

顺便说一句,这是为了解决化学方程式,所以如果任何化学家发现错误,也请随时提供帮助!

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.JFrame;


public class ProjectMain extends JPanel {
    //This converts Grams of one element to Grams of another element 
    public static double convertGramsToGrams(double element1, double element2, double amount, double molarRatio){
        double stepOne = amount * element1;
        double stepTwo = stepOne * molarRatio; 
        double stepThree = stepTwo * element2; 
        return stepThree;
    } 

    public static double percentYield(double actualYield, double theoreticalYield){
        double percentYield;
        percentYield = (actualYield / theoreticalYield) * 100; 
        return percentYield; 
    }
    //This converts from moles to grams of any element
    public static double convertMolesToGram(double element, double amount, double molarRatio){
        double StepOne = amount * molarRatio;
        double StepTwo = StepOne * element; 
        return StepTwo; 
    }
    //Radio Button stuff
    static JFrame frame;

    JLabel pic;
    RadioListener myListener = null;
    protected JRadioButton PercentRatio;
    protected JRadioButton MolesToGrams; 
    protected JRadioButton GramsToGrams;
    public RadioButton() {

        PercentRatio = new JRadioButton("Percent Ratio");
        PercentRatio.setMnemonic(KeyEvent.VK_N);
        PercentRatio.setActionCommand("Percent Ratio");

        MolesToGrams = new JRadioButton("Moles To Grams");
        MolesToGrams.setMnemonic(KeyEvent.VK_A);
        MolesToGrams.setActionCommand("Moles To Grams");

        GramsToGrams = new JRadioButton("Grams To Moles");
        GramsToGrams.setMnemonic(KeyEvent.VK_S);
        GramsToGrams.setActionCommand("Grams To Moles");

        ButtonGroup group = new ButtonGroup();
        group.add(PercentRatio);
        group.add(MolesToGrams);
        group.add(GramsToGrams);

        myListener = new RadioListener();
        PercentRatio.addActionListener(myListener);
        MolesToGrams.addActionListener(myListener);
        GramsToGrams.addActionListener(myListener);


        pic = new JLabel(new ImageIcon(""+"numbers" + ".jpg"));         //Set the Default Image

            pic.setPreferredSize(new Dimension(177, 122)); 

            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 1));
            panel.add(PercentRatio);
            panel.add(MolesToGrams);
            panel.add(GramsToGrams);

            setLayout(new BorderLayout());
            add(panel, BorderLayout.WEST);
            add(pic, BorderLayout.CENTER);
            setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
       }
       class RadioListener implements ActionListener { 
           public void actionPerformed(ActionEvent e) {
               pic.setIcon(new ImageIcon(""+e.getActionCommand() + ".jpg"));
           }
       }


       public void program(){
           if (PercentRatio.isSelected()){
               //Gets user input
                String actualYield = JOptionPane.showInputDialog("What was the yield that you found?");
                String theoYield = JOptionPane.showInputDialog("What is the yield that the stoichiometry suggests that you should have?");
                //Changes user input to a double from a string
                double newActualYield = Double.parseDouble(actualYield);
                double newTheoYield = Double.parseDouble(theoYield);
                //Gives back the answer
                JOptionPane.showMessageDialog(null, "Your percent yield is " + percentYield(newActualYield, newTheoYield), "title", JOptionPane.PLAIN_MESSAGE);

           }
           if (GramsToGrams.isSelected()){
                //Gets user input
                String amount = JOptionPane.showInputDialog("Enter in the amount in grams that you're starting with");
                String firstElement = JOptionPane.showInputDialog("Enter in the atomic mass of the that element/compound");
                String secondElement = JOptionPane.showInputDialog("Enter in the atomic mass of the second element/compound");
                String molarRatio = JOptionPane.showInputDialog("Now enter in the molar ratio (if it's a fraction, enter in the decimal version)");
                //Changes user input to a double from a string
                double newAmount = Double.parseDouble(amount);
                double newFirstElement = Double.parseDouble(firstElement);
                double newSecondElement  = Double.parseDouble(secondElement);
                double newMolarRatio = Double.parseDouble(molarRatio);
                //Gives back the answer
                JOptionPane.showMessageDialog(null,"The amount you end with in grams is" +  convertGramsToGrams(newFirstElement, newSecondElement, newAmount, newMolarRatio), "title", JOptionPane.PLAIN_MESSAGE);
           }
           if (MolesToGrams.isSelected()){
               //Gets user input
               String Moles = JOptionPane.showInputDialog("How many moles are you starting with");
               String MolarRatio = JOptionPane.showInputDialog("What is your molar ratio? (if it's a fraction, enter in the decimal version)"); 
               String Grams = JOptionPane.showInputDialog("What is the amount in grams that you are multiplying by?");
               //Changes user input to a double from a string
               double newMoles = Double.parseDouble(Moles);
               double newMolarRatio = Double.parseDouble(MolarRatio);
               double newGrams = Double.parseDouble(Grams); 
               //Gives back the answer
               JOptionPane.showMessageDialog(null, "The amount you end with in grams is " + convertMolesToGram(newGrams, newMoles, newMolarRatio), "title", JOptionPane.PLAIN_MESSAGE); 
           }

       }

}

最佳答案

public RadioButton() {

这是构造函数的语法。此构造函数不在名为 RadioButton 的类内,因此编译器认为它是一个名为 RadioButton 的方法,缺少返回类型。

我相信你想要这个。

public ProjectMain() {

关于java - 我的单选按钮代码出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192494/

相关文章:

java - 在 bufferedimage Java 上添加和绘制

java - 从一个屏幕( Activity )导航到另一个 Activity 时,我们如何获取和设置相同的 Toggle 值?

java - JPA单个查询获取嵌套集合

java - 如何独立运行一个java程序和另一个java程序?

java - 球从鼠标拖动绘制的线上弹起

jquery - 将按钮附加到可折叠内容中

java - 如何使用Java中子类的Class实例对象将对象转换为其自身的子类?

尝试在 netbeans 上运行应用程序时出现 java.lang.NullPointerException 错误

javascript - 如何指定元素自己的内部 HTML?

c# - WPF 取消按钮