【问题标题】:How I remove inputted numbers from a Text Area? [closed]如何从文本区域中删除输入的数字? [关闭]
【发布时间】: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 中的所有整数。 这样用户就可以从头开始,而不必退出程序并重新打开它。

我最初认为它会是“i”,因为这是变量/字母用于在“addIntButtonActionPerformed”下方添加整数 ...但话又说回来,也许“i”只是加了下划线,因为在 public 类下没有提到它?但是我会在那里添加什么 没有弄乱其他任何东西?然而,这对我来说是最有意义的......这只是我用 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

【问题讨论】:

  • 请考虑在您的问题上投入更多精力,甚至可以写一两句话来说明代码的作用以及您正在尝试做什么以及它是如何不起作用的。您所做的只是带有一些分散的 cmets 的邮政编码。一个好问题的一般规则是尽可能多地努力写你的问题,就像你希望志愿者回答它一样。
  • remove() 方法的代码尝试在哪里?你说你已经尝试过这样做,但你没有表现出来。请展示您的最佳尝试并告诉我们您可能遇到的问题。
  • @Black_Baron 下次请尝试将您的代码示例缩小到相关部分。如果您提供的一小段代码仍然存在问题供他们处理,那么人们通常更容易重现并帮助解决您遇到的任何问题。

标签: java arrays swing list textarea


【解决方案1】:

您似乎想清除JTextArea 类型组件userOutputArea。如果我做对了,那么这行代码应该可以做到:

userOutputArea.setText("");

如果你的意思是别的,请进一步澄清你的任务。

【讨论】:

  • 嗯......我真的在笑自己没有想到这一点,与我最初尝试做的事情相比,想到了这么简单的事情。我现在肯定觉得自己像个白痴,但是,嘿?哪个初学者不时不时?总之,非常感谢!
  • 嗯。是的。你介意我再问一个荒谬的问题吗?为什么要避免 null?
  • @HovercraftFullOfEels 我被教导在这种情况下使用NULLJtextAreaJtextComponent 继承 setText 方法。这是来自方法描述:“如果文本为null或空,具有简单删除旧文本的效果” [来自这里:docs.oracle.com/javase/6/docs/api/javax/swing/text/…] 那么如何使用NULL更危险然后使用空字符串?
  • 我错了,你是对的。我一直被教导将其设置为空字符串,以便文本本身不为空,但如果设置为空,则默认为空字符串。请让我删除我上面的​​评论。如果可以的话,我会再次投票给你,但我不能。我必须找到你的另一个答案才能投票。
猜你喜欢
  • 2011-10-29
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多