Java by API/javax.swing/UIManager

Материал из Java эксперт
Перейти к: навигация, поиск

UIManager: getBorder(Object key)

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.ruponent; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class MainClass {

 public static void main(String args[]) {
   String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
   String headers[] = { "Upper", "Lower" };
   JFrame frame = new JFrame("Label Header");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JTable table = new JTable(rows, headers);
   JScrollPane scrollPane = new JScrollPane(table);
   Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");
   JLabel headerLabel1 = new JLabel(headers[0], JLabel.CENTER);
   headerLabel1.setBorder(headerBorder);
   JLabel headerLabel2 = new JLabel(headers[1], JLabel.CENTER);
   headerLabel2.setBorder(headerBorder);
   TableCellRenderer renderer = new JComponentTableCellRenderer();
   TableColumnModel columnModel = table.getColumnModel();
   TableColumn column0 = columnModel.getColumn(0);
   TableColumn column1 = columnModel.getColumn(1);
   column0.setHeaderRenderer(renderer);
   column0.setHeaderValue(headerLabel1);
   column1.setHeaderRenderer(renderer);
   column1.setHeaderValue(headerLabel2);
   frame.add(scrollPane, BorderLayout.CENTER);
   frame.setSize(300, 150);
   frame.setVisible(true);
 }

} class JComponentTableCellRenderer implements TableCellRenderer {

 public Component getTableCellRendererComponent(JTable table, Object value, 
     boolean isSelected, boolean hasFocus, int row, int column) {
   return (JComponent)value;
 }

}


 </source>
   
  
 
  



UIManager: getCrossPlatformLookAndFeelClassName()

   <source lang="java">
 

import javax.swing.JFrame; import javax.swing.UIManager; public class Main {

 public static void main(String[] args) {
   try {
     UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
   } catch (Exception e) {
     System.err.println("Look and feel not set.");
   }
   JFrame aWindow = new JFrame("This is the Window Title");
   aWindow.setBounds(50, 100, 300, 300);
   aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   aWindow.setVisible(true);
 }

}


 </source>
   
  
 
  



UIManager: getInstalledLookAndFeels()

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class MainClass {

 public static void main(String args[]) {
   final JFrame frame = new JFrame("Change Look");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Object source = actionEvent.getSource();
       String lafClassName = null;
       if (source instanceof JComboBox) {
         JComboBox comboBox = (JComboBox) source;
         lafClassName = (String) comboBox.getSelectedItem();
       } else if (source instanceof JButton) {
         lafClassName = actionEvent.getActionCommand();
       }
       if (lafClassName != null) {
         final String finalLafClassName = lafClassName;
         try {
           UIManager.setLookAndFeel(finalLafClassName);
           SwingUtilities.updateComponentTreeUI(frame);
         } catch (Exception exception) {
           JOptionPane.showMessageDialog(frame, "Can"t change look and feel", "Invalid PLAF",
               JOptionPane.ERROR_MESSAGE);
         }
       }
     }
   };
   UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
   DefaultComboBoxModel model = new DefaultComboBoxModel();
   JComboBox comboBox = new JComboBox(model);
   JPanel panel = new JPanel();
   for (int i = 0, n = looks.length; i < n; i++) {
     JButton button = new JButton(looks[i].getName());
     model.addElement(looks[i].getClassName());
     button.setActionCommand(looks[i].getClassName());
     button.addActionListener(actionListener);
     panel.add(button);
   }
   comboBox.addActionListener(actionListener);
   frame.add(comboBox, BorderLayout.NORTH);
   frame.add(panel, BorderLayout.SOUTH);
   frame.setSize(350, 150);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



UIManager: getLookAndFeelDefaults()

   <source lang="java">
 

import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.swing.UIManager; public class Main {

 public static void main(String[] args) throws Exception {
   Set defaults = UIManager.getLookAndFeelDefaults().entrySet();
   for (Iterator i = defaults.iterator(); i.hasNext();) {
     Map.Entry entry = (Map.Entry) i.next();
     System.out.print(entry.getKey() + " = ");
     System.out.println(entry.getValue());
   }
 }

}


 </source>
   
  
 
  



UIManager: get(Object key)

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIDefaults; import javax.swing.UIManager; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Active Example");
   UIManager.put("LabelFactory", new ActiveLabel());
   final JPanel panel = new JPanel();
   JButton button = new JButton("Get");
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       JLabel label = (JLabel) UIManager.get("LabelFactory");
       panel.add(label);
       panel.revalidate();
     }
   };
   button.addActionListener(actionListener);
   Container contentPane = frame.getContentPane();
   contentPane.add(panel, BorderLayout.CENTER);
   contentPane.add(button, BorderLayout.SOUTH);
   frame.setSize(200, 200);
   frame.setVisible(true);
 }

} class ActiveLabel implements UIDefaults.ActiveValue {

 private int counter = 0;
 public Object createValue(UIDefaults defaults) {
   JLabel label = new JLabel("" + counter++);
   return label;
 }

}


 </source>
   
  
 
  



