我创建了一个构建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();
}
}
}
提前谢谢!
答案 0 :(得分:0)
错误是guiClassConversions中的NumberFormatException,在此行:
dT1 = Double.parseDouble(text1.getText());
实例化类时,text1包含一个空String。无法将空String解析为double。
答案 1 :(得分:0)
dTa1 =(Double)text1.getText();
或
Object o = text1.getText() ;
dTa1 = (Double)o ;