如何从多个JTextField中检索数据

时间:2015-05-08 16:13:34

标签: jtextfield

我们正在netbeans中创建一个项目,需要从3个JTextFields中读取数据,以便我们在方程中使用它来解决我们已经完成的部分的问题:

package combustionofgaseousfuels;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CombustionOfGaseousFuels extends JFrame 
{
    private JTextField C;
    private JTextField H;
    private JTextField Excess;
    private JTextField Answer;
    private JTextField Theo;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private ButtonGroup rad1;
    private JRadioButton ORSAT;
    private JRadioButton WET;
    private JRadioButton BOTH;
    private JRadioButton c4;
    private JRadioButton c5;
    
    public CombustionOfGaseousFuels()
    { 
        super("Flue Gas Analyzer");
        
        ORSAT= new JRadioButton("ORSAT Analysis",false);
        WET= new JRadioButton("Wet Analysis",false);
        BOTH= new JRadioButton("Both",false);
        rad1= new ButtonGroup();
        rad1.add(ORSAT);
        rad1.add(WET);
        rad1.add(BOTH);
        
        panel1=new JPanel();
        panel1.setLayout(new GridLayout(1,3));
        panel1.add(ORSAT);
        panel1.add(WET);
        panel1.add(BOTH);
        
        label1= new JLabel("Enter the Number of Carbon and Hydrogen in Your Fuel:");
        label2= new JLabel("C:");
        label3= new JLabel("H:");
        label4= new JLabel("How Much is the %Excess Air?");
        label5=new JLabel("Your Flue Gas Analysis is:");
        label6=new JLabel("The Theoretical O2 is:");
        label7=new JLabel("What Flue Gas Analysis Do You Want?");
                
        C=new JTextField(5);
        C.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event)
                    {
                        int carbon;
                        carbon= Integer.parseInt( ( (JTextField)
                        event.getSource()).getText());
                        
                         H=new JTextField(5);
                         H.addActionListener(
                            new ActionListener(){
                                @Override
                                public void actionPerformed(ActionEvent event)
                                {
                                    int hydrogen;
                                    hydrogen=Integer.parseInt(((JTextField)
                                            event.getSource()).getText());
                                    
                                      Excess= new JTextField(5);
                                      Excess.addActionListener(
                                        new ActionListener(){
                                            @Override
                                            public void actionPerformed(ActionEvent event)
                                            {
                                                int excess;
                                                excess=Integer.parseInt(((JTextField)
                                                        event.getSource()).getText());
                                                
                                                {
                                                    if (ORSAT.isSelected()){
                                                        //double c = Integer.parseInt( carbon );
                                                        //double h = Integer.parseInt( hydrogen );
                                                        //double x = Integer.parseInt( excess );
        
                                                        double tc = (carbon*1);
                                                        double th = (hydrogen*1);
                                                        double tx = (excess/100);
        
                                                        double theo = tc + (th/4);
                                                        double xs = tx * theo;
                                                        double osupplied = (1+ tx)* theo;
                                                        double n = 3.761904762 * osupplied;
      
        
                                                        double total = (tc) + xs + n;
                                                        double carbondioxide = ((tc)/ total)*100;
                                                        double nitrogen = (n/ total)*100;
                                                        double oxygen = (xs/total)*100;
                                                    
                                                        Answer.setText(String.valueOf(total));
                                                    }
                                                }
                                            }
                                        });
                                }
                            });
                    }
                });
        
        
        
       
      
        
        
        
        Answer=new JTextField(10);
        Answer.setEditable(false);
        
        Theo=new JTextField(10);
        Theo.setEditable(false);
        
        Container project=getContentPane();
        project.setLayout(new GridLayout(15,1));
        project.add(label1);
        project.add(label2);
        project.add(C);
        project.add(label3);
        project.add(H);
        project.add(label4);
        project.add(Excess);
        project.add(label7);
        project.add(panel1);
        project.add(label5);
        project.add(Answer);
        project.add(label6);
        project.add(Theo);
        
        
        setSize (400,300);
        setVisible(true);
    }
    
    public static void main(String[] args) 
    {
        CombustionOfGaseousFuels analyzer=new CombustionOfGaseousFuels();
        analyzer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        
        
    }
    
}

