|
||||||||
![]() |
||||||||
|
|
||||||||
|
|
6
330 JAVA TIPS ••••••••••••••••••••••••••••••••••••••••••••••• Applets Code Examples Databases & beans Distributed systems File Systems - I File Systems II Graphics, AWT, Swing-I Graphics, AWT, Swing-II General Java - I General Java -II General Java -III General Java -IV General Java -V Java Hardware Job, fun... Miscellaneous-I Miscellaneous-II Networking OSs & Java Servlets & Servers Threads Sound & Multimedia String, text, numbers, I/O- I String, text, numbers, I/O- II About Book About Author Excuse me for possible mistakes! English is not native language for me. I will be glad if you send me your corrections of my mistakes! (c)1999, 2000, 2001, 2002, 2003 http://JavaFAQ.nu All rights reserved worldwide. This document can not be changed, either in whole or in part without the express written permission of the publisher. All questions please Receive our newsletter with new tips! More than 13,500 subscribers (by July 2003) can not be wrong! They read our tips every week! To subscribe to "The Java FAQ Daily Tips" weekly edition newsletter send email with "subscribe" word in the header and the body (write just subscribe without ""!!!) to: javafaqtips-request@javafaq.nu or on the web: http://www.javafaq.nu/plm2html/my_subscription.shtml Code Examples Code example: I want to show you one funny thing! The code is shown below is simplest that you can imagine and does very unusual thing! It is slightly bigger than "Hello World!" program but does much more. It lists all files in the current directory if you run it like this: java test * (of course after compilation) in DOS/CMD prompt on Windows or in any shell in UNIX. The program shows all files both in Unix and Windows. If you do: java test .* on UNIX it also shows all hidden files. class test{ public static void main(String args[]){ for (int i = 0;i < args.length; i++) { System.out.println("File " + i + ":" + args[i]); } if (args.length<=0) { System.out.println("No files!"); } } } You can ask how can we get this list without any file handling functionality in the code? Indeed looks mysterious... But in reality everything is very simple. When you type "*" (wildcard) OS (DOS, Windows, UNIX), not Java (!!!) sends the list of files in the current directory to your program as a list of parameters. And you see this list... -- AP (JA) Q: Can anyone answer this - basic, it seems, despite which the answer eludes me completely - question: how do you have multiple windows in Java without using JDesktopPanes and JInternalFrames?? I don't want that kind of environment. I basically want to be able to press a button/menu option to open up a small menu of options/input/buttons like the tools->internet options menu of IE, and I have no idea how to do it. Is it actually possible without using JDPs and JIFs? Is it as simple as creating a separate class for the menu 'mini-window' and creating an instance of it from the main system? Answer: The example you mention is just a fancy dialog. Read documentation on Dialog/JDialog. Also a single application can instantiate and display multiple top level containers such as Frames/JFrames. For example import java.awt.event.*; import javax.swing.*; public class MultiFrameTest extends JFrame { int x = 0; int y = 0; public MultiFrameTest(){ super("multiFrame test"); setSize(200,200); JPanel mainPanel = new JPanel(); setContentPane(mainPanel); JButton addFrameBtn = new JButton("Add a frame"); addFrameBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ addFrame(x,y); } }); mainPanel.add(addFrameBtn); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); setVisible(true); } public void addFrame(int a, int b){ JFrame frame = new JFrame("Frame "+a); frame.setSize(100,100); frame.setLocation( b* 10, b * 10); x++; y +=10; frame.setVisible(true); } public static void main(String[] args){ new MultiFrameTest(); } } -- DB Q: How do I use the DataInputStream and DataOutputStream to transfer a file from the server to the client using sockets in JAVA? Answer: This will run on a single computer. // Client.java import java.net.*; import java.io.*; public class Client { public static void main(String[] args) throws IOException { InetAddress addr = InetAddress.getByName(null); System.out.println("addr = " + addr); Socket socket = new Socket("127.0.0.1",
|
|||||||
|
© WP Technology Inc. 2009
User-posted content is subject to its own terms. |