• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Example for use of GNU gettext.
2 // This file is in the public domain.
3 //
4 // Source code of the Java/Swing program.
5 
6 import java.util.*;
7 import java.io.*;
8 import java.text.*;
9 import java.awt.*;
10 import java.awt.event.*;
11 import javax.swing.*;
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-swing");
17     JFrame frame = new JFrame("Hello example");
18     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
19     JLabel label1 =
20       new JLabel(GettextResource.gettext(catalog,"Hello, world!"));
21     JLabel label2 =
22       new JLabel(
23           MessageFormat.format(
24               GettextResource.gettext(catalog,
25                   "This program is running as process number {0}."),
26               new Object[] { getPid() }));
27     JButton button = new JButton("OK");
28     button.addActionListener(
29         new ActionListener() {
30           public void actionPerformed (ActionEvent event) {
31             System.exit(0);
32           }
33         });
34     JPanel labels = new JPanel();
35     labels.setLayout(new GridLayout(2, 1));
36     labels.add(label1);
37     labels.add(label2);
38     JPanel buttons = new JPanel();
39     buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
40     buttons.add(button);
41     frame.getContentPane().setLayout(new BorderLayout());
42     frame.getContentPane().add(labels, BorderLayout.CENTER);
43     frame.getContentPane().add(buttons, BorderLayout.SOUTH);
44     frame.pack();
45     frame.setVisible(true);
46   }
47 
48   /* Return the process ID of the current process.  */
getPid()49   private static String getPid () {
50     try {
51       String[] args = new String[] { "/bin/sh", "-c", "echo $PPID" };
52       Process p = Runtime.getRuntime().exec(args);
53       InputStream p_out = p.getInputStream();
54       String s = (new BufferedReader(new InputStreamReader(p_out))).readLine();
55       p.destroy();
56       if (s != null)
57         return s;
58     } catch (IOException e) {
59       e.printStackTrace();
60     }
61     return "???";
62   }
63 }
64