2013-04-05 66 views
0
/* 
* 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」 整數......不過話又說回來也許「我」是隻強調這是因爲它沒有提及在公共課上?但那麼我會添加什麼 而不會搞亂其他什麼?然而,這對我來說是最有意義的......但這只是我在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 
+2

請考慮在你的問題中多加點努力,甚至可能寫一兩句關於代碼的作用,你想要什麼以及它不工作的問題。你所做的一切都是發佈代碼和幾個分散的評論。一個很好的問題的一般規則是試圖儘可能多地寫出你的問題,因爲你希望志願者花費回答它。 – 2013-04-05 00:54:56

+0

你的代碼在哪裏嘗試remove()方法?你聲明你已經試圖做到這一點,但你什麼都沒有。請展示您的最佳嘗試,並告訴我們您可能遇到的問題。 – 2013-04-05 01:48:59

+0

@Black_Baron下次嘗試將代碼示例縮小到相關部分。如果您提供一小段代碼仍然會顯示該問題以供他們使用,那麼人們通常會更容易重現並幫助解決您遇到的任何問題。 – 2013-04-06 03:43:27

回答

2

看來要清除出JTextArea類型的組件userOutputArea。如果我說得對,那麼這行代碼應該這樣做:

userOutputArea.setText(""); 

如果你的意思是別的,請進一步澄清你的任務。

+0

嗯......我真的在大笑自己沒有想到這一點,相比於我原本想做的事情來想那麼簡單。我現在肯定覺得自己有點白癡,但是嘿?什麼初學者不時每隔一段時間?無論如何,非常感謝! – 2013-04-05 02:40:08

+0

Hmm.yes適用於。如果我再問一個更荒謬的問題,你介意嗎?爲什麼避免使用null? – 2013-04-05 02:42:36

+0

@HovercraftFullOfEels我被教導在這種情況下使用'NULL'。 'JtextArea'從'JtextComponent'繼承'setText'方法。這是來自方法描述:「如果文本是** null或空**,具有簡單刪除舊文本的效果」[來自這裏:http://docs.oracle.com/javase/6/docs/ api/javax/swing/text/JTextComponent.html#setText(java.lang.String)]那麼如何使用'NULL'比使用空字符串更危險? – 2013-04-05 02:54:16