xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/examples/hello-java-swing/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/Swing 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 javax.swing.*;
13 import gnu.gettext.*;
14 
15 public class Hello {
main(String[] args)16   public static void main (String[] args) {
17     ResourceBundle catalog = ResourceBundle.getBundle("hello-java-swing");
18     JFrame frame = new JFrame("Hello example");
19     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
20     JLabel label1 =
21       new JLabel(GettextResource.gettext(catalog,"Hello, world!"));
22     JLabel label2 =
23       new JLabel(
24           MessageFormat.format(
25               GettextResource.gettext(catalog,
26                   "This program is running as process number {0}."),
27               new Object[] { getPid() }));
28     JButton button = new JButton("OK");
29     button.addActionListener(
30         new ActionListener() {
31           public void actionPerformed (ActionEvent event) {
32             System.exit(0);
33           }
34         });
35     JPanel labels = new JPanel();
36     labels.setLayout(new GridLayout(2, 1));
37     labels.add(label1);
38     labels.add(label2);
39     JPanel buttons = new JPanel();
40     buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
41     buttons.add(button);
42     frame.getContentPane().setLayout(new BorderLayout());
43     frame.getContentPane().add(labels, BorderLayout.CENTER);
44     frame.getContentPane().add(buttons, BorderLayout.SOUTH);
45     frame.pack();
46     frame.setVisible(true);
47   }
48 
49   /* Return the process ID of the current process.  */
getPid()50   private static String getPid () {
51     try {
52       String[] args = new String[] { "/bin/sh", "-c", "echo $PPID" };
53       Process p = Runtime.getRuntime().exec(args);
54       InputStream p_out = p.getInputStream();
55       String s = (new BufferedReader(new InputStreamReader(p_out))).readLine();
56       p.destroy();
57       if (s != null)
58         return s;
59     } catch (IOException e) {
60       e.printStackTrace();
61     }
62     return "???";
63   }
64 }
65