如何使更新的文本在SWING中的JTextField中可见

时间:2015-11-26 07:39:45

标签: java swing jtextfield

我有一个JTextField需要从派生类更新。所以,使用

CLASS TABPANE:

  package forstack;

  import java.awt.Cursor;
  import java.awt.EventQueue;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.beans.PropertyChangeEvent;
  import java.beans.PropertyChangeListener;

  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.SwingWorker;
  import javax.swing.border.EmptyBorder;



        public class TabPane extends JFrame implements ActionListener,                                                                                           propertyChangeListener{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;
protected JTextField textField;

private Task task;

class Task extends SwingWorker<Void, Void> {
    /*
     * Main task. Executed in background thread.
     */
    @Override
    public Void doInBackground() {

        Modbus obj = new Modbus();
        obj.updatetextfield();
        return null;
    }

    /*
     * Executed in event dispatching thread
     */
    @Override
    public void done() {

        setCursor(null); //turn off the wait cursor
        //gcsObj.setVisible(false);


    }


}


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

/**
 * Create the frame.
 */
public TabPane() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(10, 11, 414, 240);
    contentPane.add(panel);
    panel.setLayout(null);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(this);
    btnNewButton.setBounds(160, 142, 89, 23);
    panel.add(btnNewButton);

    textField = new JTextField();
    textField.setText("Mainclass");
    textField.setBounds(119, 61, 175, 20);
    panel.add(textField);
    textField.setColumns(10);
}

@Override
public void propertyChange(PropertyChangeEvent event) {
    // TODO Auto-generated method stub

}


@Override
public void actionPerformed(ActionEvent arg0) {

        // TODO Auto-generated method stub
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //Instances of javax.swing.SwingWorker are not reusuable, so
        //we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();

}

}

CLASS MODBUS:

     package forstack;

     public class Modbus extends TabPane{
/**
 * 
 */
private static final long serialVersionUID = 1L;

public void updatetextfield() {
    // TODO Auto-generated method stub
        textField.setText("hello im here");
        textField.revalidate();
        textField.repaint();

        System.out.println(textField.getText());
   }

 }

输出框架: enter image description here

我在控制台中输出“你好我在这里”,但它在GUI中不可见..

任何人都可以帮我解决这个问题..

1 个答案:

答案 0 :(得分:0)

检查您是否没有2个不同的JTextField实例 尝试重命名JTextFields变量以使其具有不同的名称 看起来你正在更新那个没有在GUI上显示的那个。