swing
Uiterlijk gebruiken
Zoeken…
Het gebruik van systeem L&F
Swing ondersteunt nogal wat native L & F's.
U kunt er altijd gemakkelijk een installeren zonder een specifieke L&F klasse aan te vragen:
public class SystemLookAndFeel
{
public static void main ( final String[] args )
{
// L&F installation should be performed within EDT (Event Dispatch Thread)
// This is important to avoid any UI issues, exceptions or even deadlocks
SwingUtilities.invokeLater ( new Runnable ()
{
@Override
public void run ()
{
// Process of L&F installation might throw multiple exceptions
// It is always up to you whether to handle or ignore them
// In most common cases you would never encounter any of those
try
{
// Installing native L&F as a current application L&F
// We do not know what exactly L&F class is, it is provided by the UIManager
UIManager.setLookAndFeel ( UIManager.getSystemLookAndFeelClassName () );
}
catch ( final ClassNotFoundException e )
{
// L&F class was not found
e.printStackTrace ();
}
catch ( final InstantiationException e )
{
// Exception while instantiating L&F class
e.printStackTrace ();
}
catch ( final IllegalAccessException e )
{
// Class or initializer isn't accessible
e.printStackTrace ();
}
catch ( final UnsupportedLookAndFeelException e )
{
// L&F is not supported on the current system
e.printStackTrace ();
}
// Now we can create some natively-looking UI
// This is just a small sample frame with a single button on it
final JFrame frame = new JFrame ();
final JPanel content = new JPanel ( new FlowLayout () );
content.setBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ) );
content.add ( new JButton ( "Native-looking button" ) );
frame.setContentPane ( content );
frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}
} );
}
}
Dit zijn de native L & F's die JDK ondersteunt (OS -> L&F):
| OS | L&F naam | L&F klasse |
|---|---|---|
| Solaris, Linux met GTK + | GTK + | com.sun.java.swing.plaf.gtk.GTKLookAndFeel |
| Andere Solaris, Linux | Motief | com.sun.java.swing.plaf.motif.MotifLookAndFeel |
| Klassieke Windows | ramen | com.sun.java.swing.plaf.windows.WindowsLookAndFeel |
| Windows XP | Windows XP | com.sun.java.swing.plaf.windows.WindowsLookAndFeel |
| Windows Vista | Windows Vista | com.sun.java.swing.plaf.windows.WindowsLookAndFeel |
| regenjas | regenjas | com.apple.laf.AquaLookAndFeel * |
| IBM UNIX | IBM | javax.swing.plaf.synth.SynthLookAndFeel * |
| HP UX | PK | javax.swing.plaf.synth.SynthLookAndFeel * |
* deze L & F's worden geleverd door de systeemverkoper en de werkelijke naam van de L&F klasse kan variëren
Aangepaste L&F gebruiken
public class CustomLookAndFeel
{
public static void main ( final String[] args )
{
// L&F installation should be performed within EDT (Event Dispatch Thread)
// This is important to avoid any UI issues, exceptions or even deadlocks
SwingUtilities.invokeLater ( new Runnable ()
{
@Override
public void run ()
{
// Process of L&F installation might throw multiple exceptions
// It is always up to you whether to handle or ignore them
// In most common cases you would never encounter any of those
try
{
// Installing custom L&F as a current application L&F
UIManager.setLookAndFeel ( "javax.swing.plaf.metal.MetalLookAndFeel" );
}
catch ( final ClassNotFoundException e )
{
// L&F class was not found
e.printStackTrace ();
}
catch ( final InstantiationException e )
{
// Exception while instantiating L&F class
e.printStackTrace ();
}
catch ( final IllegalAccessException e )
{
// Class or initializer isn't accessible
e.printStackTrace ();
}
catch ( final UnsupportedLookAndFeelException e )
{
// L&F is not supported on the current system
e.printStackTrace ();
}
// Now we can create some pretty-looking UI
// This is just a small sample frame with a single button on it
final JFrame frame = new JFrame ();
final JPanel content = new JPanel ( new FlowLayout () );
content.setBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ) );
content.add ( new JButton ( "Metal button" ) );
frame.setContentPane ( content );
frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}
} );
}
}
Je kunt een enorme lijst met beschikbare Swing L & F's vinden in het onderwerp hier: Java Look and Feel (L&F)
Houd er rekening mee dat sommige van die L & F's op dit moment behoorlijk verouderd kunnen zijn.
Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow