简体   繁体   English

JTextField没有响应

[英]JTextField doesn't respond

I work with Swing and WindowsBuilder, the eclipse environment is running on the unix server. 我使用Swing和WindowsBuilder,在Unix服务器上运行Eclipse环境。

Every time I run the program and try to copy paste some data from another window to JTextField in the program the JTextField stops responding. 我每次运行该程序,并尝试从另一个窗口复制粘贴一些数据JTextField在程序JTextField停止响应。

I mean I press play in the Eclipse, the form is up and the JTextField is responding but then I press alt + tab to move to another window, press ctrl + c and then alt + tab again to go back to the running program but the JTextField doesn't respond to anything. 我的意思是我按Eclipse中的play键,窗体启动并且JTextField响应,但是随后我按alt + tab移到另一个窗口,按ctrl + c然后再按alt + tab可以回到正在运行的程序,但是JTextField不响应任何内容。

help someone? 帮助别人?

Thanks 谢谢

well here is the code: 代码如下:

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.EventQueue;

import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultFormatter;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JCheckBox;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.text.DecimalFormat;

import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.DropMode;


public class MainPage extends JFrame {

    private JPanel contentPane;
    private String sSpe1;
    private String sSpe2;
    private JTextField txtTomB1;
    private JTextField txtTomB2;
    private JTextField txtTomB3;
    private JTextField txtTomB4;
    private JTextField txtTomB5;
    private JTextField txtPotB1;
    private JTextField txtPotB2;
    private JTextField txtPotB3;
    private JTextField txtPotB4;
    private JTextField txtPotB5;
    private JTextField txtAraB1;
    private JTextField txtAraB2;
    private JTextField txtAraB3;
    private JTextField txtAraB4;
    private JTextField txtAraB5;
    private JTextField[] arrTomBaits = new JTextField[5];
    private JTextField[] arrPotBaits = new JTextField[5];
    private JTextField[] arrAraBaits = new JTextField[5];
    private JLabel lblNote;
    private JLabel lblChooseLocation;
    private JTextField txtLocation;
    private JButton btnBrowse;
    private boolean bCanContinue;
    private JSpinner spinner;
    private JSpinner spnRValueBelow;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainPage frame = new MainPage();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainPage()
    {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 551, 609);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setSize(600, 1000);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        txtTomB1 = new JTextField();
        txtTomB1.setDropMode(DropMode.INSERT);
        txtTomB1.setBounds(20, 308, 86, 20);
        contentPane.add(txtTomB1);
        txtTomB1.setColumns(10);
        arrTomBaits[0] = txtTomB1;

        txtTomB2 = new JTextField();
        txtTomB2.setDropMode(DropMode.INSERT);
        txtTomB2.setColumns(10);
        txtTomB2.setBounds(20, 339, 86, 20);
        contentPane.add(txtTomB2);
        arrTomBaits[1] = txtTomB2;

        txtTomB3 = new JTextField();
        txtTomB3.setDropMode(DropMode.INSERT);
        txtTomB3.setColumns(10);
        txtTomB3.setBounds(20, 370, 86, 20);
        contentPane.add(txtTomB3);
        arrTomBaits[2] = txtTomB3;

        txtTomB4 = new JTextField();
        txtTomB4.setDropMode(DropMode.INSERT);
        txtTomB4.setColumns(10);
        txtTomB4.setBounds(20, 401, 86, 20);
        contentPane.add(txtTomB4);
        arrTomBaits[3] = txtTomB4;

        txtTomB5 = new JTextField();
        txtTomB5.setDropMode(DropMode.INSERT);
        txtTomB5.setColumns(10);
        txtTomB5.setBounds(20, 432, 86, 20);
        contentPane.add(txtTomB5);
        arrTomBaits[4] = txtTomB5;

        txtPotB1 = new JTextField();
        txtPotB1.setDropMode(DropMode.INSERT);
        txtPotB1.setColumns(10);
        txtPotB1.setBounds(122, 308, 86, 20);
        contentPane.add(txtPotB1);
        arrPotBaits[0] = txtPotB1;

