开发者

My program is lacking a step! Please see if you can find what I am missing?

开发者 https://www.devze.com 2023-02-20 09:01 出处:网络
When this program is ran, it is supposed to list the numbers after I enter them but it only enters the first I put of 5, what am I missing

When this program is ran, it is supposed to list the numbers after I enter them but it only enters the first I put of 5, what am I missing

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);
        }

    }
}


There are several issues with your code. I am not sure if can list all of them, first is your main method should take a String array argument if you wish to run the code as a Java application i.e.

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

Second, you don't the while and for loop in your actionPerformed(ActionEvent e) method.

The actionPerformed(ActionEvent e) method in DisplayNum class displays only the first element of the array. Maybe you want to concatenate all 5 numbers and display? Anyway, here is the code with the modifications:

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());
        }

    }
}


In your DisplayNum class, you're only displaying array[0]. If you want to display multiple, you'll need to use a loop of some sort:

for(int i = 0; i < array.length; i++) {
    // Do stuff with array[i] instead of array[0] here
}


Your array size is set to 5, so only five elements can exist.


String intInput = String.valueOf(array[0]);

in method action performed, you have passed array[o], I think you should use array.


lass DisplaYEnum method is incorrect. You are not creating your displayArray correctly.

see below one.

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);
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消