0

我目前正在尝试从两个 JTextField 中读取值,通过递增 1 来查找值的范围,然后将值写入 Double 数组。但是我所做的方式似乎不起作用,我在谷歌/互联网上找不到解决方案。

到目前为止,这是我的代码:

package textfieldtodouble;

import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import javax.swing.*;

public class TextFieldToDouble {

JFrame frame = new JFrame();
JPanel panel = new JPanel();
static JTextField maxValue1 = new JTextField(20);
static JTextField maxValue2 = new JTextField(20);
JButton btn1 = new JButton("To Double Array");
double x[];
double value1;
double value2;

void buildTheGUI() {
    panel.add(maxValue1, BorderLayout.NORTH);
    panel.add(btn1, BorderLayout.SOUTH);
    panel.add(maxValue2, BorderLayout.SOUTH);
    btn1.addActionListener(new toDouble());
    frame.add(panel);
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

TextFieldToDouble() {
    buildTheGUI();
}

public static void main(String[] args) {
    new TextFieldToDouble();
}

class toDouble implements ActionListener {

    public void actionPerformed(ActionEvent event) {
        value1 = Double.parseDouble(maxValue1.getText());
        value2 = Double.parseDouble(maxValue2.getText());

        for (int i = 0; i < value1; i++) {
            System.out.println(i);
            x[i] = value2 + i;
            value2 ++;

        }
    }
}
}

提前致谢。:)

4

2 回答 2

2

您尚未初始化数组x

value1 = Double.parseDouble(maxValue1.getText());
value2 = Double.parseDouble(maxValue2.getText());

int length = (int) value1;
x = new double[length];

for(int i = 0; i < length; i++)
{
    System.out.println(i);
    x[i] = value2 + i;
    value2 ++;
}
于 2013-03-05T19:15:55.320 回答
0

您的意思是 maxValue1 是最小值,而 maxValue2 是该范围内的最大值吗?如果是这样试试这个:

    public void actionPerformed(ActionEvent event) {
        value1 = Double.parseDouble(maxValue1.getText());
        value2 = Double.parseDouble(maxValue2.getText());

        int length = (int) value2 + 1 - (int) value1;
        x = new double[length];

        for(int i = 0; i < length; i++)
        {
            x[i] = value1 + i;
            System.out.println(x[i]);
        }   
于 2014-07-12T15:58:51.437 回答