What is the matter with my radio button code?

时间:2015-12-10 02:04:34

标签: java swing button radio-button

An error continues to appear near the "public RadioButton () {" on line 36. Also, I see I have tried everything that I could think of to solve it and am now turning to the wonderful community here. If someone can explain where I went wrong and if there are any other glaring errors with my code.

Thanks for all your advice (Sorry the code is a bit long)

Btw, this is to solve chemistry equations, so if any chemists out there see errors, feel free to help too!

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); 
           }

       }

}

1 个答案:

答案 0 :(得分:1)

public RadioButton() {

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

我相信你想要这个。

public ProjectMain() {