        txtPotB2 = new JTextField();
        txtPotB2.setDropMode(DropMode.INSERT);
        txtPotB2.setColumns(10);
        txtPotB2.setBounds(122, 339, 86, 20);
        contentPane.add(txtPotB2);
        arrPotBaits[1] = txtPotB2;

        txtPotB3 = new JTextField();
        txtPotB3.setDropMode(DropMode.INSERT);
        txtPotB3.setColumns(10);
        txtPotB3.setBounds(122, 370, 86, 20);
        contentPane.add(txtPotB3);
        arrPotBaits[2] = txtPotB3;

        txtPotB4 = new JTextField();
        txtPotB4.setDropMode(DropMode.INSERT);
        txtPotB4.setColumns(10);
        txtPotB4.setBounds(122, 401, 86, 20);
        contentPane.add(txtPotB4);
        arrPotBaits[3] = txtPotB4;

        txtPotB5 = new JTextField();
        txtPotB5.setDropMode(DropMode.INSERT);
        txtPotB5.setColumns(10);
        txtPotB5.setBounds(122, 432, 86, 20);
        contentPane.add(txtPotB5);
        arrPotBaits[4] = txtPotB5;

        txtAraB1 = new JTextField();
        txtAraB1.setDropMode(DropMode.INSERT);
        txtAraB1.setColumns(10);
        txtAraB1.setBounds(234, 308, 86, 20);
        contentPane.add(txtAraB1);
        arrAraBaits[0] = txtAraB1;

        txtAraB2 = new JTextField();
        txtAraB2.setDropMode(DropMode.INSERT);
        txtAraB2.setColumns(10);
        txtAraB2.setBounds(234, 339, 86, 20);
        contentPane.add(txtAraB2);
        arrAraBaits[1] = txtAraB2;

        txtAraB3 = new JTextField();
        txtAraB3.setDropMode(DropMode.INSERT);
        txtAraB3.setColumns(10);
        txtAraB3.setBounds(234, 370, 86, 20);
        contentPane.add(txtAraB3);
        arrAraBaits[2] = txtAraB3;

        txtAraB4 = new JTextField();
        txtAraB4.setDropMode(DropMode.INSERT);
        txtAraB4.setColumns(10);
        txtAraB4.setBounds(234, 401, 86, 20);
        contentPane.add(txtAraB4);
        arrAraBaits[3] = txtAraB4;

        txtAraB5 = new JTextField();
        txtAraB5.setDropMode(DropMode.INSERT);
        txtAraB5.setColumns(10);
        txtAraB5.setBounds(234, 432, 86, 20);
        contentPane.add(txtAraB5);
        arrAraBaits[4] = txtAraB5;

        txtLocation = new JTextField();
        txtLocation.setBounds(20, 492, 248, 20);
        contentPane.add(txtLocation);
        txtLocation.setColumns(10);
    }
}

the problem is with all the txtARAB#, txtTomB#, txtPotB# and txtLocation 问题出在所有txtARAB#,txtTomB#,txtPotB#和txtLocation

I haven't worked out the code yet. 我还没有制定代码。 But my simple guess is when you "ALT + TAB" back to the swing form your preferred textfield may have lost focus .. so what you can do is add a WindowFocusListener to your JFrame and whenever the focus is gained, transfer the focus to the preferred textbox.. like this.. 但是我的简单猜测是,当您将“ ALT + TAB”放回窗体时,您首选的文本字段可能会失去焦点 ..因此,您可以做的是将WindowFocusListener添加到您的JFrame中,每当获得焦点时,将焦点转移到首选文本框..像这样..

public class TestFrame extends JFrame {

   private JTextField txtBox1; 
   private JTextField txtBox2;

   public TestFrame() {

     // init the components and add to frame

     this.addWindowFocusListener(new WindowFocusListener() {

            @Override
            public void windowLostFocus(WindowEvent arg0) {
                System.out.println("Window Focus Lost");                
            }

            @Override
            public void windowGainedFocus(WindowEvent arg0) {
                System.out.println("Window Focus Gained");  
                txtBox2.requestFocus();
            }
        });

   }

}

So now everytime you get back to your Jframe your textbox would have got focus.. 因此,现在每次您回到Jframe时,您的文本框都会受到关注。

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

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