Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的程序缺少一个步骤!请看看你能不能找到我所缺少的?_Java - Fatal编程技术网

Java 我的程序缺少一个步骤!请看看你能不能找到我所缺少的?

Java 我的程序缺少一个步骤!请看看你能不能找到我所缺少的?,java,Java,当这个程序运行时,它应该在我输入数字后列出数字,但它只输入我输入的第一个数字(5),我缺少什么 public class Numbers extends JFrame { private JTextField textField; private JTextArea textArea, displayArea, finalArea; private JPanel controlPanel, bottomPanel, southPan

当这个程序运行时,它应该在我输入数字后列出数字,但它只输入我输入的第一个数字(5),我缺少什么

public class Numbers extends JFrame
    {

         private JTextField textField;
         private JTextArea textArea, displayArea, finalArea;
         private JPanel controlPanel, bottomPanel, southPanel, displayPanel, displayFinal;
         private JButton enter, finalNumbers;
         private String input; 
         private int intInput;
         private int[] array = new int[5];

         private int entered = 0;

    public static void main(){
        Numbers myFrame = new Numbers();
        myFrame.setSize(600,600);
        myFrame.setTitle("Numbers between 10-100 by Daniel Bendlin");
        myFrame.createGUI(); 
        myFrame.pack();
        myFrame.setVisible(true);

    }


    public void createGUI()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new BorderLayout());

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));


        southPanel = new JPanel();
        displayPanel = new JPanel();

        textArea = new JTextArea("Please enter 5 numbers between 10-100");
        textArea.setBackground(Color.white);
        displayArea = new JTextArea("");
        finalArea = new JTextArea("");

        textField = new JTextField(2);
        textField.setBackground(Color.white);

        enter = new JButton("Enter");
        enter.setBackground(Color.GREEN);
        enter.addActionListener(new EnterNum());
        finalNumbers = new JButton("Display Final Numbers");
        finalNumbers.addActionListener(new DisplayNum());

        controlPanel.add(textArea);
        southPanel.add(textField);
        southPanel.add(enter);
        displayPanel.add(displayArea);
        displayPanel.add(finalNumbers);
        displayPanel.add(finalArea);

        window.add(controlPanel,BorderLayout.NORTH);              
        window.add(southPanel,BorderLayout.CENTER);
        window.add(displayPanel,BorderLayout.SOUTH);
        textField.requestFocusInWindow();

    }
private class EnterNum implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {  

            input = textField.getText();
            intInput = Integer.parseInt(input);

