我的项目应该对多项式进行基本运算。每个多项式都是来自 Monom
类的对象的 ArrayList。
我用 Swing 创建了一个界面。我创建了一个方法,将接收到的字符串转换为 Polinom
对象。如果我使用基本命令,比如当我按下按钮、获取文本并显示它时,一切正常。但是当我调用之前提到的方法 (toPolinom
) 时,按钮不再起作用,并且在控制台中显示了红线。
这是按钮代码的样子:
mybutton = new JButton("Suma");
mybutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pol1 = numberField1.getText();
String pol2 = numberField2.getText();
polinom1 = new Polinom(p1);
polinom2 = new Polinom(p2);
polinom1 = polinom1.toPolinom(pol1);
polinom2 = polinom2.toPolinom(pol2);
resultField.setText(polinom1.suma(polinom1, polinom2)
.toString());
}
});
mybutton2 = new JButton("Diferenta");
mybutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pol1 = numberField1.getText();
String pol2 = numberField2.getText();
polinom1 = polinom1.toPolinom(pol1);
polinom2 = polinom2.toPolinom(pol2);
resultField.setText(rezultat.diferenta(polinom1, polinom2)
.toString());
}
});
resultField
和numberField1
都是JTextField
类型;
这是我的 toPolinom()
类 Polinom.java
方法:
public Polinom toPolinom(String p1) {
List<Monom> prez = new ArrayList<Monom>();
int grad, coef;
int i = 0;
for(i=0; i<p1.length()-1; i=i+5){
grad=0; coef=0;
if(p1.charAt(i)=='+') coef=Character.getNumericValue(p1.charAt(i+1));
else if (p1.charAt(i)=='-') coef=-Character.getNumericValue(p1.charAt(i+1));
grad=Character.getNumericValue(p1.charAt(i+4));
prez.add(new MonomZ(grad, coef));
}
return new Polinom(prez);
}
我测试了这个方法,它有效。
最后,这是我点击按钮时收到的信息:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at tema1.Polinom.toString(Polinom.java:208)
at tema1.Fatza$1.actionPerformed(Fatza.java:61)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
添加的警告方法:
public String toString() {
String Rezultat = "";
for (int i = 0; i < polinom.size() - 1; i++) {
Rezultat = Rezultat + verificare(polinom.get(i), polinom, i);
}
return Rezultat
+ verificare(polinom.get(polinom.size() - 1), polinom, //line 208
polinom.size() - 1);
}
private String verificare(Monom monom, List<Monom> polinom, int n) {
// functia verifica daca trebuie adaugat semn sau nu
String text;
boolean ok = true;
for (int i = 0; i < n; i++) {
if (polinom.get(i).getCoef().doubleValue() != 0) {
ok = false;
break;
}
}
double numar = monom.getCoef().doubleValue();
if (ok == true && numar > 0) {
text = monom.toString();
} else {
text = "+" + monom.toString();
}
if (numar < 0) {// numarul are inclus semn negativ
text = monom.toString();
}
return text;
}
如果我忘记了任何信息,请问我。
最佳答案
你在打电话
verificare(polinom.get(polinom.size() - 1), polinom,' 'polinom.size() - 1);
并得到一个 ArrayIndexOutOfBoundsException
,它的值为 -1,表明当您收到错误时,您的 polinom 集合的大小为 0
。对此进行检查,如果大小为 0,则不要调用此方法。
关于java - Swing Java 接口(interface)实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29107670/