xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/examples/hello-java-awt/Hello.java (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 // Example for use of GNU gettext.
2 // Copyright (C) 2003 Free Software Foundation, Inc.
3 // This file is in the public domain.
4 //
5 // Source code of the Java/AWT program.
6 
7 import java.util.*;
8 import java.io.*;
9 import java.text.*;
10 import java.awt.*;
11 import java.awt.event.*;
12 import gnu.gettext.*;
13 
14 public class Hello {
main(String[] args)15   public static void main (String[] args) {
16     ResourceBundle catalog = ResourceBundle.getBundle("hello-java-awt");
17     Frame frame = new Frame("Hello example");
18     frame.addWindowListener(
19         new WindowAdapter() {
20           public void windowClosing (WindowEvent event) {
21             System.exit(0);
22           }
23         });
24     Label label1 = new Label(GettextResource.gettext(catalog,"Hello, world!"));
25     Label label2 =
26       new Label(
27           MessageFormat.format(
28               GettextResource.gettext(catalog,
29                   "This program is running as process number {0}."),
30               new Object[] { getPid() }));
31     Button button = new Button("OK");
32     button.addActionListener(
33         new ActionListener() {
34           public void actionPerformed (ActionEvent event) {
35             System.exit(0);
36           }
37         });
38     Container labels = new Container();
39     labels.setLayout(new GridLayout(2, 1));
40     labels.add(label1);
41     labels.add(label2);
42     Container buttons = new Container();
43     buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
44     buttons.add(button);
45     frame.setLayout(new BorderLayout());
46     frame.add(labels, BorderLayout.CENTER);
47     frame.add(buttons, BorderLayout.SOUTH);
48     frame.pack();
49     frame.setVisible(true);
50   }
51 
52   /* Return the process ID of the current process.  */
getPid()53   private static String getPid () {
54     try {
55       String[] args = new String[] { "/bin/sh", "-c", "echo $PPID" };
56       Process p = Runtime.getRuntime().exec(args);
57       InputStream p_out = p.getInputStream();
58       String s = (new BufferedReader(new InputStreamReader(p_out))).readLine();
59       p.destroy();
60       if (s != null)
61         return s;
62     } catch (IOException e) {
63       e.printStackTrace();
64     }
65     return "???";
66   }
67 }
68