我如何从文本区域删除输入的数字? [关闭]

最后发布: 2013-04-05 00:53:00


问题
/*
 * IntegerSumsView.java
 * This is a program that allows an individual to add a list of numbers, and choose whether 
 * they add all numbers, or only even or odd numbers
 */

   public class IntegerSumsView extends FrameView {

//

int position = 0;
int[] aryInteger = new int[10];

 public IntegerSumsView(SingleFrameApplication app) {
    super(app);

    initComponents();

    // status bar initialization - message timeout, idle icon and busy animation, etc
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
    messageTimer = new Timer(messageTimeout, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            statusMessageLabel.setText("");
        }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
    for (int i = 0; i < busyIcons.length; i++) {
        busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
        }
    });
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            String propertyName = evt.getPropertyName();
            if ("started".equals(propertyName)) {
                if (!busyIconTimer.isRunning()) {
                    statusAnimationLabel.setIcon(busyIcons[0]);
                    busyIconIndex = 0;
                    busyIconTimer.start();
                }
                progressBar.setVisible(true);
                progressBar.setIndeterminate(true);
            } else if ("done".equals(propertyName)) {
                busyIconTimer.stop();
                statusAnimationLabel.setIcon(idleIcon);
                progressBar.setVisible(false);
                progressBar.setValue(0);
            } else if ("message".equals(propertyName)) {
                String text = (String)(evt.getNewValue());
                statusMessageLabel.setText((text == null) ? "" : text);
                messageTimer.restart();
            } else if ("progress".equals(propertyName)) {
                int value = (Integer)(evt.getNewValue());
                progressBar.setVisible(true);
                progressBar.setIndeterminate(false);
                progressBar.setValue(value);
            }
        }
    });
}

@Action
public void showAboutBox() {
    if (aboutBox == null) {
        JFrame mainFrame = IntegerSumsApp.getApplication().getMainFrame();
        aboutBox = new IntegerSumsAboutBox(mainFrame);
        aboutBox.setLocationRelativeTo(mainFrame);
    }
    IntegerSumsApp.getApplication().show(aboutBox);
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    mainPanel = new javax.swing.JPanel();
    integerLabel = new javax.swing.JLabel();
    enterIntField = new javax.swing.JTextField();
    addIntButton = new javax.swing.JButton();
    sumAllButton = new javax.swing.JButton();
    userOutputField = new javax.swing.JTextField();
    sumEvenButton = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    userOutputArea = new javax.swing.JTextArea();
    sumOddButton = new javax.swing.JButton();
    removeIntButton = new javax.swing.JButton();
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(integersums.IntegerSumsApp.class).getContext().getResourceMap(IntegerSumsView.class);
    integerLabel.setText(resourceMap.getString("integerLabel.text")); // NOI18N
    integerLabel.setName("integerLabel"); // NOI18N

    enterIntField.setText(resourceMap.getString("enterIntField.text")); // NOI18N
    enterIntField.setName("enterIntField"); // NOI18N

    addIntButton.setText(resourceMap.getString("addIntButton.text")); // NOI18N
    addIntButton.setName("addIntButton"); // NOI18N
    addIntButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addIntButtonActionPerformed(evt);
        }
    });

    sumAllButton.setText(resourceMap.getString("sumAllButton.text")); // NOI18N
    sumAllButton.setName("sumAllButton"); // NOI18N
    sumAllButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            sumAllButtonActionPerformed(evt);
        }
    });

    userOutputField.setText(resourceMap.getString("userOutputField.text")); // NOI18N
    userOutputField.setName("userOutputField"); // NOI18N

    sumEvenButton.setText(resourceMap.getString("sumEvenButton.text")); // NOI18N
    sumEvenButton.setName("sumEvenButton"); // NOI18N
    sumEvenButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            sumEvenButtonActionPerformed(evt);
        }
    });

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    userOutputArea.setColumns(20);
    userOutputArea.setRows(5);
    userOutputArea.setName("userOutputArea"); // NOI18N
    jScrollPane1.setViewportView(userOutputArea);

    sumOddButton.setText(resourceMap.getString("sumOddButton.text")); // NOI18N
    sumOddButton.setName("sumOddButton"); // NOI18N
    sumOddButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            sumOddButtonActionPerformed(evt);
        }
    });

    removeIntButton.setText(resourceMap.getString("removeIntButton.text")); // NOI18N
    removeIntButton.setName("removeIntButton"); // NOI18N
    removeIntButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            removeIntButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
        mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(mainPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(userOutputField, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                .addComponent(addIntButton)
                                .addGap(89, 89, 89))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(sumAllButton)
                                .addGap(73, 73, 73))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(sumEvenButton)
                                .addGap(61, 61, 61))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(sumOddButton)
                                .addGap(65, 65, 65)))
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addComponent(integerLabel)
                            .addGap(18, 18, 18)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(removeIntButton)
                                .addComponent(enterIntField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(18, 18, 18))
    );
    mainPanelLayout.setVerticalGroup(
        mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(mainPanelLayout.createSequentialGroup()
            .addGap(48, 48, 48)
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(integerLabel)
                        .addComponent(enterIntField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addComponent(removeIntButton))
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(addIntButton)
                            .addGap(8, 8, 8)
                            .addComponent(sumAllButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(sumEvenButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(sumOddButton)))
                    .addGap(19, 19, 19))
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)))
            .addComponent(userOutputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(17, Short.MAX_VALUE))
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(integersums.IntegerSumsApp.class).getContext().getActionMap(IntegerSumsView.class, this);
    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

    progressBar.setName("progressBar"); // NOI18N

    javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
        statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        .addGroup(statusPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(statusMessageLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
            .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(statusAnimationLabel)
            .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
        statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(statusPanelLayout.createSequentialGroup()
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(statusMessageLabel)
                .addComponent(statusAnimationLabel)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(3, 3, 3))
    );

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
}// </editor-fold>

