gpt4 book ai didi

java - 将 public void 方法设置为 JTextField

转载 作者:行者123 更新时间:2023-12-02 13:20:45 27 4
gpt4 key购买 nike

当程序执行时,错误显示 void type not allowed here Program error

resultField.setText((ai.inOrder(ai.root))); 导致上述错误

我希望数组中的内容通过 BST 类传递。 java 然后在 Result TextField 中返回。

如何解决这个问题?

下面是代码

主要

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);
}
}
}

最佳答案

你的问题是你的 inOrder 方法是一个 void 方法。您无法使用 void 方法设置字段的文本值,因为 void 方法不会返回任何内容。

如果 inOrder 方法返回一个字符串,那就没问题了。

关于java - 将 public void 方法设置为 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43581723/

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