就是想问下如果我要在一个类中调用另一个类中的tf.getText()中的内容,该怎么弄呢?比如下面的代码中,该如何做?急用的,请各位高手速回(以下面代码为例子,写具体点,谢谢!)!!!import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.SimpleDateFormat;
public class NewFrame extends JFrame
{
JLabel lab1=new JLabel("设置日期:");
JLabel lab2=new JLabel("设置时间:");
JLabel lab3=new JLabel(" 标题:");
JTextField tf1=new JTextField();
JTextField tf2=new JTextField();
JTextField tf3=new JTextField();
JTextArea ta=new JTextArea();
JButton btnQue=new JButton("确定");
JButton btnCancle=new JButton("取消");
JLabel lab4=new JLabel(" ");
JLabel labCue = new JLabel("");
public NewFrame()
{
super();
setSize(500,600);
Container conn=getContentPane();
conn.setBounds(0,0,450,450);
lab1.setFont(new Font("宋体",Font.BOLD,15));
lab2.setFont(new Font("宋体",Font.BOLD,15));
lab3.setFont(new Font("宋体",Font.BOLD,15)); lab1.setBounds(20,20,90,20);
lab2.setBounds(20,60,90,20);
lab3.setBounds(20,100,90,20);
tf1.setBounds(100,20,200,20);
tf2.setBounds(100,60,200,20);
tf3.setBounds(100,100,200,20);
conn.add(lab1);
conn.add(tf1);
conn.add(lab2);
conn.add(tf2);
conn.add(lab3);
conn.add(tf3);
JScrollPane scroll=new JScrollPane(ta);
scroll.setBounds(20,150,400,200);
conn.add(scroll);
btnQue.setBounds(20,400,90,20);
ButtonHandler handler = new ButtonHandler();
btnQue.addActionListener(handler);
conn.add(btnQue);
btnCancle.setBounds(130,400,90,20);
conn.add(btnCancle);
conn.add(labCue);
labCue.setBounds(20,430,240,30);
lab4.setBounds(20,480,150,20);
conn.add(lab4);
SimpleDateFormat dateformatD=new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat dateformatT=new SimpleDateFormat("HH:mm:ss");
Date date=new Date();
String now=(dateformatD.format(date)).toString();
String now1=(dateformatT.format(date)).toString();
tf1.setText(now);
tf2.setText(now1);
btnCancle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnQue)
{
String sql=" ";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
JOptionPane.showMessageDialog(new Frame(),"确定");
String source="jdbc:odbc:xie";
Connection con=DriverManager.getConnection(source);
con.setAutoCommit(true);
Statement stmt=con.createStatement();
tf1.getText();
tf2.getText();
tf3.getText();
sql="INSERT INTO Table1(date, time, lab, thing) VALUES ('"+tf1.getText()+"', '"+tf2.getText()+"', '"+tf3.getText()+"', '"+ta.getText()+"')";
stmt.executeUpdate(sql);
labCue.setForeground(Color.green);
labCue.setText("数据插入成功");
long oldTime = System.currentTimeMillis();//现记录当前程序启动时的时间,然后启动线程
CountTime ct = new CountTime( oldTime );
//System.out.println( oldTime );
ct.start();
}catch(Exception ew)
{
labCue.setForeground(Color.RED);
labCue.setText("数据插入失败");
}
}
}
}
public void rePaint()
{
tf3.setText(" ");
ta.setText(" ");
}
public static void main(String[] args)
{
NewFrame newframe=new NewFrame();
newframe.setVisible(true);
}
}
class CountTime extends Thread
{
long oldTime = 0;
CountTime( long oldTime )
{
this.oldTime = oldTime;
}
public void run()
{
while(true)
{
//get recent system time
long newTime = System.currentTimeMillis();
//System.out.println( newTime );
if( newTime== Double.parseDouble(tf2.getText()))
{
JOptionPane.showMessageDialog( new Frame(), "TIME OUT" );
System.exit( 0 );
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.SimpleDateFormat;
public class NewFrame extends JFrame
{
JLabel lab1=new JLabel("设置日期:");
JLabel lab2=new JLabel("设置时间:");
JLabel lab3=new JLabel(" 标题:");
JTextField tf1=new JTextField();
JTextField tf2=new JTextField();
JTextField tf3=new JTextField();
JTextArea ta=new JTextArea();
JButton btnQue=new JButton("确定");
JButton btnCancle=new JButton("取消");
JLabel lab4=new JLabel(" ");
JLabel labCue = new JLabel("");
public NewFrame()
{
super();
setSize(500,600);
Container conn=getContentPane();
conn.setBounds(0,0,450,450);
lab1.setFont(new Font("宋体",Font.BOLD,15));
lab2.setFont(new Font("宋体",Font.BOLD,15));
lab3.setFont(new Font("宋体",Font.BOLD,15)); lab1.setBounds(20,20,90,20);
lab2.setBounds(20,60,90,20);
lab3.setBounds(20,100,90,20);
tf1.setBounds(100,20,200,20);
tf2.setBounds(100,60,200,20);
tf3.setBounds(100,100,200,20);
conn.add(lab1);
conn.add(tf1);
conn.add(lab2);
conn.add(tf2);
conn.add(lab3);
conn.add(tf3);
JScrollPane scroll=new JScrollPane(ta);
scroll.setBounds(20,150,400,200);
conn.add(scroll);
btnQue.setBounds(20,400,90,20);
ButtonHandler handler = new ButtonHandler();
btnQue.addActionListener(handler);
conn.add(btnQue);
btnCancle.setBounds(130,400,90,20);
conn.add(btnCancle);
conn.add(labCue);
labCue.setBounds(20,430,240,30);
lab4.setBounds(20,480,150,20);
conn.add(lab4);
SimpleDateFormat dateformatD=new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat dateformatT=new SimpleDateFormat("HH:mm:ss");
Date date=new Date();
String now=(dateformatD.format(date)).toString();
String now1=(dateformatT.format(date)).toString();
tf1.setText(now);
tf2.setText(now1);
btnCancle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnQue)
{
String sql=" ";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
JOptionPane.showMessageDialog(new Frame(),"确定");
String source="jdbc:odbc:xie";
Connection con=DriverManager.getConnection(source);
con.setAutoCommit(true);
Statement stmt=con.createStatement();
tf1.getText();
tf2.getText();
tf3.getText();
sql="INSERT INTO Table1(date, time, lab, thing) VALUES ('"+tf1.getText()+"', '"+tf2.getText()+"', '"+tf3.getText()+"', '"+ta.getText()+"')";
stmt.executeUpdate(sql);
labCue.setForeground(Color.green);
labCue.setText("数据插入成功");
long oldTime = System.currentTimeMillis();//现记录当前程序启动时的时间,然后启动线程
CountTime ct = new CountTime( oldTime );
//System.out.println( oldTime );
ct.start();
}catch(Exception ew)
{
labCue.setForeground(Color.RED);
labCue.setText("数据插入失败");
}
}
}
}
public void rePaint()
{
tf3.setText(" ");
ta.setText(" ");
}
public static void main(String[] args)
{
NewFrame newframe=new NewFrame();
newframe.setVisible(true);
}
}
class CountTime extends Thread
{
long oldTime = 0;
CountTime( long oldTime )
{
this.oldTime = oldTime;
}
public void run()
{
while(true)
{
//get recent system time
long newTime = System.currentTimeMillis();
//System.out.println( newTime );
if( newTime== Double.parseDouble(tf2.getText()))
{
JOptionPane.showMessageDialog( new Frame(), "TIME OUT" );
System.exit( 0 );
}
}
}
}
public void run(JTextComponent tf2)
{
while(true)
{
//get recent system time
long newTime = System.currentTimeMillis();
//System.out.println(new Time);
if( newTime== Double.parseDouble(tf2.getSelectedText()))
{ JOptionPane.showMessageDialog(new Frame(), "TIME OUT");
System.exit(0);
}
}
}
return this.tf2.getText();
} public void setTf2Value(String value) {
this.tf2.setText(value);
} // //////////////////////////////////// 然后在另一个类中:
NewFrame newFrame=new NewFrame(); if (newTime == Double.parseDouble(newFrame.getTf2Value().toString())) {
JOptionPane.showMessageDialog(new Frame(), "TIME OUT");
System.exit(0);
}
伪代码,供参考:class A
{
//attribute
private JTextField tfA;
tfA = new JTextField(); //set a method that can return the value of tf
public String getTextField()
{
return tfA.getText();
}
}class B
{
//create a instance of A
A a = new A(); //attribute
JTextField tfB; public static void main( Stirng[] args )
{
B b = new B();
//get tfA through the A's method - getTextField()
b.tfB = a.getTextField();
}
}