private void addIntButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    //I apologize if these comments are a little imformal...or doesn't quite make sense, I'm fairly new at this
    //Here, you add any integer into the enterIntField, and after pressing the addIntButton, the integer is inputted into the
    //userOutputArea, 
    //the "null" is for when you go to add a second integer, it does not input both first integer again ( as well as the second integer)
    //you can add up to 10 different variables.
    //Now I would also like to say I apologize if my any of my terminology is wrong...or makes no sense

    aryInteger[position] = Integer.parseInt(enterIntField.getText());
    position ++;

    userOutputArea.setText(null);

    for (int i = 0; i < position; i++) {
        userOutputArea.setText(userOutputArea.getText() + aryInteger[i] + "\n");

    }

}                                            

private void sumAllButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    //this adds all numbers inputted and outputs it the userOutputField...

    int sumall = 0;

    for(int i = 0; i < position; i++)
    {
    sumall += aryInteger[i];
    }

    userOutputField.setText("The sum of all numbers is " + sumall);

}                                            

private void sumEvenButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              

    //add all even numbers

    int sumeven = 0;

    for(int i = 0; i < position; i++)
    {
        if(aryInteger[i]%2==0) {
        sumeven += aryInteger[i];
        }
    }
    userOutputField.setText("The sum of all even numbers is " + sumeven);

}                                             

private void sumOddButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    //adds only the odd numbers...

    int sumodd = 0;

    for(int i = 0; i < position; i++)
    {
        if(aryInteger[i]%2!=0) {
        sumodd += aryInteger[i];
        }
    }
    userOutputField.setText("The sum of all odd numbers is " + sumodd);

}                                            

问题就在下面......

private void removeIntButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                

这里是我想创建一个'删除'按钮,它取出已经输入userOutputArea的所有整数。 因此用户可以从头开始而无需退出程序并重新打开它。

我原本以为它会是“我”,因为这是使用变量/字母在'addIntButtonActionPerformed'下面添加整数...但是再次或许“i”只是下划线,因为在公共类下没有提到它? 但那么我会在没有搞乱任何其他东西的情况下添加什么呢? 这对我来说是最有意义的,但是......这只是我用java制作的第五个程序,所以我认为我很有可能犯错误。 我知道这是可怜的,但这是我目前最好的尝试,我在等待回复的时候仍在尝试。 即使是一个小小的提示也会非常感激!

    aryInteger.remove(i);

}                                               
// 3 5 6 8 9
// Variables declaration - do not modify
private javax.swing.JButton addIntButton;
private javax.swing.JTextField enterIntField;
private javax.swing.JLabel integerLabel;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JProgressBar progressBar;
private javax.swing.JButton removeIntButton;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
private javax.swing.JButton sumAllButton;
private javax.swing.JButton sumEvenButton;
private javax.swing.JButton sumOddButton;
private javax.swing.JTextArea userOutputArea;
private javax.swing.JTextField userOutputField;
// End of variables declaration
java arrays swing list textarea
回答

看来你想要清除JTextArea类型组件userOutputArea 如果我做对了,那么这行代码应该这样做:

userOutputArea.setText("");

如果你有其他意思,请进一步澄清你的任务。