我有一个包含2个JFormattedTextFields,2个JComboBoxes,2个JLabel和1个JButton的程序。当我在一个JFormattedTextField中输入任何文本,然后单击任何前面提到的控件时,我输入的文本中的JFormattedTextField被清除。那是为什么?
我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
/*'Current' refers to the Current XP/Level controls
*'Desired' refers to the desired XP/Level controls*/
public class RSXPCalc
{
//Variables to use in multiple controls
static private String[] choices = {"Level", "XP"};
/*Global controls*/
//Current XP/Level Controls
static private JFormattedTextField currentBox = new JFormattedTextField(createFormatter("#########"));
static private JLabel currentLabel = new JLabel("Value according to above :");
static private JComboBox<String> currentChoice = new JComboBox<String>(choices);
//Desired XP/Level Controls
static private JFormattedTextField desiredBox = new JFormattedTextField(createFormatter("#########"));
static private JLabel desiredLabel = new JLabel("Value according to above :");
static private JComboBox<String> desiredChoice = new JComboBox<String>(choices);
//Other controls
static private JButton calc = new JButton("Calculate XP"); //Button
static private JLabel CXPL = new JLabel(""); //Current XP Label
//Vairiables used in calculation
static private double CXP, DXP, CLevel, DLevel;
static String temp;
public static void main(String[] args)
{
//Creating the main window
JFrame main = new JFrame("Test Frame");
//Configuring the frame
main.setSize(400,200); //Size
main.setLayout(null); //Allows for free-placement on a coordinate grid of objects
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exitcode
main.setResizable(false); //making it a fixed size
//Configuring the 'current' controls
currentBox.setBounds(165,43,70,30); //Setting the (x,y) location and size of the current level/XP textbox
currentLabel.setBounds(10,43,155,30); //Setting the (x,y) location and size of the current level/XP label
currentChoice.setBounds(10,10,225,30); //Setting the (x,y) location and size of the current level/XP combobox
//Configuring the 'desired' controls
desiredBox.setBounds(165,109,70,30); //Setting the (x,y) location and size of the desired level/XP textbox
desiredLabel.setBounds(10,109,155,30); //Setting the (x,y) location and size of the desired level/XP label
desiredChoice.setBounds(10,76,225,30); //Setting the (x,y) location and size of the desired level/XP combobox
//Configuring the button
calc.setBounds(245, 10, 142, 62);
//Placing the CurrentXP Label
CXPL.setBounds(245, 65, 142, 62);
//Adding the controls to the window and making the window visible\
//'Current' Controls
main.add(currentBox);
main.add(currentLabel);
main.add(currentChoice);
//Desired Controls
main.add(desiredBox);
main.add(desiredLabel);
main.add(desiredChoice);
//Other controls
main.add(calc); //Button
main.add(CXPL); //CurrentXP label
main.setVisible(true);
}
//Format method. Taken from oracle.com
static protected MaskFormatter createFormatter(String s)
{
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
}