0

GUIを構築するJavaコードを作成しました。プログラム全体は、私が作成したコンバータ ウィンドウ以外でも動作します。エラーは空の文字列を示しています。私は gui プログラムが初めてで、このプログラムの何が問題なのか知りたいです。理由の説明もいいでしょう。申し訳ありませんが、コードがたくさんありますが、プログラムの実行中にエラーが表示されます。エラーの行は表示されません。最初からやり直すべきですか?もしそうなら、誰かが私に良い出発点を与えることができますか? よろしくお願いします!

    package thisprogramisforfun.guiClasses;

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import run.listenerOp;

/**
 * @version date
 * @author my name
 */
public class guiClassConversions extends JFrame{

    private JPanel panel;
    private JTextField text1;
    private JComboBox combo1;
    private JComboBox combo2;
    private JTextField tAnswer;
    private final int WINDOW_WIDTH = 400;
    private final int WINDOW_HEIGHT = 250;
    private static String t1 = "";
    private static String t2 = "";
    private static String tA = "";
    public static String over[] = {"inch", "feet", "centimeter", "second", 
        "minute", "hour"};
    public static String under[] = {"inch", "feet", "centimeter", "second", 
        "minute", "hour"};
    private  static double dT1;
    private static double dT2;
    private static double dTA;
    private static int ic1;
    private static int ic2;

    private String cb1S0 = over[0];
    private String cb1S1 = over[1];
    private String cb1S2 = over[2];
    private String cb1S3 = over[3];
    private String cb1S4 = over[4];
    private String cb1S5 = over[5];

    private String cb2S0 = under[0];
    private String cb2S1 = under[1];
    private String cb2S2 = under[2];
    private String cb2S3 = under[3];
    private String cb2S4 = under[4];
    private String cb2S5 = under[5];

    public guiClassConversions(){
        super("Coversions");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel(dT1);
        add(panel);
        setVisible(true);
    }

    private void buildPanel(double dT11){
        panel = new JPanel();
        text1 = new JTextField(t1, 10);
        combo1 = new JComboBox(over);
        combo2 = new JComboBox(under);
        tAnswer = new JTextField(tA, 10);
        tAnswer.setEditable(false);

        String cb1Ans = over[ic1];
        String cb2Ans = under[ic2];

        listenerOp op = new listenerOp();
        dT1 = Double.parseDouble(text1.getText());
        tA = op.conv(cb1Ans, cb2Ans, dT1);

        panel.setLayout(new FlowLayout());
        panel.add(text1);
        panel.add(combo1);
        panel.add(combo2);
        panel.add(tAnswer);

        combo1.addItemListener(new ItemListener(){
            @Override
            public void itemStateChanged(ItemEvent e){
               if(e.getStateChange() == ItemEvent.SELECTED){
                 ic1 = combo1.getSelectedIndex();      
               }
               String cb1Ans = over[ic1];
               String cb2Ans = under[ic2];

               listenerOp op = new listenerOp();
               dT1 = Double.parseDouble(text1.getText());
               tA = op.conv(cb1Ans, cb2Ans, dT1);

            }
        });
        combo2.addItemListener(new ItemListener(){
            @Override
            public void itemStateChanged(ItemEvent e){

                if(e.getStateChange() == ItemEvent.SELECTED){
                     ic2 = combo1.getSelectedIndex();
                }
                String cb1Ans = over[ic1];
                String cb2Ans = under[ic2];

                listenerOp op = new listenerOp();
                dT1 = Double.parseDouble(text1.getText());
                tA = op.conv(cb1Ans, cb2Ans, dT1);
            }
        });  

    }
}

listenerOp クラスは次のとおりです。

    package run;

/**
 * @version date
 * @author my name
 */
public class listenerOp {


    public String conv(String cb1, String cb2, double t1){
        DanielConversions DConv = new DanielConversions();
        String[] as = {"inch", "feet", "centimeter", "second", "minute", 
            "hour"};
        double dAns = 0;
        String sAns;

        while(cb1.equals(as[0])) { //while first list is inch
        if(cb2.equals(as[0])){
            dAns = t1;
        }else if(cb2.equals(as[1])){
            dAns = DConv.inToFeet(t1);
        }else if(cb2.equals(as[2])){
            dAns = DConv.inToCm(t1);
        }else
            sAns = "These cannot be converted";
            }

        while(cb1.equals(as[1])){ //while first list is feet
        if(cb2.equals(as[0])){
            dAns = DConv.feetToInch(t1);
        }else if(cb2.equals(as[1])){
            dAns = t1;
        }else if(cb2.equals(as[2])){
            dAns = DConv.feetToCm(t1);
        }else
            sAns = "These cannot be converted";
            }

        while(cb1.equals(as[2])){ //while first list is cm
        if(cb2.equals(as[0])){
            dAns = DConv.cmToIn(t1);
        }else if(cb2.equals(as[1])){
            dAns = DConv.cmToFeet(t1);
        }else if(cb2.equals(as[2])){
            dAns = t1;
        }else
            sAns = "These cannot be converted";
            }

        while(cb1.equals(as[3])){ //while first list is second
        if(cb2.equals(as[3])){
            dAns = t1;
        }else if(cb2.equals(as[4])){
            dAns = DConv.secondToMinute(t1);
        }else if(cb2.equals(as[5])){
            dAns = DConv.secondToHour(t1);
        }else
            sAns = "These cannot be converted";
            }

        while(cb1.equals(as[4])){ //while first list is minute
        if(cb2.equals(as[3])){
            dAns = DConv.minuteToSecond(t1);
        }else if(cb2.equals(as[4])){
            dAns = t1;
        }else if(cb2.equals(as[5])){
            dAns = DConv.minuteToHour(t1);
        }else
            sAns = "These cannot be converted";
            }

        while(cb1.equals(as[5])){ //while first list is hour
        if(cb2.equals(as[3])){
            dAns = DConv.hourToSecond(t1);
        }else if(cb2.equals(as[4])){
            dAns = DConv.hourToMinute(t1);
        }else if(cb2.equals(as[5])){
            dAns = t1;
        }else
            sAns = "These cannot be converted";
            }

        sAns = Double.toString(dAns);
        return sAns;
        }         
    }

