程序执行时,此类错误显示不允许使用void类型 Program error
resultField.setText((ai.inOrder(ai.root))); 导致上述错误
我希望数组中的内容通过Class BST传递。然后在结果TextField中返回java。
我该如何解决这个问题?
Bellow是代码
主要
package p3gui;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
public class P3GUI extends JFrame {
JFrame f = new JFrame("Binary Search Tree Sort");// Title
private final JButton evaluate;
private final JLabel textfieldLabel;
private final JTextField entryField;
private final JLabel resutfieldlabel;
private final JLabel radioLabel1;
private final JLabel radioLabel2;
private final JTextField resultField;
private final JRadioButton ascending;
private final JRadioButton descending;
private final JRadioButton integer;
private final JRadioButton fraction;
private final ButtonGroup radioButtons = new ButtonGroup();
private final ButtonGroup radioButtons2 = new ButtonGroup();
private final JOptionPane popup = new JOptionPane();
P3GUI()
{
f.setSize(425, 375);
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible //window size
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
radioLabel2 = new JLabel(" Numeric Type ");
f.add(radioLabel2);
radioLabel2.setBounds(225, 275, 100, 15);
integer = new JRadioButton(" Integer ");
radioButtons.add(integer);
f.add(integer);
integer.setBounds(225, 295, 160, 15);
fraction = new JRadioButton(" Fraction ");
radioButtons.add(fraction);
f.add(fraction);
fraction.setBounds(225, 315, 160, 15);
radioLabel1 = new JLabel(" Sorted Order ");
f.add(radioLabel1);
radioLabel1.setBounds(10, 275, 100, 15);
ascending = new JRadioButton(" Ascending ");
radioButtons2.add(ascending);
f.add(ascending);
ascending.setBounds(10, 295, 160, 15);
descending = new JRadioButton(" Descending ");
radioButtons2.add(descending);
f.add(descending);
descending.setBounds(10, 315, 160, 15);
textfieldLabel = new JLabel(" Original List ");
f.add(textfieldLabel);
textfieldLabel.setBounds(42, 10, 160, 25);
entryField = new JTextField("");
//entryField.addActionListener(this);//ActionListener
f.add(entryField);
entryField.setBounds(118, 10, 245, 25);
evaluate = new JButton(" Perform Sort ");
f.add(evaluate);
evaluate.setBounds(137, 180, 130, 30);
resutfieldlabel = new JLabel(" Sorted List ");
f.add(resutfieldlabel);
resutfieldlabel.setBounds(52, 100, 100, 25);
resultField = new JTextField("");
//resultField.addActionListener(this);//ActionListener
resultField.setEditable(false);
f.add(resultField);
resultField.setBounds(125, 100, 220, 25);
evaluate.addActionListener(new getItListener());
}//END of P3GUI
class getItListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String text;
/*text = entryField.getText().trim(). replaceAll(" ","");
String textArray[] = text.split(" ");*/
text = getAmtValue();
String textArray[] = text.split(" ");
if (ascending.isSelected() && integer.isSelected()) {
BST ai = new BST(new Integer(textArray[0]));
for (int i = 1; i < textArray.length; i++) {
ai.insert(ai.root, new Integer(textArray[i]));
}
resultField.setText((ai.inOrder(ai.root)));
} else if (descending.isSelected() && integer.isSelected()) {
//Do Something
} else if (ascending.isSelected() && fraction.isSelected()) {
// Do Something
} else if (descending.isSelected() && fraction.isSelected()) {
// Do Something
} else {
JOptionPane.showMessageDialog(popup, "Please enter valid
amount.");
}
}
}
public String getAmtValue() {
try {
return (entryField.getText().trim().replaceAll(" ", ""));
} catch (NumberFormatException e) {
System.out.println("This is not a number");
eraseTextField();
return "";
}
}
//clears text field
public void eraseTextField() {
entryField.setText("");
entryField.requestFocus();
}
public static void main(String[] args) {
P3GUI p3GUI;
p3GUI = new P3GUI();
}
}
包p3gui;
class Node {
public Object element;
public Node left;
public Node right;
// CONSTRUCTORS
public Node(Object theElement) {
this(theElement, null, null);
}
public Node(Object theElement, Node lLink, Node rLink) {
element = theElement;
this.left = lLink;
this.right = rLink;
}
}
BST.Java
public class BST {
public Node root;
public BST(Object x) { // ONLY CONSTRUCTOR//
root = new Node(x);
}
public Node insert(Node node, Integer x) {
if (node == null) {
return node = new Node(x);
}
if (x < (Integer) node.element) {
node.left = insert(node.left, x);
} else {
node.right = insert(node.right, x);
}
return node;
}
/**
*
* @param node
*/
public void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
inOrder(node.right);
}
}
/**
*
* @param node
*/
public void descOrder(Node node) {
if (node != null) {
descOrder(node.right);
descOrder(node.left);
}
}
}
答案 0 :(得分:1)
您的问题是您的inOrder方法是一种void方法。 您不能使用void方法设置字段的文本值,因为void方法不会返回任何内容。
如果inOrder方法返回一个String,那就没问题了。