sinat_36930749 2016-12-08 05:55 采纳率: 80%
浏览 7074
已采纳

java中如何调用另一个类中的变量

package newpackage;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.chart.PieChart.Data;
import javax.swing.JFrame;

import javax.swing.JPanel;

//窗体类   
public class MyFrame extends JFrame {  

    /** 
     * @param args 
     */ 
    JPanel jPanel;
    char charA;  
    public MyFrame(){  
        this.setSize(500,100);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.setTitle("Keylogger");  
        this.setVisible(true);  
        this.addKeyListener(new MyKeyListener()); 
        jPanel = new JPanel();
        add(jPanel);


    }  



    public static void main(String[] args) {  
        // TODO Auto-generated method stub   
        new MyFrame();  

    }  

}  

class MyKeyListener extends KeyAdapter{

public void keyPressed(KeyEvent e){

char charA = e.getKeyChar();

String s = null;
try {
s = "You pressed "+charA+" at "+getTime();
} catch (ParseException ex) {
Logger.getLogger(MyKeyListener.class.getName()).log(Level.SEVERE, null, ex);
}

        System.out.println(s); 
    }  

private Date getTime() throws ParseException {
    Date d = new Date();  
       String s = null;  



    s = DateFormat.getDateInstance().format(d);  

    s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);  



    s = DateFormat.getDateInstance(DateFormat.FULL).format(d);  


    s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);  


    s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);  


    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    s = sdf.format(d);  


    DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
    s = sdf2.format(d);  


    DateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");  
    s = sdf3.format(d);  

    s = sdf.format(d);  
    Date today = sdf.parse(s);  

    Calendar c = Calendar.getInstance();  

    return c.getTime();
}


}  



        如果想新建一个类来调用
            class MyKeyListener extends KeyAdapter{  
    public void keyPressed(KeyEvent e){  
        char charA = e.getKeyChar();  
        String  s = null;
        try {
            s = "You pressed "+charA+" at "+getTime();
        } catch (ParseException ex) {
            Logger.getLogger(MyKeyListener.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(s); 
    }   

                            这里的s,但是不知道该怎么编写

                            下面是我新建的类:

package newpackage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class FileWriter {

public static void main(String[] args) throws IOException 


 {
    FileOutputStream f = new FileOutputStream(new File("D:\\temp\\text.txt"));
    PrintStream p = new PrintStream(f);
            MyKeyListener m = new MyKeyListener();


    p.println(s);
    p.close();

}


            希望各位大神能给我一些帮助,感激不尽
  • 写回答

4条回答

  • sinat_36930749 2016-12-08 06:47
    关注

    写了一下现在知道怎么存入了,还有个问题就是我想在我新建的类中调用前面这个类里的s,该怎么写。。。LZ学习JAVA的时间比较短,希望各位大神不吝赐教。。感谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码