每次更改时都从JTextArea获取字符串?

时间:2016-11-20 23:51:18

标签: java swing jtextarea

每次编辑时,我都会尝试打印JTextField的值。我对Swing很新,所以我不知道它的一切。我只是想将它打印到控制台

package searchdatabase.main;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

public static String CURRENT_STRING = null;

//Swing Variables Declare
public static JFrame frame;
public static JPanel panel;
public static JTextField inputField;

public static void main(String[] args) {
    createDisplay("Search Database", 400, 400);
    getCurrentString(60);
}

public static void createDisplay(String title, int width, int height) {
    //Create JFrame
    frame = new JFrame(title);
    frame.setSize(new Dimension(width, height));
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create JPanel
    panel = new JPanel();

    //Create JTextField
    inputField = new JTextField();
    inputField.setPreferredSize(new Dimension(width / 2, height / 22));

    //Adds everything
    panel.add(inputField);
    frame.add(panel);

    //Sets JFrame Visible After adding everything
    frame.setVisible(true);
}

public static void getCurrentString(int max_time) {
    int time = 0;
    String DEF_STRING = null;
    System.out.println("Method Running");
    System.out.println("_______________");
    while(true) {
        time++;
        if(time == max_time) {
            time = 0;
        }
        if(CURRENT_STRING == DEF_STRING) {
            //Don't do anything
        } else if(CURRENT_STRING != DEF_STRING) {
            CURRENT_STRING = inputField.getText();
            System.out.println(CURRENT_STRING);
        } 
    }
 }
}

我认为我把它缩小到getCurrentString方法的错误,主要是在while循环中。

0 个答案:

没有答案