如何将文本字段从主类传递到另一个类 [英] how to pass textfield from main class to another class

查看:176
本文介绍了如何将文本字段从主类传递到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本字段从主类pri传递到CounterTask1类。

How to pass the textfield from main class pri to CounterTask1 class.

下面的程序就是一个例子。实际程序包含类似的结构。

Below program is an example. Real program contains similar construction.

在CounterTask1类中,textfield添加另一个字符串。如果点击打印按钮,它应该在终端中打印textfield。

In CounterTask1 class the textfield add with another string.If click print button it should print textfield in terminal.

提前感谢。

import java.io.*;
import javax.swing.*; 
import java.awt.event.*;  
import javax.swing.SwingWorker;
import java.util.List;
public class pri 
{
 JFrame Frame1 = new JFrame();
 JLabel SourceLabel1    = new JLabel("Source Name"); 
 JTextField SourceField1 = new JTextField(20);
 public void MainFrame()        
 {
  final CounterTask1 task1 = new CounterTask1();
  Frame1.setLayout(null);    
  JButton Print = new JButton("Print");
  Print.setBounds(10,10,100,30);
  Print.addActionListener(new ActionListener()
  { public void actionPerformed(ActionEvent ae)
    { String sourcename=SourceField1.getText();
      System.out.println("Printing in Terminal "+sourcename);
      task1.execute();          } });

  JButton Exit =  new JButton("Exit");
  Exit.setBounds(10,50,100,30);
  Exit.addActionListener(new ActionListener()
  { public void actionPerformed(ActionEvent ae)
    { System.exit(0); } }); 

        SourceField1.setBounds(130,10,100,30);
        Frame1.add(SourceField1);
        Frame1.add(Print);
        Frame1.add(Exit);
        Frame1.pack();

        Frame1.setSize(250,150);
        Frame1.setLocation(100,100);
        Frame1.setVisible(true);
        Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} //MainFrame

public static void main(String[] args)
{   SwingUtilities.invokeLater(new Runnable()
    { public void run()
        {
            pri frame = new pri();
            frame.MainFrame();  }   });
}
  } 

class CounterTask1 extends SwingWorker<Integer, Integer> 
 {
protected Integer doInBackground() throws Exception
 {

        String one = SourceField1.getText();
        String two = "Thanks !";
        String Addst = one +two ;
        System.out.println("printing in Task" + Addst);
        return 0;

  }// protected main class

protected void process(List<Integer> chunks)
{
    System.out.println(chunks);  
}

} // counter task


推荐答案

我会做不同的事情 - 将文本传递给SwingWorker的构造函数:

I would do things differently -- pass the text to the SwingWorker in its constructor:

  Print.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent ae) {
        String sourcename = SourceField1.getText();
        System.out.println("Printing in Terminal " + sourcename);

        // note change in constructor
        // this way getText() is called on the EDT.
        CounterTask1 task1 = new CounterTask1(SourceField1.getText());
        task1.execute();
     }
  });

和其他类:

class CounterTask1 extends SwingWorker<Integer, Integer> {
   private String text;

   public CounterTask1(String text) {
      this.text = text;
   }

   protected Integer doInBackground() throws Exception {

      String one = text;
      String two = "Thanks !";
      String Addst = one + two;
      System.out.println("printing in Task" + Addst);
      return 0;
   }

注意:


  • 如果你需要第二个类来调用第一个类的方法,那么你将需要传递一个第一个引用到第二个类似于我传递String的方法。

  • 确保不要从 doInBackground()方法中调用Swing。

  • 学习并遵守Java命名约定以便我们可以更好地了解您未来的代码帖子。类名应以大写字母和字段开头,变量和方法名以小写字母开头。

  • If you need the second class to call methods of the first, then you will want to pass a reference of first into second, similar to how I am passing a String above.
  • Be sure not to make Swing calls from the doInBackground() method.
  • Learn and adhere to Java naming convention so that we can better understand your future code posts. Class names should start with a capital letter and field, variable and method names with a lower-case letter.

这篇关于如何将文本字段从主类传递到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