UIManager: getSystemLookAndFeelClassName()

   <source lang="java">
 

import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class MainClass {

 protected JFrame theFrame = new JFrame("LNF Switcher");
 protected Container cp;
 protected String curLF = "javax.swing.plaf.metal.MetalLookAndFeel";
 protected JRadioButton previousButton;
 public MainClass() {
   theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   cp = theFrame.getContentPane();
   cp.setLayout(new FlowLayout());
   ButtonGroup bg = new ButtonGroup();
   JRadioButton bJava = new JRadioButton("Java");
   bJava.addActionListener(new LNFSetter("javax.swing.plaf.metal.MetalLookAndFeel", bJava));
   bg.add(bJava);
   cp.add(bJava);
   JRadioButton bMSW = new JRadioButton("MS-Windows");
   bMSW.addActionListener(new LNFSetter("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", bMSW));
   bg.add(bMSW);
   cp.add(bMSW);
   JRadioButton bMotif = new JRadioButton("Motif");
   bMotif.addActionListener(new LNFSetter("com.sun.java.swing.plaf.motif.MotifLookAndFeel", bMotif));
   bg.add(bMotif);
   cp.add(bMotif);
   JRadioButton bMac = new JRadioButton("Sun-MacOS");
   bMac.addActionListener(new LNFSetter("com.sun.java.swing.plaf.mac.MacLookAndFeel", bMac));
   bg.add(bMac);
   cp.add(bMac);
   String defaultLookAndFeel = UIManager.getSystemLookAndFeelClassName();
   JRadioButton bDefault = new JRadioButton("Default");
   bDefault.addActionListener(new LNFSetter(defaultLookAndFeel, bDefault));
   bg.add(bDefault);
   cp.add(bDefault);
   (previousButton = bDefault).setSelected(true);
   theFrame.pack();
   theFrame.setVisible(true);
 }
 class LNFSetter implements ActionListener {
   String theLNFName;
   JRadioButton thisButton;
   LNFSetter(String lnfName, JRadioButton me) {
     theLNFName = lnfName;
     thisButton = me;
   }
   public void actionPerformed(ActionEvent e) {
     try {
       UIManager.setLookAndFeel(theLNFName);
       SwingUtilities.updateComponentTreeUI(theFrame);
       theFrame.pack();
     } catch (Exception evt) {
       JOptionPane.showMessageDialog(null, "setLookAndFeel didn"t work: " + evt, "UI Failure",
           JOptionPane.INFORMATION_MESSAGE);
       previousButton.setSelected(true); // reset the GUI to agree
     }
     previousButton = thisButton;
   }
 }
 public static void main(String[] argv) {
   new MainClass();
 }

}


 </source>
   
  
 
  



UIManager.LookAndFeelInfo: getClassName()

   <source lang="java">
 

import javax.swing.UIManager; public class MainClass {

 public static void main (String args[]) {
   UIManager.LookAndFeelInfo plaf[] = UIManager.getInstalledLookAndFeels();
   for (int i=0, n=plaf.length; i<n; i++) {
     System.out.println("Name: " + plaf[i].getName());
     System.out.println("  Class name: " + plaf[i].getClassName());
   }
 }

}


 </source>
   
  
 
  



UIManager.LookAndFeelInfo: getClassName() (2)

   <source lang="java">
 

