[Java] ComponentOrientation 类使用方法 →→→→→进入此内容的聊天室

来自 , 2019-03-15, 写在 Java, 查看 143 次.
URL http://www.code666.cn/view/abea47ba
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.text.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import javax.swing.border.TitledBorder;
  7.  
  8. public class SimpleExample extends JPanel {
  9.  
  10.   static JFrame frame;
  11.   static Font smallFont;
  12.   static Font mediumFont;
  13.   static Font bigFont;
  14.  
  15.   private ResourceBundle resources;
  16.   private ComponentOrientation co;
  17.  
  18.   private static void applyComponentOrientation(Component c, ComponentOrientation o) {
  19.  
  20.     c.setComponentOrientation(o);
  21.  
  22.     if (c instanceof JMenu) {
  23.       JMenu menu = (JMenu)c;
  24.       int ncomponents = menu.getMenuComponentCount();
  25.       for (int i = 0 ; i < ncomponents ; ++i) {
  26.         applyComponentOrientation( menu.getMenuComponent(i), o );
  27.       }
  28.     } else if (c instanceof Container) {
  29.       Container container = (Container)c;
  30.       int ncomponents = container.getComponentCount();
  31.       for (int i = 0 ; i < ncomponents ; ++i) {
  32.         applyComponentOrientation( container.getComponent(i), o );
  33.       }
  34.     }
  35.   }
  36.  
  37.   private void loadResources() {
  38.     try {
  39.       resources = ResourceBundle.getBundle("resources.Simple",
  40.                         Locale.getDefault());
  41.     } catch (MissingResourceException mre) {
  42.       mre.printStackTrace();
  43.       System.exit(1);
  44.     }
  45.   }
  46.  
  47.   private static JFrame getFrame() {
  48.     return frame;
  49.   }
  50.  
  51.   public SimpleExample() {
  52.  
  53.     // Load our resource bundle
  54.     loadResources();
  55.  
  56.     JRadioButton oneButton, twoButton, threeButton;
  57.     JButton button;
  58.  
  59.     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  60.     ge.getAllFonts();
  61.  
  62.     // Setup the fonts
  63.     smallFont = new Font("Bitstream Cyberbit", Font.PLAIN, 14);
  64.     mediumFont = new Font("Bitstream Cyberbit", Font.PLAIN, 18);
  65.     bigFont = new Font("Bitstream Cyberbit", Font.PLAIN, 20);
  66.  
  67.     co = ComponentOrientation.getOrientation(Locale.getDefault());
  68.  
  69.     setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
  70.     String language = Locale.getDefault().getLanguage();
  71.  
  72.     // Create the buttons
  73.     button = new JButton(resources.getString("Hello"));
  74.     button.setToolTipText(resources.getString("HelloToolTip"));
  75.     button.setFont(mediumFont);
  76.  
  77.     // Setup the buttons
  78.     oneButton = new JRadioButton(resources.getString("One"));
  79.     oneButton.setFont(mediumFont);
  80.     oneButton.setMnemonic(resources.getString("OneMnemonic").charAt(0));
  81.     oneButton.setHorizontalAlignment(JButton.TRAILING);
  82.     oneButton.setHorizontalTextPosition(JButton.TRAILING);
  83.  
  84.     twoButton = new JRadioButton(resources.getString("Two"));
  85.     twoButton.setFont(mediumFont);
  86.     twoButton.setMnemonic(resources.getString("TwoMnemonic").charAt(0));
  87.     twoButton.setHorizontalAlignment(JButton.TRAILING);
  88.     twoButton.setHorizontalTextPosition(JButton.TRAILING);
  89.  
  90.     threeButton = new JRadioButton(resources.getString("Three"));
  91.     threeButton.setFont(mediumFont);
  92.     threeButton.setMnemonic(resources.getString("ThreeMnemonic").charAt(0));
  93.     threeButton.setHorizontalAlignment(JButton.TRAILING);
  94.     threeButton.setHorizontalTextPosition(JButton.TRAILING);
  95.  
  96.     // Group the radio buttons
  97.     ButtonGroup group = new ButtonGroup();
  98.     group.add(oneButton);
  99.     group.add(twoButton);
  100.     group.add(threeButton);
  101.  
  102.     // Register a listener for the radio buttons
  103.     RadioListener myListener = new RadioListener();
  104.     oneButton.addActionListener(myListener);
  105.     twoButton.addActionListener(myListener);
  106.     threeButton.addActionListener(myListener);
  107.  
  108.     // Setup the button panel
  109.     JPanel buttonPanel = new JPanel();
  110.     buttonPanel.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
  111.     TitledBorder tb = new TitledBorder(resources.getString("Numbers"));
  112.     tb.setTitleFont(smallFont);
  113.     tb.setTitleJustification(
  114.         co.isLeftToRight() ? TitledBorder.LEFT : TitledBorder.RIGHT);
  115.  
  116.     buttonPanel.setBorder(tb);
  117.     buttonPanel.setLayout(new FlowLayout());
  118.     buttonPanel.add(button);
  119.     buttonPanel.add(oneButton);
  120.     buttonPanel.add(twoButton);
  121.     buttonPanel.add(threeButton);
  122.  
  123.     add(buttonPanel, BorderLayout.CENTER);
  124.  
  125.     // Setup the date panel
  126.     JPanel datePanel = new JPanel();
  127.     datePanel.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
  128.     tb = new TitledBorder(resources.getString("Dates"));
  129.     tb.setTitleFont(smallFont);
  130.     tb.setTitleJustification(
  131.         co.isLeftToRight() ? TitledBorder.LEFT : TitledBorder.RIGHT);
  132.  
  133.     datePanel.setBorder(tb);
  134.     datePanel.setLayout(new BoxLayout(datePanel,BoxLayout.X_AXIS));
  135.     datePanel.add(Box.createRigidArea(new Dimension(5,1)));
  136.  
  137.  
  138.     JComboBox months = new JComboBox(dfs.getMonths());
  139.     months.setFont(mediumFont);
  140.  
  141.     String weekDays[] = dfs.getWeekdays();
  142.     JComboBox days = new JComboBox();
  143.     days.setFont(mediumFont);
  144.  
  145.     // Determine what day is the first day of the week
  146.  
  147.     int firstDayOfWeek = cal.getFirstDayOfWeek();
  148.     int dayOfWeek;
  149.  
  150.     for (dayOfWeek = firstDayOfWeek; dayOfWeek < weekDays.length; dayOfWeek++)
  151.       days.addItem(weekDays[dayOfWeek]);
  152.  
  153.     for (dayOfWeek = 0; dayOfWeek < firstDayOfWeek; dayOfWeek++)
  154.       days.addItem(weekDays[dayOfWeek]);
  155.  
  156.     if (!co.isLeftToRight()) {
  157.       datePanel.add(days);
  158.       datePanel.add(Box.createRigidArea(new Dimension(5,1)));
  159.  
  160.       datePanel.add(months);
  161.       datePanel.add(Box.createRigidArea(new Dimension(5,1)));
  162.     } else {
  163.       datePanel.add(months);
  164.       datePanel.add(Box.createRigidArea(new Dimension(5,1)));
  165.  
  166.       datePanel.add(days);
  167.       datePanel.add(Box.createRigidArea(new Dimension(5,1)));
  168.     }
  169.     add(datePanel);
  170.  
  171.     // Setup the formatting panel
  172.     JPanel formatPanel = new JPanel();
  173.     formatPanel.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
  174.     tb = new TitledBorder(resources.getString("Formats"));
  175.     tb.setTitleFont(smallFont);
  176.     tb.setTitleJustification(co.isLeftToRight() ?
  177.           TitledBorder.LEFT : TitledBorder.RIGHT);
  178.  
  179.     formatPanel.setBorder(tb);
  180.     formatPanel.setLayout(new BoxLayout(formatPanel,BoxLayout.X_AXIS));
  181.     formatPanel.add(Box.createRigidArea(new Dimension(5,1)));
  182.  
  183.     double theNumber = 1234.56;
  184.     NumberFormat nFormat = NumberFormat.getInstance();
  185.     NumberFormat cFormat = NumberFormat.getCurrencyInstance();
  186.     NumberFormat pFormat = NumberFormat.getPercentInstance();
  187.     DateFormat dFormat = DateFormat.getDateInstance();
  188.  
  189.     JLabel numberLabel = new JLabel(nFormat.format(theNumber));
  190.     numberLabel.setForeground(Color.black);
  191.     numberLabel.setFont(bigFont);
  192.  
  193.     JLabel percentLabel = new JLabel(pFormat.format(theNumber));
  194.     percentLabel.setForeground(Color.black);
  195.     percentLabel.setFont(bigFont);
  196.  
  197.     JLabel currencyLabel = new JLabel(cFormat.format(theNumber));
  198.     currencyLabel.setForeground(Color.black);
  199.     currencyLabel.setFont(bigFont);
  200.  
  201.     JLabel dateLabel = new JLabel(dFormat.format(new Date()));
  202.     dateLabel.setForeground(Color.black);
  203.     dateLabel.setFont(bigFont);
  204.  
  205.     formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  206.  
  207.     if (co.isLeftToRight()) {
  208.       formatPanel.add(numberLabel);
  209.       formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  210.       formatPanel.add(percentLabel);
  211.       formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  212.       formatPanel.add(currencyLabel);
  213.       formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  214.       formatPanel.add(dateLabel);
  215.     } else {
  216.       formatPanel.add(dateLabel);
  217.       formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  218.       formatPanel.add(currencyLabel);
  219.       formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  220.       formatPanel.add(percentLabel);
  221.       formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  222.       formatPanel.add(numberLabel);
  223.     }
  224.     formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
  225.  
  226.     add(formatPanel);
  227.   }
  228.  
  229.   public JMenuBar createMenuBar() {
  230.     JMenuBar menuBar = new JMenuBar();
  231.  
  232.     JMenu file =
  233.       (JMenu) menuBar.add(new JMenu(resources.getString("FileMenu")));
  234.     file.setFont(mediumFont);
  235.       file.setMnemonic(resources.getString("FileMenuMnemonic").charAt(0));
  236.  
  237.     JMenuItem exitItem = (JMenuItem)
  238.       file.add(new JMenuItem(resources.getString("FileMenuExit")));
  239.     exitItem.setFont(mediumFont);
  240.       exitItem.setMnemonic(resources.getString("FileMenuExitMnemonic").charAt(0));
  241.     exitItem.addActionListener(new ActionListener() {
  242.         public void actionPerformed(ActionEvent e) {
  243.           System.exit(0);
  244.         }
  245.       });
  246.  
  247.     menuBar.add(new LocaleChanger());
  248.  
  249.     return menuBar;
  250.   }
  251.  
  252.   public void reloadResources() {
  253.     try {
  254.       resources = ResourceBundle.getBundle("resources.Simple", Locale.getDefault());
  255.     } catch (MissingResourceException mre) {
  256.       mre.printStackTrace();
  257.       System.exit(1);
  258.     }
  259.   }
  260.  
  261.   /**
  262.    * An ActionListener that listens to the radio buttons
  263.    */
  264.   class RadioListener implements ActionListener {
  265.     public void actionPerformed(ActionEvent e) {
  266.       String lnfName = e.getActionCommand();
  267.  
  268.       Object[] options = { resources.getString("OK"), resources.getString("CANCEL") };
  269.       Object[] arguments = { new Integer(3), lnfName };
  270.  
  271.       JOptionPane.sho*****tionDialog(null,
  272.       MessageFormat.format(resources.getString("WarningMsg"), arguments),
  273.           resources.getString("WarningTitle"),
  274.           JOptionPane.DEFAULT_OPTION,
  275.           JOptionPane.WARNING_MESSAGE,
  276.           null, options, options[0]);
  277.       try {
  278.       } catch (Exception exc) {
  279.         JRadioButton button = (JRadioButton)e.getSource();
  280.         button.setEnabled(false);
  281.       }
  282.     }
  283.   }
  284.  
  285.   /**
  286.    * A class to change the locale for the application
  287.    */
  288.   class LocaleChanger extends JMenu implements ItemListener {
  289.  
  290.     public LocaleChanger() {
  291.       super();
  292.       setText(resources.getString("LanguageMenu"));
  293.       setFont(mediumFont);
  294.       setMnemonic(resources.getString("LanguageMenuMnemonic").charAt(0));
  295.  
  296.       ButtonGroup langGroup = new ButtonGroup();
  297.       String language = Locale.getDefault().getLanguage();
  298.  
  299.       // Sort the language names according to the rules specific to each locale
  300.       RuleBasedCollator rbc = (RuleBasedCollator)Collator.getInstance();
  301.       ArrayList al = new ArrayList();
  302.       al.add(resources.getString("Arabic"));
  303.       al.add(resources.getString("Chinese"));
  304.       al.add(resources.getString("English"));
  305.       al.add(resources.getString("German"));
  306.       al.add(resources.getString("Italian"));
  307.       al.add(resources.getString("French"));
  308.       al.add(resources.getString("Hebrew"));
  309.       al.add(resources.getString("Japanese"));
  310.       al.add(resources.getString("Russian"));
  311.  
  312.       Collections.sort(al, rbc);
  313.  
  314.       String langName = Locale.getDefault().getDisplayLanguage();
  315.       for (int i = 0; i < al.size(); i++) {
  316.         JRadioButtonMenuItem mi;
  317.         mi = (JRadioButtonMenuItem)
  318.           add(new JRadioButtonMenuItem((String)al.get(i)));
  319.         mi.setFont(mediumFont);
  320.         if (langName.equalsIgnoreCase((String)al.get(i)))
  321.           mi.setSelected(true);
  322.         mi.addItemListener(this);
  323.         langGroup.add(mi);
  324.       }
  325.     }
  326.  
  327.     public void itemStateChanged(ItemEvent e) {
  328.       JRadioButtonMenuItem rb = (JRadioButtonMenuItem) e.getSource();
  329.       if (rb.isSelected()) {
  330.         String selected = rb.getText();
  331.         if (selected.equals(resources.getString("Arabic"))) {
  332.           Locale.setDefault(new Locale("ar", "EG"));
  333.           co = ComponentOrientation.RIGHT_TO_LEFT;
  334.         } else if (selected.equals(resources.getString("English"))) {
  335.           Locale.setDefault(Locale.US);
  336.           co = ComponentOrientation.LEFT_TO_RIGHT;
  337.         } else if (selected.equals(resources.getString("German"))) {
  338.           Locale.setDefault(Locale.GERMANY);
  339.           co = ComponentOrientation.LEFT_TO_RIGHT;
  340.         } else if (selected.equals(resources.getString("Italian"))) {
  341.           Locale.setDefault(Locale.ITALY);
  342.           co = ComponentOrientation.LEFT_TO_RIGHT;
  343.         } else if (selected.equals(resources.getString("French"))) {
  344.           Locale.setDefault(Locale.FRANCE);
  345.           co = ComponentOrientation.LEFT_TO_RIGHT;
  346.         } else if (selected.equals(resources.getString("Hebrew"))) {
  347.           Locale.setDefault(new Locale("iw", "IL"));
  348.           co = ComponentOrientation.RIGHT_TO_LEFT;
  349.         } else if (selected.equals(resources.getString("Chinese"))) {
  350.           Locale.setDefault(Locale.CHINA);
  351.           co = ComponentOrientation.LEFT_TO_RIGHT;
  352.         } else if (selected.equals(resources.getString("Japanese"))) {
  353.           Locale.setDefault(Locale.JAPAN);
  354.           co = ComponentOrientation.LEFT_TO_RIGHT;
  355.         } else if (selected.equals(resources.getString("Russian"))) {
  356.           Locale.setDefault(new Locale("ru", "RU"));
  357.           co = ComponentOrientation.LEFT_TO_RIGHT;
  358.         }
  359.       }
  360.  
  361.       SimpleExample panel = new SimpleExample();
  362.       SimpleExample.frame.setVisible(false);
  363.       SimpleExample.frame.getContentPane().removeAll();
  364.       SimpleExample.frame.setJMenuBar(panel.createMenuBar());
  365.       SimpleExample.frame.getContentPane().add("Center", panel);
  366.       SimpleExample.frame.pack();
  367.       SimpleExample.frame.show();
  368.       applyComponentOrientation(SimpleExample.getFrame(), co);
  369.     }
  370.   }
  371.  
  372.   public static void main(String [] argv) {
  373.  
  374.     SimpleExample panel = new SimpleExample();
  375.  
  376.     frame = new JFrame("Simple Example");
  377.     frame.addWindowListener(new WindowAdapter() {
  378.       public void windowClosing(WindowEvent e) {System.exit(0);}
  379.     });
  380.     frame.setJMenuBar(panel.createMenuBar());
  381.     frame.getContentPane().add("Center", panel);
  382.     frame.pack();
  383.     frame.setVisible(true);
  384.   }
  385. }
  386. //源代码片段来自云代码http://yuncode.net
  387.                        

回复 "ComponentOrientation 类使用方法"

这儿你可以回复上面这条便签

captcha