Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当另一个接收到焦点时,JFormattedTextField被清除_Java_Swing_Jformattedtextfield - Fatal编程技术网

Java 当另一个接收到焦点时,JFormattedTextField被清除

Java 当另一个接收到焦点时,JFormattedTextField被清除,java,swing,jformattedtextfield,Java,Swing,Jformattedtextfield,我有一个包含2个JFormattedTextFields、2个JComboxes、2个JLabel和1个JButton的程序。当我在一个JFormattedTextField中输入任何文本,然后单击前面提到的任何控件时,我输入文本的JFormattedTextField将被清除。为什么呢 我的代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;

我有一个包含2个JFormattedTextFields、2个JComboxes、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;
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
导入javax.swing.text.*;
导入java.beans.PropertyChangeListener;
导入java.beans.PropertyChangeEvent;
导入java.text.*;
/*“当前”是指当前的XP/级别控制
*“所需”是指所需的XP/级别控制*/
公共类RSXPCalc
{
//要在多个控件中使用的变量
静态私有字符串[]选项={“Level”,“XP”};
/*全球控制*/
//当前XP/级别控制
静态私有JFormattedTextField currentBox=新的JFormattedTextField(createFormatter(“########”);
静态专用JLabel currentLabel=新JLabel(“根据上述值:”);
静态专用JComboBox currentChoice=新JComboBox(选项);
//所需的XP/级别控制
静态私有JFormattedTextField desiredBox=新的JFormattedTextField(createFormatter(“#########”);
静态专用JLabel desiredLabel=新JLabel(“根据上述值:”);
静态专用JComboBox desiredChoice=新JComboBox(选项);
//其他控制
静态私有JButton calc=newjbutton(“计算XP”);//按钮
静态专用JLabel CXPL=newjlabel(“”;//当前XP标签
//计算中使用的变量
静态专用双CXP、DXP、CLevel、DLevel;
静态字符串温度;
公共静态void main(字符串[]args)
{
//创建主窗口
JFrame main=新JFrame(“测试框架”);
//配置框架
main.setSize(400200);//大小
main.setLayout(null);//允许在对象的坐标网格上自由放置
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exitcode
main.setresizeable(false);//将其设置为固定大小
//配置“当前”控件
currentBox.setBounds(165,43,70,30);//设置当前级别的(x,y)位置和大小/XP文本框
currentLabel.setBounds(10,43155,30);//设置当前级别/XP标签的(x,y)位置和大小
currentChoice.setBounds(10,10225,30);//设置当前级别/XP组合框的(x,y)位置和大小
//配置“所需”控件
desiredBox.setBounds(165109,70,30);//设置所需级别的(x,y)位置和大小/XP文本框
desiredLabel.setBounds(10109155,30);//设置所需级别/XP标签的(x,y)位置和大小
desiredChoice.setBounds(10,76225,30);//设置所需级别/XP组合框的(x,y)位置和大小
//配置按钮
计算立根数(245,10142,62);
//放置CurrentXP标签
CXPL.挫折(245,65,142,62);
//将控件添加到窗口并使窗口可见\
//“当前”控制
main.add(当前框);
main.add(当前标签);
main.add(当前选择);
//所需控件
main.add(desiredBox);
主。添加(所需标签);
main.add(desiredChoice);
//其他控制
main.add(计算);//按钮
main.add(CXPL);//CurrentXP标签
main.setVisible(true);
}
//格式方法。取自oracle.com
静态保护的MaskFormatter createFormatter(字符串s)
{
MaskFormatter格式化程序=null;
试一试{
格式化程序=新的掩码格式化程序;
}catch(java.text.ParseException exc){
System.err.println(“格式化程序错误:+exc.getMessage());
系统退出(-1);
}
返回格式化程序;
}
}

请在已清除的字段中发布一些最小的代码,并描述您正在键入的内容。@EricGalluzzo Done。无论文本是什么,从格式化字段的角度来看,输入而不是格式化文本字段很可能是无效的。格式要求输入正好是9位数字。它正在按预期工作,这是对的。您已经设置了一个格式设置程序,该格式设置程序只需要9位数字。任何不符合该要求的东西都会被现场丢弃。我建议你看一下这些文件