JTextArea实现实时的文本获取,也就是输入文本可以读取里面的值。

package arduino.cwj.test;

import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

class Jp extends JPanel{
	JButton jb=new JButton("hah");
	public Jp(){
		//setSize(100,100);

		jb.setBounds(20,20,50,50);
		this.add(jb);
	}

}

public class Jpanel_test extends JFrame{
	//private  int count=0;
	private JTextArea textArea=new JTextArea();
	//private static Thread t;
	private String s;
	
	public Jpanel_test(){
		setBounds(200,200,400,400);
		Container con=getContentPane();
		textArea.setBounds(100, 100, 50, 20);
		
		Jp jp=new Jp();
		jp.setBounds(100,100,100,100);
		con.add(textArea);
/*		textArea.getDocument().addDocumentListener(new DocumentListener(){

			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				s=textArea.getText().toString();
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				s=textArea.getText().toString();
			}

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				s=textArea.getText().toString();
			}

		});*/
		

	
			

				jp.jb.addMouseListener(new MouseListener(){

					@Override
					public void mouseClicked(MouseEvent e) {
						// TODO Auto-generated method stub
						
						//while(true){
							
							
						//}
						//count

						//jp.jb.setEnabled(false);
						
						
						
					}

					@Override
					public void mouseEntered(MouseEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void mouseExited(MouseEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void mousePressed(MouseEvent arg0) {
						// TODO Auto-generated method stub
						//s=textArea.getText().toString();
					}

					@Override
					public void mouseReleased(MouseEvent arg0) {
						// TODO Auto-generated method stub
						s=textArea.getText();
						
						String a="a";
						if(s.equals(a)){
							JOptionPane.showMessageDialog(null, "保存文件的地址使用绝对路径");
							
						}else {
							System.out.println(s);
						}
						
						
							//System.exit(0);
							//continue;
						
						
					}
					
					
				});
				
				


		//System.out.println(s);
		//System.out.println(textArea.getText());
		con.add(jp);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		Jpanel_test m=new Jpanel_test();
		//System.out.println(m.s);
		//System.out.println(m.textArea.getText());
		
		
	}


	
	

}


/*public class TestShutDownHook { 
    public TestShutDownHook() { 
        doShutDownWork(); 
    } 
    private void doShutDownWork() { 
        Runtime run=Runtime.getRuntime();//当前 Java 应用程序相关的运行时对象。  
        run.addShutdownHook(new Thread(){ //注册新的虚拟机来关闭钩子  
            @Override 
            public void run() { 
                //程序结束时进行的操作  
                System.out.println("程序结束调用"); 
            } 
        }); 
    } 
    public static void main(String[] args) { 
        new TestShutDownHook(); 
        for (int i = 0; i < 1000; i++) { // 在这里增添您需要处理代码 }  
            System.out.println(i); 
        } 
    } 
} */



/*public class Test {
	public static void main(String[] args) {
		Test t = new Test();
		t.SystemIn();
	}
	
	public void SystemIn() {
	    // 写入文件路径
		String path = null;
		// 读入输入内容
		BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
		
		try {
			// 保存输入内容
			StringBuffer value = new StringBuffer();
			// 读入一行内容
			String context = read.readLine();
			while (context != null) {
				if (context.equals("保存")) break;
				value.append(context + "\n");
				read = new BufferedReader(new InputStreamReader(System.in));
				context = read.readLine();
			}
			System.out.println("请输入地址:");
			while(true) {
				// 读入路径
				read = new BufferedReader(new InputStreamReader(System.in));
				if (path == null) {
					path = read.readLine();
					File file = new File(path);
					if (!file.exists()) {
						System.out.println("文件不存在,请输入地址:");
						path = null;
						continue;
					}
					// 写入文件
					PrintWriter print = new PrintWriter(new FileOutputStream(file));
					print.write(value.toString());
					print.close();
				}
				
				if (read.readLine().equals("quit")) {
					System.exit(0);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
*/

 

你可能感兴趣的:(java学习)