gpt4 book ai didi

java - 如何从文本区域中删除输入的数字?

转载 作者:行者123 更新时间:2023-12-02 07:08:31 24 4
gpt4 key购买 nike

/*
* 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

最佳答案

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

userOutputArea.setText("");

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

关于java - 如何从文本区域中删除输入的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15824202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com