/* * ******************************************************* * name: fortuneJava * author: Lorenzo Ferrara * email: * date: 4 mar 2005 * license: GPL ver 2 * ******************************************************* */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class fortuneJava { public Component createComponents(String frase) { JLabel label = new JLabel(frase); JPanel pane = new JPanel(); pane.setBorder(BorderFactory.createEmptyBorder( 30, //top 30, //left 10, //bottom 30) //right ); pane.setLayout(new GridLayout(0, 1)); pane.add(label); return pane; } public static void main(String[] args) { Process p; Runtime r = Runtime.getRuntime(); InputStream fortuneOutput; String phrase = ""; try { p = r.exec("fortune -a"); //Modify the command to execute p.waitFor(); fortuneOutput = p.getInputStream(); int i; while ((i = fortuneOutput.read()) != -1) { char c = (char)i; if (i == 10) phrase += "
"; if (i == 9) phrase += "    "; else phrase += c; } phrase += ""; } catch(Exception e) { System.out.println("fortune not found"); } try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { } //Create the top-level container and add contents to it. JFrame frame = new JFrame("FortuneJava"); fortuneJava app = new fortuneJava(); Component contents = app.createComponents(phrase); frame.getContentPane().add(contents, BorderLayout.CENTER); //Finish setting up the frame, and show it. frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); frame.pack(); frame.setVisible(true); } }