أويس
عدد المساهمات : 14 تاريخ التسجيل : 04/11/2011 العمر : 35
| موضوع: كود بلغة الجافا يمثل جانب الClient في برنامج دردشه مصغر الجمعة نوفمبر 04, 2011 9:42 pm | |
| شباب دا كود بسيط وأتمنى كل الشباب يستفيدو منو يعني بلاش كوبي بست نحاول نستفيد ونفهمو أول أما كود ال Server حتلقوه في نفس المنتدى[right] كما لا ننسى وجود بعض الصور التي يجب أن ننتبه لها لأنها مستخدمه داخل الكود
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class Client extends JFrame implements ActionListener { JLabel label1,label2,label3,label4,label5,label6,label7; JTextArea text; JScrollPane pane; JTextField field1,field2,field3,field4; JButton sign,send,out,exit; JPanel panel; Font font=new Font("Goudy Old Style",2,20); ImageIcon image1,image2; UIManager.LookAndFeelInfo looks[]; JProgressBar load; Socket socket; DataInputStream indata; DataOutputStream outdata; int port; listen tmp=new listen(); public Client() { super("********************************* Client *********************************"); looks = UIManager.getInstalledLookAndFeels(); try { UIManager.setLookAndFeel(looks[2].getClassName()); SwingUtilities.updateComponentTreeUI(this); } catch(Exception s) { } panel=new APanel(); panel.setLayout(null); label1=new JLabel("Enter The Server IP"); label1.setFont(font); label1.setBounds(50,100,500,50); panel.add(label1); field1=new JTextField(); field1.setFont(font); field1.setBounds(250,105,350,40); panel.add(field1); label2=new JLabel("Enter Your ID"); label2.setFont(font); label2.setBounds(50,180,500,50); panel.add(label2); field2=new JTextField(); field2.setFont(font); field2.setBounds(250,185,250,40); panel.add(field2); label3=new JLabel("Enter The Server Port"); label3.setFont(font); label3.setBounds(50,270,500,50); panel.add(label3); field3=new JTextField(); field3.setFont(font); field3.setBounds(250,275,150,40); panel.add(field3); sign=new JButton("Sign In"); sign.setFont(font); sign.addActionListener(this); sign.setBounds(150,400,130,40); panel.add(sign); image1=new ImageIcon("done.png"); sign.setIcon(image1); exit=new JButton("Exit"); exit.setFont(font); exit.addActionListener(this); exit.setBounds(400,400,130,40); panel.add(exit); image2=new ImageIcon("exit.png"); exit.setIcon(image2); load=new JProgressBar(1,100); load.setBounds(400,500,200,30); panel.add(load); load.setVisible(false); label4=new JLabel("Messages"); label4.setFont(font); label4.setBounds(150,10,300,50); panel.add(label4); label4.setVisible(false); text=new JTextArea(); text.setFont(font); pane=new JScrollPane(text); pane.setBounds(10,60,550,400); text.setEditable(false); pane.setVisible(false); panel.add(pane); label5=new JLabel("Enter Your Message Here "); label5.setFont(font); label5.setBounds(70,450,300,50); label5.setVisible(false); panel.add(label5); field4=new JTextField(); field4.setFont(font); field4.setBounds(10,500,400,40); field4.setVisible(false); panel.add(field4); label6=new JLabel("Welcome"); label6.setFont(font); label6.setBounds(600,150,300,50); panel.add(label6); label6.setVisible(false); label7=new JLabel(); label7.setFont(font); label7.setBounds(580,200,300,50); panel.add(label7); label7.setVisible(false); send=new JButton("Send"); send.setFont(font); send.addActionListener(this); send.setBounds(430,505,130,40); send.setVisible(false); panel.add(send); out=new JButton("Sign Out"); out.setFont(font); out.addActionListener(this); out.setBounds(580,505,130,40); out.setVisible(false); panel.add(out); add(panel); setSize(750,600); setLocation(230,100); setVisible(true); setResizable(false); } public static void main(String args[]) { Client temp=new Client(); temp.setDefaultCloseOperation(0); } boolean flag=true; boolean flag2=true; public void actionPerformed(ActionEvent e) { flag=true; flag2=true; if(e.getSource()==sign) { if(field1.getText().equals("")) { label1.setForeground(Color.red); flag=false; } else { label1.setForeground(Color.black); } if(field2.getText().equals("")) { label2.setForeground(Color.red); flag=false; } else { label2.setForeground(Color.black); } if(field3.getText().equals("")) { label3.setForeground(Color.red); flag=false; } else { for(int i=0;i<field3.getText().length();i++) { if(field3.getText().charAt(i)<'0'||field3.getText().charAt(i)>'9') { label3.setForeground(Color.red); flag=false; flag2=false; } else flag2=true; } if(flag2) label3.setForeground(Color.black); } if(flag) { load.setVisible(true); Load test=new Load(); test.start(); } } if(e.getSource()==send) { try { String xx=field2.getText()+" Says : "+field4.getText(); outdata.writeUTF(xx); } catch(Exception em) { } } if(e.getSource()==out) { int choose=JOptionPane.showOptionDialog(null,"Are You Sure ........",null,0,3,null,null,0); if(choose==0) { try { socket.close(); } catch(Exception mn) { } System.exit(0); } } if(e.getSource()==exit) { int choose=JOptionPane.showOptionDialog(null,"Are You Sure ........",null,0,3,null,null,0); if(choose==0) { try { socket.close(); } catch(Exception mn) { } System.exit(0); } } } class Load extends Thread { public void run() { try { for(int i=0;i<=100;i++) { load.setValue(i); Thread.sleep(10); } load.setVisible(false); Log(); } catch(Exception m) { m.printStackTrace(); } } } public void Log() { try { port=Integer.parseInt(field3.getText()); socket=new Socket(field1.getText(),port); indata=new DataInputStream(socket.getInputStream()); outdata=new DataOutputStream(socket.getOutputStream()); outdata.writeUTF(field2.getText()); String ss=indata.readUTF(); if(!(ss.equals(field2.getText()))) JOptionPane.showMessageDialog(null,ss); else { label1.setVisible(false); label2.setVisible(false); label3.setVisible(false); field1.setVisible(false); field2.setVisible(false); field3.setVisible(false); sign.setVisible(false); exit.setVisible(false); label4.setVisible(true); label5.setVisible(true); label6.setVisible(true); label7.setText(field2.getText()); label7.setVisible(true); field4.setVisible(true); send.setVisible(true); out.setVisible(true); pane.setVisible(true); tmp.start(); } } catch(Exception mn) { if(port!=1990) { JOptionPane.showMessageDialog(null,"Wrong Port"); } else { JOptionPane.showMessageDialog(null,"Wrong IP"); } } } class listen extends Thread { public void run() { while(true) try { DataInputStream indata=new DataInputStream(socket.getInputStream()); String s=indata.readUTF(); text.append(s+"\n"); } catch(Exception e) { JOptionPane.showMessageDialog(null,"The Server Has Just Shut Down"); } } } class APanel extends JPanel { ImageIcon aboutimage = null; public APanel() { aboutimage=new ImageIcon("back.jpg"); setOpaque(false); } public void paint(Graphics g) { aboutimage.paintIcon(this, g, -300, 0); super.paint(g); } public Dimension getPreferredSize() { return new Dimension(aboutimage.getIconWidth(), aboutimage.getIconHeight()); } } }
| |
|
أويس
عدد المساهمات : 14 تاريخ التسجيل : 04/11/2011 العمر : 35
| موضوع: تعقيب الجمعة نوفمبر 04, 2011 9:44 pm | |
| من الواضح في مشاكل في ";" علامة نهاية السطر في الكود "}{" وأيضا بداية ونهاية Functions | |
|