Swing et java

[Résolu/Fermé]
Signaler
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
-
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
-
Bonjour,

j'ai trouve un code sur l'utilisation de SWING mais lorsque je le tester sous eclipse j'ai une erreur ,ce meme code marche tres bien avec jcreator ,ais moi il faut creer mon proet avec eclipse ,le voila le code .

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

public class intraface extends JPanel {
    public intraface() {
        super(new GridLayout(1, 1));
        
        JTabbedPane tabbedPane = new JTabbedPane();
        ImageIcon icon = createImageIcon("images/middle.gif");
        
        JComponent panel1 = makeTextPanel("Panel #1");
        tabbedPane.addTab("Tab 1", icon, panel1,
                "Does nothing");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        
        JComponent panel2 = makeTextPanel("Panel #2");
        tabbedPane.addTab("Tab 2", icon, panel2,
                "Does twice as much nothing");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        
        JComponent panel3 = makeTextPanel("Panel #3");
        tabbedPane.addTab("Tab 3", icon, panel3,
                "Still does nothing");
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
        
        JComponent panel4 = makeTextPanel(
                "Panel #4 (has a preferred size of 410 x 50).");
        panel4.setPreferredSize(new Dimension(410, 50));
        tabbedPane.addTab("Tab 4", icon, panel4,
                "Does nothing at all");
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
        
        //Add the tabbed pane to this panel.
        add(tabbedPane);
        
        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }
    
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
    
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = intraface.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from
     * the event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("intraface");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Add content to the window.
        frame.add(new intraface(), BorderLayout.CENTER);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
		UIManager.put("swing.boldMetal", Boolean.FALSE);
		createAndShowGUI();
            }
        });
    }
}







j'ai ce probleme
IWAV0052E Invocation Target Exception creating intarface
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.eclipse.ve.internal.java.vce.launcher.remotevm.JFCLauncher$1.run(JFCLauncher.java:59)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.Error: Unresolved compilation problems: 
	Access restriction: The type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type ImageIcon is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JLabel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JPanel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JComponent is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type SwingUtilities is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type UIManager is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JPanel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The constructor JPanel(LayoutManager) is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The constructor JTabbedPane() is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type ImageIcon is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JComponent is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method addTab(String, Icon, Component, String) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setMnemonicAt(int, int) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JComponent is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method addTab(String, Icon, Component, String) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setMnemonicAt(int, int) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JComponent is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method addTab(String, Icon, Component, String) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setMnemonicAt(int, int) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JComponent is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setPreferredSize(Dimension) from the type JComponent is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method addTab(String, Icon, Component, String) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setMnemonicAt(int, int) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setTabLayoutPolicy(int) from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The field SCROLL_TAB_LAYOUT from the type JTabbedPane is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JComponent is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JPanel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The constructor JPanel(boolean) is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JPanel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JLabel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The constructor JLabel(String) is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JLabel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setHorizontalAlignment(int) from the type JLabel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JLabel is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The field CENTER from the type SwingConstants is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type ImageIcon is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The constructor ImageIcon(URL) is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type ImageIcon is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The constructor JFrame(String) is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method setDefaultCloseOperation(int) from the type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The field EXIT_ON_CLOSE from the type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type SwingUtilities is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method invokeLater(Runnable) from the type SwingUtilities is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type UIManager is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method put(Object, Object) from the type UIManager is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar

	at intarface.<init>(intarface.java:39)
	... 19 more



est ce que c'est un probleme dans ma version d'eclipse


merci d'avance pour toute proposition


5 réponses


Salut,