代码中没有错误,但是当我们尝试运行它时,它会显示一些错误,。请帮助我们完成项目。

2 个答案:

答案 0 :(得分:0)

public class CombustionOfGaseousFuels extends JFrame 
{
    private JTextField C;
    private JTextField H;
    private JTextField Excess;
    private JTextField Answer;
    private JTextField Theo;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private ButtonGroup rad1;
    private JRadioButton ORSAT;
    private JRadioButton WET;
    private JRadioButton BOTH;
    private JRadioButton c4;
    private JRadioButton c5;

    public CombustionOfGaseousFuels()
    { 
        super("Flue Gas Analyzer");

        ORSAT= new JRadioButton("ORSAT Analysis",false);
        WET= new JRadioButton("Wet Analysis",false);
        BOTH= new JRadioButton("Both",false);
        rad1= new ButtonGroup();
        rad1.add(ORSAT);
        rad1.add(WET);
        rad1.add(BOTH);

        panel1=new JPanel();
        panel1.setLayout(new GridLayout(1,3));
        panel1.add(ORSAT);
        panel1.add(WET);
        panel1.add(BOTH);

        label1= new JLabel("Enter the Number of Carbon and Hydrogen in Your Fuel:");
        label2= new JLabel("C:");
        label3= new JLabel("H:");
        label4= new JLabel("How Much is the %Excess Air?");
        label5=new JLabel("Your Flue Gas Analysis is:");
        label6=new JLabel("The Theoretical O2 is:");
        label7=new JLabel("What Flue Gas Analysis Do You Want?");

        C=new JTextField(5);
        H=new JTextField(5);
        Excess= new JTextField(5);
        C.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event)
                    {
                        final int  carbon;
                        carbon= Integer.parseInt( ( (JTextField)
                        event.getSource()).getText());


                         H.addActionListener(
                            new ActionListener(){
                                @Override
                                public void actionPerformed(ActionEvent event)
                                {
                                    final int hydrogen;
                                    hydrogen=Integer.parseInt(((JTextField)
                                            event.getSource()).getText());


                                      Excess.addActionListener(
                                        new ActionListener(){
                                            @Override
                                            public void actionPerformed(ActionEvent event)
                                            {
                                                int excess;
                                                excess=Integer.parseInt(((JTextField)
                                                        event.getSource()).getText());

                                                {
                                                    if (ORSAT.isSelected()){
                                                        //double c = Integer.parseInt( carbon );
                                                        //double h = Integer.parseInt( hydrogen );
                                                        //double x = Integer.parseInt( excess );

                                                        double tc = (carbon*1);
                                                        double th = (hydrogen*1);
                                                        double tx = (excess/100);

                                                        double theo = tc + (th/4);
                                                        double xs = tx * theo;
                                                        double osupplied = (1+ tx)* theo;
                                                        double n = 3.761904762 * osupplied;


                                                        double total = (tc) + xs + n;
                                                        double carbondioxide = ((tc)/ total)*100;
                                                        double nitrogen = (n/ total)*100;
                                                        double oxygen = (xs/total)*100;

                                                        Answer.setText(String.valueOf(total));
                                                    }
                                                }
                                            }
                                        });
                                }
                            });
                    }
                });








        Answer=new JTextField(10);
        Answer.setEditable(false);

        Theo=new JTextField(10);
        Theo.setEditable(false);

        Container project=getContentPane();
        project.setLayout(new GridLayout(15,1));
        project.add(label1);
        project.add(label2);
        project.add(C);
        project.add(label3);
        project.add(H);
        project.add(label4);
        project.add(Excess);
        project.add(label7);
        project.add(panel1);
        project.add(label5);
        project.add(Answer);
        project.add(label6);
        project.add(Theo);


        setSize (400,300);
        setVisible(true);
    }

    public static void main(String[] args) 
    {
        CombustionOfGaseousFuels analyzer=new CombustionOfGaseousFuels();
        analyzer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }

}

答案 1 :(得分:0)

您应该在C.addActionListener之外定义H和Excess。试试看 H =新的JTextField(5);
Excess = new JTextField(5);
 就在 C = new JTextField(5);