XXѧԺjavaʵÑ鱨¸æ
public Frame1() { }
//jbInit()·½·¨Íê³É½çÃæµÄ³õʼ»¯ÉèÖà private void jbInit() throws Exception {
37
setLayout(borderLayout1); jButton1.addActionListener(this); jButton2.addActionListener(this);
add(jTextArea1,borderLayout1.CENTER); add(jPanel1,borderLayout1.SOUTH); jPanel1.setLayout(new FlowLayout()); jPanel1.add(jButton1); jPanel1.add(jButton2); setBounds(200,200,600,600);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){ }
setVisible(false); System.exit(0);
});
XXѧԺjavaʵÑ鱨¸æ
}
//±£´æ¡¢´ò¿ªÁ½¸ö°´Å¥µÄʼþ´¦Àí·½·¨ public void actionPerformed(ActionEvent e) { //Èç¹ûÓû§µã»÷±£´æ°´Å¥
if(e.getSource()==jButton1){ JFileChooser fc1=new JFileChooser();
int option = fc1.showSaveDialog(null); // ÏÔʾÎļþ¶Ô»°¿ò
if(option == JFileChooser.APPROVE_OPTION) // Èç¹ûÈ·ÈÏÑ¡È¡Îļþ {
File file = fc1.getSelectedFile(); // È¡µÃÑ¡ÔñµÄÎļþ setTitle(file.toString()); // ÔÚ±êÌâÀ¸ÉÏÉ趨ÎļþÃû³Æ try {
file.createNewFile(); // ½¨Á¢Îļþ }
catch(IOException e1) {
JOptionPane.showMessageDialog(null, e1.toString(),
\ÎÞ·¨½¨Á¢ÐÂÎļþ\ } } }
//Èç¹ûÓû§µã»÷´ò¿ª°´Å¥ if(e.getSource()==jButton2) {
jTextArea1.setText(\
JFileChooser fc=new JFileChooser();
38
XXѧԺjavaʵÑ鱨¸æ
int option1 = fc.showDialog(null, null);
39
// ʹÓÃÕß°´ÏÂÈ·Èϼü
if(option1 == JFileChooser.APPROVE_OPTION) { try {
// ¿ªÆôÑ¡È¡µÄÎļþ BufferedReader buf = new BufferedReader( new FileReader( fc.getSelectedFile()));
setTitle(fc.getSelectedFile().toString());// É趨Îļþ±êÌâ jTextArea1.setText(\Çå³ýÇ°Ò»´ÎÎļþ
String lineSeparator = System.getProperty(\ // È¡µÃϵͳÏàÒÀµÄ»»ÐÐ×Ö·û String text;// ¶ÁÈ¡Îļþ²¢¸½¼ÓÖÁÎÄ×Ö±à¼Çø while((text = buf.readLine()) != null) { }
jTextArea1.append(text); jTextArea1.append(lineSeparator);
buf.close(); }
catch(IOException ee) {
JOptionPane.showMessageDialog(null, ee.toString(), \¿ªÆôÎļþʧ°Ü\ }
XXѧԺjavaʵÑ鱨¸æ
catch(Exception ex){
System.out.print(ex.toString());
} } } }
2£®Ö÷³ÌÐò£º
}
import java.awt.*; import javax.swing*;
public class Application1 {
public Application1( ) {
Frame1 frame = new Frame1(); // ʹ´°¿Ú¾ÓÖÐ
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height)
{ frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width)
{ frameSize.width = screenSize.width; }
frame.setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true); }
//Ö÷³ÌÐòµÄmain()·½·¨
public static void main(String[] args) { new Application1( ); } }
Èý¡¢ÊÔÑéÒªÇó
1¡¢Ô¤Ï°ÊÔÑéÄÚÈݲ¢Ð´³öÉÏ»ú±¨¸æ¡£ 2¡¢ÊµÑéÖгöÏÖµÄÎÊÌ⼰ʵÑéÌå»á¡£
ʵÑéÊ®Îå ÍøÂçͨÐÅ
Ò»¡¢ÊµÑéÄ¿µÄ
1£®Àí½âÍøÂçͨÐŵÄÔÀíºÍ³£ÓÃͨÐż¼ÊõµÄ¸ÅÄ 2£®ÕÆÎÕ»ùÓÚTCPÐÒéµÄÌ×½Ó×ÖµÄÍøÂç±à³Ì·½·¨ 3£®ÕÆÎÕÍøÂçͨÐÅÔÚʵ¼ÊÓ¦Óÿª·¢³ÌÐòÖеÄÓ¦Óᣠ¶þ¡¢ÊµÑéÄÚÈÝ
°¸Àý ÁÄÌì³ÌÐò½âÎö
ͨ¹ý½¨Á¢Ò»¸öSocket¿Í»§¶ËºÍÒ»¸öServerSocket·þÎñ¶Ë½øÐÐʵʱÊý¾Ý½»»»¡£ÔËÐÐʱÏÈ´ò¿ª·þÎñÆ÷¶Ë£¬È»ºó
40