ここにメインがあります:

    package thisprogramisforfun.guiClasses;

import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import run.DanielMath;

/**
 * @version date
 * @author my name
 */
public class guiClassMain extends JFrame{

    private JPanel panel;
    private JLabel label;
    private JButton dist;
    private JButton interest;
    private JButton compInterest;
    private JButton largerOrSmaller;
    private JButton findNumber;
    private JButton dens;
    private JButton percError;
    private JButton conv;
    private static String[] buttons = {"Distance", "Interest", 
        "Compound Interest", "Larger or Smaller", "Find Number", "Density", 
        "Percent Error", "Conversions"};
    private static String select = "Select to find ";
    private final int WINDOW_WIDTH = 350;
    private final int WINDOW_HEIGHT = 200;

    public guiClassMain(){
        setTitle("Daniel's Homework Helper");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);
    }

    private void buildPanel(){
        panel = new JPanel();
        label = new JLabel("Click on a button to perform one of the tasks: ");
        dist = new JButton(buttons[0]);
        interest = new JButton(buttons[1]);
        compInterest = new JButton(buttons[2]);
        largerOrSmaller = new JButton(buttons[3]);
        findNumber = new JButton(buttons[4]);
        dens = new JButton(buttons[5]);
        percError = new JButton(buttons[6]);
        conv = new JButton(buttons[7]);

        label.setForeground(Color.BLACK);
        panel.setBackground(Color.gray);
        dist.setToolTipText(select + buttons[0]);
        interest.setToolTipText(select + buttons[1]);
        compInterest.setToolTipText(select + buttons[2]);
        largerOrSmaller.setToolTipText(select + buttons[3]);
        findNumber.setToolTipText(select + buttons[4]);
        dens.setToolTipText(select + buttons[5]);
        percError.setToolTipText(select + buttons[6]);
        conv.setToolTipText(select + buttons[7]);

        panel.add(label);
        panel.add(dist);
        panel.add(interest);
        panel.add(compInterest);
        panel.add(largerOrSmaller);
        panel.add(findNumber);
        panel.add(dens);
        panel.add(percError);
        panel.add(conv);

        distButtonListener resp = new distButtonListener();
        dist.addActionListener(resp);
        interestButtonListener i = new interestButtonListener();
        interest.addActionListener(i);
        compInterestButtonListener ci = new compInterestButtonListener();
        compInterest.addActionListener(ci);
        largerOrSmallerButtonListener ls = new largerOrSmallerButtonListener();
        largerOrSmaller.addActionListener(ls);
        findNumbButtonListener fn = new findNumbButtonListener();
        findNumber.addActionListener(fn);
        densButtonListener d = new densButtonListener();
        dens.addActionListener(d);
        percErrorButtonListener pe = new percErrorButtonListener();
        percError.addActionListener(pe);
        convButtonListener c = new convButtonListener();
        conv.addActionListener(c);
    }

    private class distButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassDistance d = new guiClassDistance();
        }
    }

    private class interestButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassInterest i = new guiClassInterest();
        }
    }

    private class compInterestButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassCompInterest ci = new guiClassCompInterest();
        }
    }

    private class largerOrSmallerButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassSmallerOrLarger sOrL = new guiClassSmallerOrLarger();
        }
    }

    private class findNumbButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassFindNumber fn = new guiClassFindNumber();
        }
    }

    private class densButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassDensity dens = new guiClassDensity();
        }
    }

    private class percErrorButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassPercentError pe = new guiClassPercentError();
        }
    }

    private class convButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            guiClassConversions c = new guiClassConversions();
        }
    }

}

よろしくお願いします!

4

2 に答える 2

0

エラーは、次の行の guiClassConversions の NumberFormatException です。

dT1 = Double.parseDouble(text1.getText());

クラスがインスタンス化されると、text1 には空の文字列が含まれます。空の文字列を double に解析することはできません。

于 2013-11-06T03:08:59.627 に答える