Possibilité de changer le style de foenêtre ('Look And Feel')

This commit is contained in:
BuildTools 2015-03-03 16:06:38 +01:00
parent 6bbb25322a
commit 226e4d46a4
3 changed files with 99 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import java.util.Locale;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
@ -67,6 +68,17 @@ public class Main
Locale.setDefault(Locale.FRANCE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
new ExceptionInInitializerError("Erreur lors du changement de 'look and feel'").printStackTrace();
System.err.print("Caused by ");
e.printStackTrace();
}
try
{
new File(Main.class.getResource("/assets").toURI());

View File

@ -3,6 +3,7 @@
*/
package galaxyoyo.gameengine.frame;
import galaxyoyo.gameengine.frame.listeners.ChangeLAFListener;
import galaxyoyo.gameengine.frame.listeners.CreateMapListener;
import galaxyoyo.gameengine.frame.listeners.OpenMapListener;
@ -44,9 +45,14 @@ public class MainFrame extends JFrame
private JMenuBar menuBar = new JMenuBar();
private JMenu fichier = new JMenu("Fichier");
private JMenu display = new JMenu("Affichage");
private JMenu editMaps = new JMenu("Cartes");
private JMenu changeLAF = new JMenu("Modfier l'apparence");
private JMenuItem createMap = new JMenuItem("Cr\u00e9er");
private JMenuItem openMap = new JMenuItem("Ouvrir");
private JMenuItem systemLAF = new JMenuItem("Apparence syst\u00e8me");
private JMenuItem javaLAF = new JMenuItem("Apparence Java");
private JMenuItem darkLAF = new JMenuItem("Apparence sombre");
/**
* Constructeur
@ -64,6 +70,7 @@ public class MainFrame extends JFrame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fichier.setMnemonic(KeyEvent.VK_F + KeyEvent.ALT_DOWN_MASK);
display.setMnemonic(KeyEvent.VK_A + KeyEvent.ALT_DOWN_MASK);
createMap.addActionListener(new CreateMapListener());
editMaps.add(createMap);
@ -72,7 +79,17 @@ public class MainFrame extends JFrame
fichier.add(editMaps);
systemLAF.addActionListener(new ChangeLAFListener(systemLAF, this));
changeLAF.add(systemLAF);
javaLAF.addActionListener(new ChangeLAFListener(javaLAF, this));
changeLAF.add(javaLAF);
darkLAF.addActionListener(new ChangeLAFListener(darkLAF, this));
changeLAF.add(darkLAF);
display.add(changeLAF);
menuBar.add(fichier);
menuBar.add(display);
this.setJMenuBar(menuBar);
}

View File

@ -0,0 +1,70 @@
package galaxyoyo.gameengine.frame.listeners;
import galaxyoyo.gameengine.frame.MainFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ChangeLAFListener implements ActionListener
{
private final JMenuItem item;
private final JFrame frame;
public ChangeLAFListener(JMenuItem LAF, MainFrame f)
{
this.item = LAF;
this.frame = f;
}
@Override
public void actionPerformed(ActionEvent event)
{
if (item.getText().toLowerCase().contains("sys"))
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
new ExceptionInInitializerError("Erreur lors du changement de 'look and feel'").printStackTrace();
System.err.print("Caused by ");
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frame);
}
else if (item.getText().toLowerCase().contains("java"))
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e)
{
new ExceptionInInitializerError("Erreur lors du changement de 'look and feel'").printStackTrace();
System.err.print("Caused by ");
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frame);
}
else if (item.getText().toLowerCase().contains("sombre"))
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch (Exception e)
{
new ExceptionInInitializerError("Erreur lors du changement de 'look and feel'").printStackTrace();
System.err.print("Caused by ");
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frame);
}
}
}