Topic: 帮忙改改 (线程) |
Print this page |
1.帮忙改改 (线程) | Copy to clipboard |
Posted by: xingchao Posted on: 2006-12-09 17:38 帮忙改改这个程序 我想从一个text中输入字符 然后从 另一个textArea 中输出 要用线程编写
Ms.java (1.5k) |
2.Re:帮忙改改 (线程) [Re: xingchao] | Copy to clipboard |
Posted by: myfend Posted on: 2006-12-10 11:03 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Demo extends JFrame implements ActionListener,Runnable{ private JButton jbSend = null; private JButton jbExit = null; private JTextField jtfInput = null; private JTextArea jtaContent = null; private Thread runThread = null; private String strContent = null; public Demo(){ super("SendData"); initWin(); setFrameSizeAndLocation(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void setFrameSizeAndLocation(){ this.setSize(640,480); Dimension screenSize = this.getToolkit().getScreenSize(); Dimension winSize = this.getSize(); int left = (screenSize.width-winSize.width)/2; int top = (screenSize.height-winSize.height)/2; this.setLocation(left,top); } private void initWin(){ jbSend = new JButton("Send"); jbSend.addActionListener(this); jbExit = new JButton("Exit"); jbExit.addActionListener(this); jtfInput = new JTextField(); jtaContent = new JTextArea(); Container container = this.getContentPane(); container.setLayout(new BorderLayout()); container.add(jtfInput,BorderLayout.NORTH); container.add(new JScrollPane(jtaContent),BorderLayout.CENTER); JPanel panel = new JPanel(); panel.add(jbSend); panel.add(jbExit); container.add(panel,BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbSend){ runThread = new Thread(this,"tSendData"); runThread.start(); }else if(e.getSource()==jbExit){ System.exit(0); } } public void run(){ Runnable r = new Runnable(){ public void run(){ jtaContent.append(strContent+"\n"); } }; try{ strContent = jtfInput.getText(); SwingUtilities.invokeAndWait(r); }catch(Exception exc){ exc.printStackTrace(); } } public static void main(String[] args){ new Demo(); } } Demo.java (1.96k) |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |