1. 程式人生 > >JTextArea實現實時的文字獲取,也就是輸入文字可以讀取裡面的值。

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();
		}
	}
}
*/