Bethy
发贴: 2
积分: 0
|
于 2006-05-14 01:34
求高手帮我修改一下程序!万分感谢~ import java.io.IOException; import java.net.InetAddress; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.border.*; import java.util.Vector; public class MulticastChatFrame extends JFrame implements ActionListener, WindowListener, MulticastChatEventListener {
protected String myusername; protected InetAddress myip = null; protected int myport = -1; // The multicast chat object protected MulticastChat chat; public JFrame Frame1; protected JButton jButton1,jButton2; protected JTextArea jTextArea1,jTextArea2,jTextArea3; protected JList jList1,jList2,jList3,jList4; // Constructs a new swing multicast chat frame (in unconnected state) protected String [] aa={"JAVA基础编程.doc","明天会更好.mp3","QQ2006.exe","梦幻星空.jpg"}; public MulticastChatFrame() { super("MulticastChat (unconnected)"); JPanel contentPane; jList1 = new JList(); jButton1 = new JButton(); jButton2 = new JButton(); jTextArea1 = new JTextArea(); jTextArea2 = new JTextArea(); TitledBorder titledBorder1; TitledBorder titledBorder2; TitledBorder titledBorder3; Component component1; JPopupMenu jPopupMenu1 = new JPopupMenu(); MenuBar menuBar1 = new MenuBar(); MenuBar menuBar2 = new MenuBar(); List list1 = new List(); List list2 = new List(); ScrollPane scrollPane1 = new ScrollPane(); jList2 = new JList(); ScrollPane scrollPane2 = new ScrollPane(); jList3 = new JList(); jTextArea3 = new JTextArea(); jList4 = new JList(); contentPane = (JPanel) this.getContentPane(); titledBorder1 = new TitledBorder(""); titledBorder2 = new TitledBorder(""); titledBorder3 = new TitledBorder(""); component1 = Box.createVerticalStrut; jButton2.setBounds(new Rectangle(225, 219, 73, 44)); jButton2.setFont(new java.awt.Font("Serif", 0, 13)); jButton2.setAlignmentY((float) 0.5); jButton2.setToolTipText(""); jButton2.setText("下载"); jButton1.setBounds(new Rectangle(66, 219, 73, 45)); jButton1.setFont(new java.awt.Font("Serif", 0, 13)); jButton1.setMaximumSize(new Dimension(57, 25)); jButton1.setBorderPainted(true); jButton1.setFocusPainted(true); jButton1.setIcon(null); jButton1.setMargin(new Insets(2, 14, 2, 14)); jButton1.setRolloverEnabled(false); jButton1.setText("共享"); jButton1.setVerticalAlignment(SwingConstants.CENTER); jButton1.addActionListener(this); contentPane.setLayout(null); this.setFont(new java.awt.Font("Dialog", 0, 16)); this.setResizable(true); this.setSize(new Dimension(434, 300)); this.setTitle("P2P共享文件平台"); jList1.setBounds(new Rectangle(176, 145, 0, 0)); jTextArea1.setBackground(Color.pink); jTextArea1.setFont(new java.awt.Font("Serif", 1, 22)); jTextArea1.setPreferredSize(new Dimension(48, 18)); jTextArea1.setRequestFocusEnabled(true); jTextArea1.setToolTipText(""); jTextArea1.setSelectedTextColor(Color.white); jTextArea1.setSelectionEnd; jTextArea1.setText(" 本地共享"); jTextArea1.setRows(0); jTextArea1.setTabSize(12); jTextArea1.setWrapStyleWord(false); jTextArea1.setBounds(new Rectangle(22, 14, 157, 32)); jTextArea2.setBackground(Color.pink); jTextArea2.setFont(new java.awt.Font("Serif", 1, 22)); jTextArea2.setText(" 网络共享"); jTextArea2.setBounds(new Rectangle(182, 14, 156, 32)); contentPane.setMinimumSize(new Dimension(1, 1)); contentPane.setToolTipText(""); component1.setBounds(new Rectangle(99, 31, 0, 8)); list1.setBounds(new Rectangle(204, 65, 0, 0)); list2.setBackground(Color.darkGray); list2.setBounds(new Rectangle(207, 66, 0, 0)); scrollPane1.setBounds(new Rectangle(184, 54, 154, 163)); jList2.setFont(new java.awt.Font("Dialog", 0, 13)); scrollPane2.setBounds(new Rectangle(22, 54, 157, 164)); jList3.setFont(new java.awt.Font("Serif", 0, 13)); jTextArea3.setBackground(Color.pink); jTextArea3.setFont(new java.awt.Font("Serif", 1, 16)); jTextArea3.setText(" 已下载文件"); jTextArea3.setBounds(new Rectangle(352, 16, 106, 32)); jList4.setFont(new java.awt.Font("Serif", 0, 14)); jList4.setBounds(new Rectangle(351, 54, 108, 162)); contentPane.add(jList1, null); contentPane.add(jButton1, null); contentPane.add(jButton2, null); contentPane.add(jTextArea2, null); contentPane.add(component1, null); contentPane.add(jTextArea1, null); contentPane.add(list1, null); contentPane.add(list2, null); contentPane.add(scrollPane1, null); contentPane.add(scrollPane2, null); contentPane.add(jTextArea3, null); contentPane.add(jList4, null); scrollPane2.add(jList3, null); scrollPane1.add(jList2, null); String [] a={"JAVA基础编程.doc","明天会更好.mp3","QQ2006.exe","梦幻星空.jpg"}; jList3.setListData; addWindowListener(this); public void join(String username, InetAddress group, int port, int ttl) throws IOException { setTitle("MulticastChat " + username + "@" + group.getHostAddress() + ":" + port + " [TTL=" + ttl + "]"); chat = new MulticastChat(username, group, port, ttl, this); }
protected void log(final String message) { SwingUtilities.invokeLater(new Runnable() { public void run() { if (message.indexOf("JAVA基础编程.doc") !=-1) { Vector vc=new Vector(); vc.removeAllElements(); vc.add(message); jList2.setListData(vc); } else { System.err.println(message); }
for(int i = 0; i>3;i++) { String keyword = new String(); keyword= aa.getString; //使用keyword进行监听 }
String filename = new String();
} }); }
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jButton1)) {
//String message = messageField.getText(); //messageField.setText(""); System.err.println("jButton1 pushed"); if() { System.out.println("1"+"|"+username+"|"+myip+"|"+myport+"|"+"5"+"|"+"32"+"|"+"16"+"|"+"4"); } String message =new String("JAVA基础编程.doc"); try { chat.sendMessage(message); } catch (Throwable ex) { JOptionPane.showMessageDialog(this, "Error sending message: " + ex.getMessage(), "Chat Error", JOptionPane.ERROR_MESSAGE); } } } public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { try { if (chat != null) { chat.terminate(); } } catch (Throwable ex) { JOptionPane.showMessageDialog(this, "Error leaving chat: " + ex.getMessage(), "Chat Error", JOptionPane.ERROR_MESSAGE); } dispose(); } public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {}
// Invoked by the MulticastChat receive thread when a message has arrived public void chatMessageReceived(String username, InetAddress address, int port, String message) { log(message); }
// Invoked by the MulticastChat receive thread when a user has joined public void chatParticipantJoined(String username, InetAddress address, int port) { log("+++ " + username + " has joined from " + address.getHostName() + ":" + port); }
// Invoked by the MulticastChat receive thread when a user has left public void chatParticipantLeft(String username, InetAddress address, int port) { log("--- " + username + " has left from " + address.getHostName() + ":" + port); }
// Command-line invocation expecting three arguments public static void main(String[] args) { if ((args.length != 5) && (args.length != 6)) { System.err.println("Usage: MulticastChatFrame " + "<username> <group> <port1> <ip> <port2> { <ttl> }"); System.err.println(" - default time-to-live value is 1"); System.exit(1); }
String username = args[0]; InetAddress group = null; int port1 = -1; int ttl = 1;
try { group = InetAddress.getByName(args[1]); } catch (Throwable e) { System.err.println("Invalid multicast group address: " + e.getMessage()); System.exit(1); }
if (!group.isMulticastAddress()) { System.err.println("Group argument '" + args[1] + "' is not a multicast address"); System.exit(1); }
try { port1 = Integer.parseInt(args[2]); } catch (NumberFormatException e) { System.err.println("Invalid port number argument: " + args[2]); System.exit(1); }
if (args.length >= 6) { try { ttl = Integer.parseInt(args[5]); } catch (NumberFormatException e) { System.err.println("Invalid TTL number argument: " + args[3]); System.exit(1); } }
try { MulticastChatFrame frame = new MulticastChatFrame(); frame.setSize(480, 400);
frame.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } });
frame.show();
frame.join(username, group, port1, ttl); } catch (Throwable e) { System.err.println("Error starting frame: " + e.getClass().getName() + ": " + e.getMessage()); System.exit(1); } } }
|