/* javax.swing.plaf.metal.MetalLookAndFeel com.sun.java.swing.plaf.motif.MotifLookAndFeel com.sun.java.swing.plaf.windows.WindowsLookAndFeel com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel

  • /

import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; public class MainClass {

 public static void main(String[] args) throws Exception {
   UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
   for (int i = 0; i < infos.length; i++) {
     LookAndFeelInfo info = infos[i];
     System.out.println(info.getClassName());
    
   } 
 }

}


 </source>
   
  
 
  



UIManager.LookAndFeelInfo: getName()

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class MainClass {

 public static void main(String args[]) {
   final JFrame frame = new JFrame("Change Look");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Object source = actionEvent.getSource();
       String lafClassName = null;
       if (source instanceof JComboBox) {
         JComboBox comboBox = (JComboBox) source;
         lafClassName = (String) comboBox.getSelectedItem();
       } else if (source instanceof JButton) {
         lafClassName = actionEvent.getActionCommand();
       }
       if (lafClassName != null) {
         final String finalLafClassName = lafClassName;
         try {
           UIManager.setLookAndFeel(finalLafClassName);
           SwingUtilities.updateComponentTreeUI(frame);
         } catch (Exception exception) {
           JOptionPane.showMessageDialog(frame, "Can"t change look and feel", "Invalid PLAF",
               JOptionPane.ERROR_MESSAGE);
         }
       }
     }
   };
   UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
   DefaultComboBoxModel model = new DefaultComboBoxModel();
   JComboBox comboBox = new JComboBox(model);
   JPanel panel = new JPanel();
   for (int i = 0, n = looks.length; i < n; i++) {
     JButton button = new JButton(looks[i].getName());
     model.addElement(looks[i].getClassName());
     button.setActionCommand(looks[i].getClassName());
     button.addActionListener(actionListener);
     panel.add(button);
   }
   comboBox.addActionListener(actionListener);
   frame.add(comboBox, BorderLayout.NORTH);
   frame.add(panel, BorderLayout.SOUTH);
   frame.setSize(350, 150);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



UIManager: put(Object key, Object value)

   <source lang="java">
 

import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; public class MainClass {

 public static void main(final String args[]) {
   UIManager.put("OptionPane.cancelButtonText", "Annuler");
   UIManager.put("OptionPane.noButtonText", "Non");
   UIManager.put("OptionPane.okButtonText", "D"accord");
   UIManager.put("OptionPane.yesButtonText", "Oui");
   int result = JOptionPane.showConfirmDialog(new JFrame(),
       "Est-ce que vous avez 18 ans ou plus?", "Choisisez une option",
       JOptionPane.YES_NO_CANCEL_OPTION);
   if (result == JOptionPane.YES_OPTION) {
     System.out.println("Yes");
   } else if (result == JOptionPane.NO_OPTION) {
     System.out.println("No");
   } else if (result == JOptionPane.CANCEL_OPTION) {
     System.out.println("Cancel");
   } else if (result == JOptionPane.CLOSED_OPTION) {
     System.out.println("Closed");
   }
 }

}



 </source>
   
  
 
  



UIManager: setLookAndFeel(String className)

   <source lang="java">
 

import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class MainClass {

 protected JFrame theFrame = new JFrame("LNF Switcher");
 protected Container cp;
 protected String curLF = "javax.swing.plaf.metal.MetalLookAndFeel";
 protected JRadioButton previousButton;
 public MainClass() {
   theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   cp = theFrame.getContentPane();
   cp.setLayout(new FlowLayout());
   ButtonGroup bg = new ButtonGroup();
   JRadioButton bJava = new JRadioButton("Java");
   bJava.addActionListener(new LNFSetter("javax.swing.plaf.metal.MetalLookAndFeel", bJava));
   bg.add(bJava);
   cp.add(bJava);
   JRadioButton bMSW = new JRadioButton("MS-Windows");
   bMSW.addActionListener(new LNFSetter("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", bMSW));
   bg.add(bMSW);
   cp.add(bMSW);
   JRadioButton bMotif = new JRadioButton("Motif");
   bMotif.addActionListener(new LNFSetter("com.sun.java.swing.plaf.motif.MotifLookAndFeel", bMotif));
   bg.add(bMotif);
   cp.add(bMotif);
   JRadioButton bMac = new JRadioButton("Sun-MacOS");
   bMac.addActionListener(new LNFSetter("com.sun.java.swing.plaf.mac.MacLookAndFeel", bMac));
   bg.add(bMac);
   cp.add(bMac);
   String defaultLookAndFeel = UIManager.getSystemLookAndFeelClassName();
   JRadioButton bDefault = new JRadioButton("Default");
   bDefault.addActionListener(new LNFSetter(defaultLookAndFeel, bDefault));
   bg.add(bDefault);
   cp.add(bDefault);
   (previousButton = bDefault).setSelected(true);
   theFrame.pack();
   theFrame.setVisible(true);
 }
 class LNFSetter implements ActionListener {
   String theLNFName;
   JRadioButton thisButton;
   LNFSetter(String lnfName, JRadioButton me) {
     theLNFName = lnfName;
     thisButton = me;
   }
   public void actionPerformed(ActionEvent e) {
     try {
       UIManager.setLookAndFeel(theLNFName);
       SwingUtilities.updateComponentTreeUI(theFrame);
       theFrame.pack();
     } catch (Exception evt) {
       JOptionPane.showMessageDialog(null, "setLookAndFeel didn"t work: " + evt, "UI Failure",
           JOptionPane.INFORMATION_MESSAGE);
       previousButton.setSelected(true); // reset the GUI to agree
     }
     previousButton = thisButton;
   }
 }
 public static void main(String[] argv) {
   new MainClass();
 }

}


 </source>