            while (entered < array.length){

                try{

                    if((intInput >= 10) && (intInput <= 100)){

                        for(int i = 0; i < array.length; i++){

                            array[i] = intInput;
                            entered = entered + 1;
                            textField.setText("");
                            displayArea.setText("Entered number(s)..." + array[i]);
                         }                  
                        }else{
                            displayArea.setText("Input numbers that range between 10 and 100");
                        }
            }catch (NumberFormatException x){displayArea.setText("\"" + textField.getText() + "\" is not a legal number.");

            textField.selectAll();
            textField.requestFocus();
                                        }

           } 
       }
    }
    private class DisplayNum implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String intInput = String.valueOf(array[0]);
            finalArea.setText("\n" + intInput);
        }

    }
}
公共类编号扩展JFrame
{
私有JTextField textField;
私有JTextArea文本区、显示区、最终区;
专用JPanel控制面板、底部面板、南面板、显示面板、显示最终面板;
私有JButton enter,finalnumber;
私有字符串输入;
私人内部输入;
私有int[]数组=新int[5];
输入的私有int=0;
公共静态void main(){
数字myFrame=新数字();
myFrame.setSize(600600);
myFrame.setTitle(“Daniel Bendlin创作的10-100之间的数字”);
myFrame.createGUI();
myFrame.pack();
myFrame.setVisible(true);
}
public void createGUI()
{
setDefaultCloseOperation(关闭时退出);
容器窗口=getContentPane();
setLayout(新的BorderLayout());
控制面板=新的JPanel();
setLayout(新的FlowLayout());
底部面板=新的JPanel();
底部面板.setLayout(新的FlowLayout(FlowLayout.CENTER));
southPanel=newjpanel();
displayPanel=newjpanel();
textArea=新的JTextArea(“请输入10-100之间的5个数字”);
textArea.setBackground(颜色:白色);
displayArea=新的JTextArea(“”);
最终面积=新的JTextArea(“”);
textField=新的JTextField(2);
textField.setBackground(颜色:白色);
回车=新的JButton(“回车”);
输入.setBackground(颜色.绿色);
enter.addActionListener(new EnterNum());
finalNumber=新的JButton(“显示最终数字”);
addActionListener(新的DisplayNum());
控制面板。添加(文本区域);
添加(文本字段);
southPanel.add(输入);
添加(显示区域);
displayPanel.add(最终编号);
displayPanel.add(finalArea);
添加(控制面板,BorderLayout.NORTH);
添加(southPanel,BorderLayout.CENTER);
添加(displayPanel,BorderLayout.SOUTH);
textField.requestFocusInWindow();
}
私有类EnterNum实现ActionListener
{
已执行的公共无效操作(操作事件e)
{  
input=textField.getText();
intInput=Integer.parseInt(输入);
while(输入如果在DisplayNum类中((intInput>=10)&(intInput),则只显示
数组[0]
。如果要显示多个,则需要使用某种循环:

for(int i = 0; i < array.length; i++) {
    // Do stuff with array[i] instead of array[0] here
}
for(int i=0;i
您的数组大小设置为5,因此只能存在五个元素。

String intInput=String.valueOf(数组[0])


在执行的方法操作中,您已经传递了数组[o],我认为您应该使用数组。

lass DisplaYEnum方法不正确。您没有正确创建displayArray

见下文

private class DisplayNum implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String intInput = "";
        for (int i = 0; i < array.length; i++) {
            intInput = intInput.concat(" " + String.valueOf(array[i]));
        }

        finalArea.setText("\n" + intInput);
    }

}
私有类DisplayNum实现ActionListener{
已执行的公共无效操作(操作事件e){
字符串intInput=“”;
for(int i=0;i
您的代码有几个问题。我不确定是否可以列出所有问题,首先,如果您希望将代码作为Java应用程序运行,您的主方法应该使用字符串数组参数,即

public static void main(String[] args){
}
其次,在
actionPerformed(ActionEvent e)
方法中,您不需要
while
for
循环

DisplayNum类中的actionPerformed(ActionEvent e)方法仅显示数组的第一个元素。也许您要连接所有5个数字并显示?无论如何,下面是经过修改的代码:

public class Numbers extends JFrame {

    private JTextField textField;
    private JTextArea textArea, displayArea, finalArea;
    private JPanel controlPanel, bottomPanel, southPanel, displayPanel,
            displayFinal;
    private JButton enter, finalNumbers;
    private String input;
    private int intInput;
    private int[] array = new int[5];

    private int entered = 0;

    public static void main(String[] args) {
        Numbers myFrame = new Numbers();
        myFrame.setSize(600, 600);
        myFrame.setTitle("Numbers between 10-100 by Daniel Bendlin");
        myFrame.createGUI();
        myFrame.pack();
        myFrame.setVisible(true);

    }

    public void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new BorderLayout());

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        southPanel = new JPanel();
        displayPanel = new JPanel();

        textArea = new JTextArea("Please enter 5 numbers between 10-100");
        textArea.setBackground(Color.white);
        displayArea = new JTextArea("");
        finalArea = new JTextArea("");

        textField = new JTextField(2);
        textField.setBackground(Color.white);

        enter = new JButton("Enter");
        enter.setBackground(Color.GREEN);
        enter.addActionListener(new EnterNum());
        finalNumbers = new JButton("Display Final Numbers");
        finalNumbers.addActionListener(new DisplayNum());

        controlPanel.add(textArea);
        southPanel.add(textField);
        southPanel.add(enter);
        displayPanel.add(displayArea);
        displayPanel.add(finalNumbers);
        displayPanel.add(finalArea);

        window.add(controlPanel, BorderLayout.NORTH);
        window.add(southPanel, BorderLayout.CENTER);
        window.add(displayPanel, BorderLayout.SOUTH);
        textField.requestFocusInWindow();

    }

    private class EnterNum implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            input = textField.getText();
            intInput = Integer.parseInt(input);

            if (entered < array.length) {

                try {

                    if ((intInput >= 10) && (intInput <= 100)) {
                        array[entered] = intInput;  
                        textField.setText("");
                        displayArea.setText("Entered number(s)..."
                                + array[entered]);
                        entered = entered + 1;
                } else {
                        displayArea
                                .setText("Input numbers that range between 10 and 100");
                    }
                } catch (NumberFormatException x) {
                    displayArea.setText("\"" + textField.getText()
                            + "\" is not a legal number.");

                    textField.selectAll();
                    textField.requestFocus();
                }

            }
        }
    }

    private class DisplayNum implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; i++) {
                sb.append(String.valueOf(array[i]));
                if(i < array.length){
                    sb.append(",");
                }
            }
            finalArea.setText("\n" + sb.toString());
        }

    }
}
公共类编号扩展JFrame{
私有JTextField textField;
私有JTextArea文本区、显示区、最终区;
专用JPanel控制面板、底部面板、南面板、显示面板、,
显示最终结果;
私有JButton enter,finalnumber;
私有字符串输入;
私人内部输入;
私有int[]数组=新int[5];
输入的私有int=0;
公共静态void main(字符串[]args){
数字myFrame=新数字();
myFrame.setSize(600600);
myFrame.setTitle(“Daniel Bendlin创作的10-100之间的数字”);
myFrame.createGUI();
myFrame.pack();
myFrame.setVisible(true);
}
public void createGUI(){
setDefaultCloseOperation(关闭时退出);
容器窗口=getContentPane();
setLayout(新的BorderLayout());
控制面板=新的JPanel();
setLayout(新的FlowLayout());
底部面板=新的JPanel();
底部面板.setLayout(新的FlowLayout(FlowLayout.CENTER));
southPanel=newjpanel();
displayPanel=newjpanel();
textArea=新的JTextArea(“请输入10-100之间的5个数字”);
textArea.setBackground(颜色:白色);
displayArea=新的JTextArea(“”);
最终面积=新的JTextArea(“”);
textField=新的JTextField(2);
textField.setBackground(颜色:白色);
回车=新的JButton(“回车”);
输入.setBackground(颜色.绿色);
enter.addActionListener(new EnterNum());
finalNumber=新的JButton(“显示最终数字”);
addActionListener(新的DisplayNum());
控制面板。添加(文本区域);
添加(文本字段);
southPanel.add(输入);
添加(显示区域);
displayPanel.add(最终编号);
displayPanel.add(finalArea);
window.add