[英]Need a method to show results in JTextField
Program Description: The purpose of this program is to allow a user to sort integers and fractions either in descending or ascending order. 程序说明:该程序的目的是允许用户以降序或升序对整数和分数进行排序。 Using a series of 4 radio buttons the user can select the numeric type (integer or fraction) to be sorted and the way the numeric type is going to be sorted (ascending or descending).
使用一系列的4个单选按钮,用户可以选择要排序的数字类型(整数或小数)以及数字类型的排序方式(升序或降序)。 After the user has selected the options, put their numbers or fractions in the JTextField then they can simple hit the "Perform Sort" Button and the sort version of the numbers will appear in the result JTextField.
用户选择了选项后,将其数字或分数放入JTextField,然后他们可以简单地单击“ Perform Sort”按钮,数字的排序版本将出现在结果JTextField中。
Problem: At the ActionListner in the P3GUI.JAVA, resultField.setText(( ai.inOrder(ai.root)));
问题:在P3GUI.JAVA的ActionListner中,resultField.setText
resultField.setText(( ai.inOrder(ai.root)));
is used to display the sorted version of the integers in the resultField JTextField. 用于在resultField JTextField中显示整数的排序版本。 However
resultField.setText(( ai.inOrder(ai.root)));
但是
resultField.setText(( ai.inOrder(ai.root)));
casues "incompatiable types: int cannot be converted to String."
出现
"incompatiable types: int cannot be converted to String."
Maybe a Integer toString method is need?? 也许需要一个整数toString方法? not sure
不确定
Any Suggestion on making this run will be greatly appreciated. 任何建议进行此运行将不胜感激。
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()) { for integers
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((int 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 entvalid amount.");
}
}
}
METHOD BEING CALLED: 调用方法:
public int inOrder(Node node) {
if (node != null) {
inOrder(node.left);
inOrder(node.right);
}
return ((Integer) node.element);
}
/**
*
* @param node
* @return
*/
public int descOrder(Node node) {
if (node != null) {
descOrder(node.right);
descOrder(node.left);
}
return ((Integer) node.element);
}
FULL 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;
/**
*
* @author Mike
*/
public class P3GUI extends JFrame {
JFrame f = new JFrame("Binary Search Tree Sort");// Title
/////////////////////////////////////////////Set up of fields, labels and buttons//////////////////////////////////////////////
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();//create radio buttons group
private final ButtonGroup radioButtons2 = new ButtonGroup();//create radio buttons group
private final JOptionPane popup = new JOptionPane();
/////////////////////////////////////////////Display///////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////Start Panel///////////////////////////////////////////////////////////////////////
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);
/////////////////////////////////////////////JRadioButton label////////////////////////////////////////////////////////////
radioLabel2 = new JLabel(" Numeric Type ");
f.add(radioLabel2);
radioLabel2.setBounds(225, 275, 100, 15);
/////////////////////////////////////////////JRadioButton Ascending///////////////////////////////
integer = new JRadioButton(" Integer ");
radioButtons.add(integer);
f.add(integer);
integer.setBounds(225, 295, 160, 15);
/////////////////////////////////////////////JRadioButton Descending///////////////////////////////
fraction = new JRadioButton(" Fraction ");
radioButtons.add(fraction);
f.add(fraction);
fraction.setBounds(225, 315, 160, 15);
/////////////////////////////////////////////JRadioButton label////////////////////////////////////////////////////////////
radioLabel1 = new JLabel(" Sorted Order ");
f.add(radioLabel1);
radioLabel1.setBounds(10, 275, 100, 15);
/////////////////////////////////////////////JRadioButton Ascending///////////////////////////////
ascending = new JRadioButton(" Ascending ");
radioButtons2.add(ascending);
f.add(ascending);
ascending.setBounds(10, 295, 160, 15);
/////////////////////////////////////////////JRadioButton Descending///////////////////////////////
descending = new JRadioButton(" Descending ");
radioButtons2.add(descending);
f.add(descending);
descending.setBounds(10, 315, 160, 15);
/////////////////////////////////////////////Text Field Label and Field////////////////////////////////////////////////////////
textfieldLabel = new JLabel(" Original List ");
f.add(textfieldLabel);
textfieldLabel.setBounds(42, 10, 160, 25);
/////////////////////////////////////////////Entry Field Label and Field////////////////////////////////////////////////////////
entryField = new JTextField("");
//entryField.addActionListener(this);//ActionListener
f.add(entryField);
entryField.setBounds(118, 10, 245, 25);
/////////////////////////////////////////////Add Evaluate Button///////////////////////////////////////////////////////////////
evaluate = new JButton(" Perform Sort ");//creating instance of JButton
f.add(evaluate);
evaluate.setBounds(137, 180, 130, 30);
/////////////////////////////////////////////Result label and Field////////////////////////////////////////////////////////////
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()) { //ascending for integers
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((int ai.inOrder(ai.root)));
} else if (descending.isSelected() && integer.isSelected()) { // descending for integer
//Do Something
} else if (ascending.isSelected() && fraction.isSelected()) { //ascending for fractions
// Do Something
} else if (descending.isSelected() && fraction.isSelected()) { // descending for fractions
// Do Something
} else {
JOptionPane.showMessageDialog(popup, "Please entvalid
amount.");
}
}
}
//returns value in text field
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();
}
}
BST.JAVA BST.JAVA
package 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;
}
}
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
* @return
*/
public int inOrder(Node node) {
if (node != null) {
inOrder(node.left);
inOrder(node.right);
}
return ((Integer) node.element);
}
/**
*
* @param node
* @return
*/
public int descOrder(Node node) {
if (node != null) {
descOrder(node.right);
descOrder(node.left);
}
return ((Integer) node.element);
}
}
You can try to use NumberFormat to parse int to String, for example: 您可以尝试使用NumberFormat将int解析为String,例如:
DecimalFormat myFormatter = new DecimalFormat("###,###");
String output = myFormatter.format(value);
###,###
represents the pattern that you will to use in your number format. ###,###
代表您将在数字格式中使用的模式。 More information here: https://docs.oracle.com/javase/tutorial/java/data/numberformat.html 此处的更多信息: https : //docs.oracle.com/javase/tutorial/java/data/numberformat.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.