Il semblerait que ton fichier .java se nomme intarface.java alors que la classe et le constructeur se nomment:
public class intraface extends JPanel {
    public intraface() {


Tu es tout à fait libre de les nommer comme bon te semble mais en respectant les conventions Java avec le nom qui commence par une majuscule et bien entendu partout de la même facon.

Je suppose que tu voulais appeler ton appli Interface.java (I majuscule)

Je te conseille donc de renommer le fichier Interface.java et dans le code TOUS LES intraface Interface et ton code compilera sans erreurs et s'executera correctement (si tu as bien l'image middle.gif).

Corsialement,

Dan
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13
Bonjour DAN ,

c'est pas un problème de nom de la classe ,cette exemple c'est un exemple de SUN et j'ai l'exécuter avec JCREATOR et ça marche ,mais avec Eclipse je ne sais pas est ce que c'est un problème de package ou library SWING ??? est ce que ma version Eclipse qui pose le problème .??

merci
Utilisateur anonyme
Re,

Pourtant l'exception indique bien

.......
.......

at intarface.<init>(intarface.java:39) <<<<line 39
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13 > Utilisateur anonyme
le voila le vrai code dans le lien

https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabbedPaneDemoProject/src/components/TabbedPaneDemo.java




import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

public class TabbedPaneDemo extends JPanel {
    public TabbedPaneDemo() {
        super(new GridLayout(1, 1));
        
        JTabbedPane tabbedPane = new JTabbedPane();
        ImageIcon icon = createImageIcon("images/middle.gif");
        
        JComponent panel1 = makeTextPanel("Panel #1");
        tabbedPane.addTab("Tab 1", icon, panel1,
                "Does nothing");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        
        JComponent panel2 = makeTextPanel("Panel #2");
        tabbedPane.addTab("Tab 2", icon, panel2,
                "Does twice as much nothing");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        
        JComponent panel3 = makeTextPanel("Panel #3");
        tabbedPane.addTab("Tab 3", icon, panel3,
                "Still does nothing");
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
        
        JComponent panel4 = makeTextPanel(
                "Panel #4 (has a preferred size of 410 x 50).");
        panel4.setPreferredSize(new Dimension(410, 50));
        tabbedPane.addTab("Tab 4", icon, panel4,
                "Does nothing at all");
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
        
        //Add the tabbed pane to this panel.
        add(tabbedPane);
        
        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }
    
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
    
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from
     * the event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TabbedPaneDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Add content to the window.
        frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
		UIManager.put("swing.boldMetal", Boolean.FALSE);
		createAndShowGUI();
            }
        });
    }
}
Messages postés
16373
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
4 août 2021
2 861
Le code fonctionne, y compris sur Eclipse, donc le bug dépend surement de ta configuration d'Eclipse. Peut-être un Eclipse 32 bits avec un JDK 64 bits.
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13
merci monsieur KX,

je ne pense pas ,je travail tout sur 32 bit, la bibliothèque AWT fonctionne bien mais tout ce qui concerne swing NON,et aussi avec JCREATOR ça marche très bien ,je pense je vais télécharger une autre version d'Eclipse et je vais voir

merci
Messages postés
16373
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
4 août 2021
2 861
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13
oui,je suis entrain de télécharger une nouvelle version,mais il este toujours une question pourquoi ce problème ?

monsieur KX ,je veux développer une interface graphique qui contient des onglets et menus avec java ,merci de m'envoyer toute lien qui pourra m'aide (si c'est possible bien sure)

merci
Messages postés
16373
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
4 août 2021
2 861
Le problème vient d'un problème de configuration d'Eclipse, à l'exécution il n'a pas réussi à utiliser la bibliothèque runtime "C:\Program Files\Java\jre7\lib\rt.jar" du coup c'est comme s'il ne connaissait aucune des classes de bases. Mais je pense que si tu avais exécuté le code en ligne de commande ça marcherait bien, de même que ça marche avec JCreator ou avec un Eclipse bien configuré.
Le point important à retenir c'est que ce n'est pas un problème de code !
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13
oui,je pense c'est un problème de rt.jar je ne le trouve pas dans la listes des library de mon projet ,j'avais l'ajouter ,le message d'erreur change comme suit

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Access restriction: The type SwingUtilities is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method invokeLater(Runnable) from the type SwingUtilities is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The type UIManager is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar
	Access restriction: The method put(Object, Object) from the type UIManager is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar

	at TabbedPaneDemo.main(TabbedPaneDemo.java:91)




j'ai autoriser l'accès a tout le onde de tout mon DRIVER C mais toujours le meme message d'erreur même

merci en tout cas pour votre temps
Messages postés
16373
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
4 août 2021
2 861
"j'ai autoriser l'accès a tout le onde de tout mon DRIVER C"
C'est plutôt violent comme solution... et inutile !

Éventuellement ce que tu peux faire c'est aller dans ces paramètres de configuration d'Eclipse :
Project > Properties > Java Build Path

Là normalement tu devrais avoir "JRE System Library" et en le développant "rt.jar" doit être dedans.
Si ce n'est pas le cas, tu peux l'ajouter manuellement rt.jar avec Add External JARs
Sinon tu devrais changer de JRE, en sélectionnant Edit puis en choisissant une autre JRE selon les options que tu as disponibles...

Mais normalement Eclipse est déjà configuré de base pour ça...
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13
maintenant ça marche lorsque j'ai installer une autre version d'Eclipse ?
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13
oui rt.jar n'était pas dans le library et j'avais l'ajouter
Messages postés
82
Date d'inscription
mercredi 20 mai 2009
Statut
Membre
Dernière intervention
6 avril 2015
13
lorsque j'ai installer une nouvelle version d'Eclipse ,puis j'ai importer my workspace ,Eclipse m'a proposer de configurer l'accessibilte de rt.jar j'ai fait aléatoirement une valuer de 1 et la mettre accessible et toutes les message d'erreur sont disparaître ,enfin merci a tous