简体   繁体   English

将公共无效方法设置为JTextField

[英]Setting public void method to JTextField

When program is executed void type not allowed here errors shows Program error 当程序被执行时,void类型不允许,这里的错误显示程序错误

resultField.setText((ai.inOrder(ai.root))); resultField.setText((ai.inOrder(ai.root))); causes the error above 导致上述错误

I want the content in the array to be passed through Class BST. 我希望数组中的内容通过Class BST传递。 java then returned in Result TextField. 然后,java在Result TextField中返回。

How do I fix this? 我该如何解决?

Bellow is the code 波纹管是代码

MAIN 主要

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

    }
}

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

Your problem is that your inOrder method is a void method. 您的问题是您的inOrder方法是一个无效方法。 You can't set the text value of your field with a void method, because a void method doesn't return anything. 您不能使用void方法设置字段的文本值,因为void方法不会返回任何内容。

If the inOrder method were to return a String, it would be fine. 如果inOrder方法返回一个String,那就没问题了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM