Java 如何将textfield从主类传递到另一个类
如何将文本字段从main类pri传递到CounterTask1类 下面的程序就是一个例子。实际程序包含类似的结构 在CounterTask1类中,文本字段添加另一个字符串。如果单击“打印”按钮,则应在终端中打印文本字段 提前谢谢Java 如何将textfield从主类传递到另一个类,java,swing,constructor,jtextfield,swingworker,Java,Swing,Constructor,Jtextfield,Swingworker,如何将文本字段从main类pri传递到CounterTask1类 下面的程序就是一个例子。实际程序包含类似的结构 在CounterTask1类中,文本字段添加另一个字符串。如果单击“打印”按钮,则应在终端中打印文本字段 提前谢谢 import java.io.*; import javax.swing.*; import java.awt.event.*; import javax.swing.SwingWorker; import java.util.List; public class
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
import java.io.*;
导入javax.swing.*;
导入java.awt.event.*;
导入javax.swing.SwingWorker;
导入java.util.List;
公共课pri
{
JFrame Frame1=新JFrame();
JLabel SourceLabel1=新的JLabel(“源名称”);
JTextField SourceField1=新的JTextField(20);
公共虚拟主机()
{
最终CounterTask1 task1=新CounterTask1();
Frame1.setLayout(空);
JButton Print=新JButton(“Print”);
打印.立根(10,10100,30);
Print.addActionListener(新ActionListener()
{已执行的公共无效操作(操作事件ae)
{String sourcename=SourceField1.getText();
System.out.println(“终端打印”+sourcename);
task1.execute();}});
JButton Exit=新JButton(“Exit”);
出口.立根(10,50100,30);
Exit.addActionListener(新ActionListener()
{已执行的公共无效操作(操作事件ae)
{System.exit(0);}};
源字段1.立根(130,10100,30);
Frame1.add(SourceField1);
框架1.添加(打印);
框架1.添加(退出);
Frame1.pack();
框架1.设置尺寸(250150);
框架1.设置位置(100100);
Frame1.setVisible(true);
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//大型机
公共静态void main(字符串[]args)
{SwingUtilities.invokeLater(new Runnable())
{public void run()
{
pri帧=新pri();
frame.MainFrame();}});
}
}
类CounterTask1扩展了SwingWorker
{
受保护的整数doInBackground()引发异常
{
String one=SourceField1.getText();
stringtwo=“谢谢!”;
字符串Addst=1+2;
System.out.println(“打印入任务”+Addst);
返回0;
}//受保护的主类
受保护的无效进程(列表块)
{
System.out.println(块);
}
}//计数器任务
我会做不同的事情——将文本传递给SwingWorker的构造函数:
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;
}
class CounterTask1扩展SwingWorker{
私有字符串文本;
公共计数器任务1(字符串文本){
this.text=文本;
}
受保护的整数doInBackground()引发异常{
字符串1=文本;
stringtwo=“谢谢!”;
字符串Addst=1+2;
System.out.println(“打印入任务”+Addst);
返回0;
}
注:
- 如果您需要第二个类来调用第一个类的方法,那么您需要将第一个类的引用传递给第二个类,类似于我在上面传递字符串的方式
- 确保不要从
方法进行Swing调用doInBackground()
- 学习并遵守Java命名约定,以便我们能够更好地理解您未来的代码帖子。类名应以大写字母开头,字段、变量和方法名应以小写字母开头
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;
}
class CounterTask1扩展SwingWorker{
私有字符串文本;
公共计数器任务1(字符串文本){
this.text=文本;
}
受保护的整数doInBackground()引发异常{
字符串1=文本;
stringtwo=“谢谢!”;
字符串Addst=1+2;
System.out.println(“打印入任务”+Addst);
返回0;
}
注:
- 如果您需要第二个类来调用第一个类的方法,那么您需要将第一个类的引用传递给第二个类,类似于我在上面传递字符串的方式
- 确保不要从
方法进行Swing调用doInBackground()
- 学习并遵守Java命名约定,以便我们能够更好地理解您未来的代码帖子。类名应以大写字母开头,字段、变量和方法名应以小写字母开头