![]() |
|
|
|
|
|
|
2
30th August 15:59
External User
Posts: 1
|
<Code deleted>
Your code defines class PanelGraph nested within class TestGr1. This is an "inner class". As such, an instance of PanelGraph can only exist within an instance of TestGr1, and you don't have any (nowhere is there a "new TestGr1"). One solution is to not nest PanelGraph within TestGr1. Move it to the end. You will have to remove the "public" modifier as well: import javax.swing.* ; import java.awt.* ; public class TestGr1 { public static void main(String[] args) { // JFrame frame = new JFrame("TestGr1") ; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E); // creation du panel de base JPanel panelBase = new JPanel() ; frame.setContentPane(panelBase) ; PanelGraph graph = new PanelGraph() ; panelBase.add(new JLabel("bonjour")) ; panelBase.add(graph) ; // on rend le frame visible frame.pack() ; frame.setVisible(true) ; } // fin main } // fin TestGr1 class PanelGraph extends JPanel{ public void paintComponent (Graphics g){ super.paintComponent(g) ; g.setColor(Color.orange); g.fillRect(0,0,50,50) ; System.out.println("passe dans paint") ; } // fin paintComponent } // fin panelGraph The above will compile without error and is the simplest solution that I thought of, although not necessarily the best. If your intent is for PanelGraph to be public, then move it into a separate file called PanelGraph, add the "public" modifier, and compile it. (This may involve a new problem having to do with packages, but I didn't test it.) There are other solutions. The appropriate solution depends upon your intentions. See also: http://mindprod.com/jgloss/nestedclasses.html http://mindprod.com/jgloss/static.html -- Ian Shef These are my personal opinions and not those of my employer. |
|
|
|